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-of-a-selectlist-to-the-controller-using-a-view-mo

视图模型:


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

4

1 回答 1

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) %>

希望这可以帮助。

于 2012-07-05T11:19:28.113 回答