我有一个 MVC3 Web 应用程序。在 index.cshtml
上,我有两个下拉列表。当我从这些列表中进行选择时,我需要单击next
按钮,并且我想显示所选的值。我该怎么做?
homecontroller.cs
DataRepository objRepository = new DataRepository();
public ActionResult Index()
{
ViewModel objViewModel = new ViewModel();
objViewModel.ID = objRepository.GetPricingSecurityID();
objViewModel.ddlId = objRepository.GetCUSIP();
return View(objViewModel);
}
ViewModel.cs
public class ViewModel
{
//DDL ID
[Required(ErrorMessage = "Please select a PricingSecurityID")]
public List<SelectListItem> ddlId { get; set; }
//DropDownList Values
[Required(ErrorMessage = "Please select a PricingSecurityID")]
public List<SelectListItem> ID { get; set; }
}
index.cshtml
<div class="editor-label">
@Html.Label("Pricing SecurityID")
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.ID,
new SelectList(Model.ID, "Value", "Text"),
"-- Select category --"
)
@Html.ValidationMessageFor(model => model.ID)
</div>
<div class="editor-label">
@Html.Label("CUSIP ID")
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.ddlId,
new SelectList(Model.ddlId, "Value", "Text"),
"-- Select category --"
)
@Html.ValidationMessageFor(model => model.ddlId)
</div>
<p>
<input type="submit" value="Next" />
</p>
如何显示选定的值?
最佳答案
如果您的要求是构建某种向导,则需要一种在步骤之间维护状态的方法。
ViewBag 对此没有好处,因为您应该遵循每个向导步骤的 PRG(发布/重定向/获取)模式。
TempData 适用于在步骤之间向前导航,但如果用户返回或直接导航到步骤,TempData 就会失败。
因此,您需要使用生命周期更长的产品。 ASP.NET Session 对象或数据库都是很好的选择。
这是一个例子:
public class WizardController : Controller
{
public ActionResult Step1()
{
var session = GetWizardSession();
if (session.Step1 == null)
{
session.Step1 = new Step1View
{
PricingSecurityIds = new SelectList(new[] { 1, 2, 3, 4, 5 }),
SomeOtherIds = new SelectList(new[] { 1, 2, 3, 4, 5 })
};
}
return View(session.Step1);
}
[HttpPost]
public ActionResult Step1(Step1View cmd)
{
var session = GetWizardSession();
// save the wizard state
session.Step1.SelectedPricingSecurityId = cmd.SelectedPricingSecurityId;
session.Step1.SelectedSomeOtherId = cmd.SelectedSomeOtherId;
// now onto step 2
session.Step2 = new Step2View
{
PricingSecurityId = cmd.SelectedPricingSecurityId,
SomeOtherId = cmd.SelectedSomeOtherId,
Name = "John Smith"
};
return RedirectToAction("step2");
}
public ActionResult Step2()
{
return View(GetWizardSession().Step2);
}
public WizardSession GetWizardSession()
{
var session = Session["wizardsession"];
if (session == null)
{
session = new WizardSession();
Session["wizardsession"] = session;
}
return session as WizardSession;
}
}
public class Step1View
{
public SelectList PricingSecurityIds { get; set; }
public SelectList SomeOtherIds { get; set; }
public int SelectedPricingSecurityId { get; set; }
public int SelectedSomeOtherId { get; set; }
}
public class Step2View
{
public int PricingSecurityId { get; set; }
public int SomeOtherId { get; set; }
public string Name { get; set; }
}
public class WizardSession
{
public Step1View Step1 { get; set; }
public Step2View Step2 { get; set; }
}
- 在第 1 步中,我们调用
GetWizardSession
。这会从 ASP.NETSession
返回一个对象其中包含我们为向导中每个步骤收集的所有信息。在此示例中,我们仅存储每个步骤的 ViewModel(即session.Step1
)。 - 我们检查 session 中是否存在 Step1,如果不存在则创建它。然后我们将 Step1 模型传递到我们的 View 。
- 当用户提交表单时,我们会更新
session.Step1
中的“选定”值。这确保了如果用户导航回/step1,我们会“记住”它们的值。然后,我们为第 2 步构建模型并将其保存在 session 中。 - 当我们导航到/step2 时,我们假设 session 中存在模型(因为它们应该从步骤 1 到达这里),因此我们只需返回
return View(GetWizardSession().Step2);
观点:
步骤 1
@model MvcWizardDemo.Controllers.Step1View
@{
ViewBag.Title = "Step1";
}
<h2>Step1</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Step1View</legend>
<div class="editor-label">
@Html.LabelFor(m => m.PricingSecurityIds)
</div>
<div class="editor-field">
@Html.DropDownListFor(m => m.SelectedPricingSecurityId, Model.PricingSecurityIds)
@Html.ValidationMessageFor(m => m.PricingSecurityIds)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.SomeOtherIds)
</div>
<div class="editor-field">
@Html.DropDownListFor(m => m.SelectedSomeOtherId, Model.SomeOtherIds)
@Html.ValidationMessageFor(m => m.SomeOtherIds)
</div>
<p>
<input type="submit" value="Next" />
</p>
</fieldset>
}
步骤 2
@model MvcWizardDemo.Controllers.Step2View
@{
ViewBag.Title = "Step2";
}
<h2>Step2</h2>
Hi, @Model.Name you selected the following values in the previous step:
<p>
<strong>Security Id:</strong> @Model.PricingSecurityId
</p>
<p>
<strong>Some other Id:</strong> @Model.SomeOtherId
</p>
关于asp.net-mvc - 如何选择下拉列表值并在mvc3中显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10120841/