0
嗨;我有2個下拉列表。一個是客戶ddl另一個是工作。我想在任何選擇過程後保持選定的值。但是我不能。 我想保持ddl'selected項目的選定值點擊按鈕後。 (我已經使用了2種方法:dropdownlist和dropdownlistfor。)我最喜歡的文章是http://stackoverflow.com/questions/6981698/how-to-keep-dropdownlist-selected-value-after-postback 和 http:///stackoverflow.com/questions/4950798/how-to-post-the-selected-value-of-a-selectlist-to-the-controller-using-a-view-mo 如何在asp.net mvc 3中點擊submitbutton後保持選定的值?
視圖模型:
public class MyViewModel
{
public IEnumerable<string> Columns { get; set; }
public IEnumerable<dynamic> Values { get; set; }
public bool HasNext { get; set; }
public bool HasPrevious { get; set; }
public IEnumerable<CustomerModel> Customers { get; set; }
public IEnumerable<SelectListItem> Jobs { get; set; }
}
Cotroller:
public class JobController : Controller
{
public ActionResult Index(int? page)
{
var model = new MyViewModel();
model.Customers = CustomerOperation.GetCustomers().Items;
ViewData["Jobs"] = new SelectList(JobOperation.GetCustomersAssemblyList().Items, "scheduleId", "name", null);
return View(model);
}
查看:
<table style="padding:25px; margin:10px 10px 10px 10px; width:800px" id="sample">
<tr>
<td>Customer Name: </td>
<td>
<%= Html.DropDownListFor(X=>X.Customers,new SelectList(Model.Customers,"Id","Name"),"** Please Select **", new { id = "ddlCustomers" })%>
</td>
<td>Job Name:</td>
<td>
<%= Html.DropDownList("Jobs", (IEnumerable<SelectListItem>)ViewData["Jobs"], "** Please Select **", new { id = "ddlJobs", disabled = "disabled" })%>
</td>
<td>
<input value="monitor" name="submitButton" type="submit" />
</td>
</tr>
</table>
爲什麼在視圖模型中沒有字符串「SelectedCustomer」和「SelectedJob」?然後將您的視圖重寫爲Html.DropDownListFor(X => X.SelectedCustomer ...)和SelectedJob相同。 – 2012-07-05 10:54:28
@Alexey:我不明白謝謝... – programmerist 2012-07-05 11:01:36