编辑记录时ASP.Net MVC下拉选择值

时间:2011-06-10 19:02:19

标签: asp.net asp.net-mvc-3

我一直在处理我能够填充的下拉列表,但如果我回来编辑记录,则下拉列表没有显示所选值......

在控制器中,

        var datacontext = new ServicesDataContext();
        var serviceToUpdate = datacontext.Mapings.First(m => m.CustomerServiceMappingID == id);
        var qw = (from m in datacontext.Mapings
                  where id == m.CustomerServiceMappingID
                  select m.CustomerID).First();
        ViewData["CustomerID"] = qw;

        var a = datacontext.services.Select(arg => arg.ServiceID).ToList();
        ViewData["ServiceID"] = new SelectList(a,serviceToUpdate.ServiceID);

在视图中:

    @Html.DropDownListFor(model => model.ServiceID, ViewData["ServiceID"] as SelectList)

serviceToUpdate,ServiceID具有正确的值但是当我尝试编辑所选值时不会以某种方式返回...而是仅返回下拉列表的第一个值...

然而,在控制器中,我又做了一件事,我做了类似的事情,

      ViewData["ServiceID"] = new  SelectList(a,"ServiceID","ServiceID");

在这里,我收到一条错误

           'System.Int32' does not contain a property with the name 'ServiceID'.

1 个答案:

答案 0 :(得分:0)

更改时会发生什么

ViewData["ServiceID"] = new  SelectList(a,"ServiceID","ServiceID");

ViewData["SelectListServiceID"] = new  SelectList(a,"ServiceID","ServiceID");

并在您看来:

@Html.DropDownListFor(model => model.ServiceID, ViewData["SelectListServiceID"] as SelectList)

我以前见过这个,它与自动绑定有关。您的模型中可能有一个名为ServiceID的属性。

相关问题