2012-07-05 37 views
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> 

+2

爲什麼在視圖模型中沒有字符串「SelectedCustomer」和「SelectedJob」?然後將您的視圖重寫爲Html.DropDownListFor(X => X.SelectedCustomer ...)和SelectedJob相同。 – 2012-07-05 10:54:28

+0

@Alexey:我不明白謝謝... – programmerist 2012-07-05 11:01:36

回答

1

你的模型應該是這樣的

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; } 

    public CustomerModel Customer {get; set;} 
} 

視圖中的代碼添加下拉應該是這樣的

<%= Html.DropDownListFor(X=>X.Customer,Model.Customers) %> 

希望這幫助。

相關問題