ASP .Net MVC 3 Html.DropDown没有绑定到变量

时间:2012-04-02 19:09:56

标签: asp.net-mvc drop-down-menu

我有一个组织列表(CalledOrganizationSelectList),其中一个是被调用的组件(CalledOrganizationId)。我在我的ViewModel中设置了两个,但MVC / Razor没有正确呈现下拉列表(即没有为下拉列表中的正确项目设置selected =“selected”属性。)

有一种解决方法更具有意义。我在ViewModel中添加了一个新成员,并将下拉列表绑定到该成员。这很好用。刚才它停止工作......

public class CallViewModel{
public int? CalledOrganizationId { get; set; }        
public SelectList CalledOrganizationSelectList { get; set; }}

在我的控制器中:

var vm = new CallViewModel();
var calledOrganizationSelectList = new List<object>();
calledOrganizationSelectList.Add( new {Text="",Id=""});
calledOrganizationSelectList.AddRange(
db.MyOrganizations.OrderBy(x=>x.Name)
  .Select(x => new { Text = x.Name, Id =     x.Id.ToString()}).ToList());
var sl = new SelectList(calledOrganizationSelectList, "Id", "Text",
  vm.CalledOrganizationId);
vm.CalledOrganizationSelectList = sl;
return View(vm);

在我看来:

<div>CalledOrganizationId = @Model.CalledOrganizationId : 
select list contains
<ul>@foreach (var itm in Model.CalledOrganizationSelectList)
  {<li>Value: @itm.Value Text: @itm.Text Is Selected: @itm.Selected</li>}
</ul>
</div>
@Html.DropDownList("CalledOrganizationId", Model.CalledOrganizationSelectList)

在我呈现的页面源中:

<div>CalledOrganizationId = 38 : select list contains     
<ul>
<li>Value:  Text:  Is Selected: False</li>
<li>Value: 37 Text: rrr Is Selected: False</li>
<li>Value: 38 Text: sss1 Is Selected: True</li>
</ul>
</div>
<select data-val="true" data-val-number="The field CalledOrganizationId must be a number." id="CalledOrganizationId" name="CalledOrganizationId">
<option selected="selected" value=""></option> 
<option value="37">rrr</option> 
<option value="38">sss1</option> </select>

我先解决了我的头发留下的东西,然后在我的ViewModel上引入了一个新的变量,这似乎工作正常。

1 个答案:

答案 0 :(得分:3)

在viewmodel上有2个属性是执行DropDownLists的正确方法。一个属性包含所有可用选项,另一个属性包含当前选择的选项:

public class CallViewModel
{
    // this captures the selected item
    public int? CalledOrganizationId { get; set; }

    // this contains the select list options     
    public IEnumerable<SelectListItem> CalledOrganizationSelectList { get; set; }
}

但是,您无需在控制器中添加空选项。您可以在视图中执行此操作:

@Html.DropDownListFor(m => m.CalledOrganizationId, 
    Model.CalledOrganizationSelectList, "")

你可以在控制器中侥幸逃脱:

var vm = new CallViewModel();
//var calledOrganizationSelectList = new List<object>();
//calledOrganizationSelectList.Add( new {Text="",Id=""});
//calledOrganizationSelectList.AddRange(
//db.MyOrganizations.OrderBy(x=>x.Name)
//  .Select(x => new { Text = x.Name, Id =     x.Id.ToString()}).ToList());
//var sl = new SelectList(calledOrganizationSelectList, "Id", "Text",
//  vm.CalledOrganizationId);
//vm.CalledOrganizationSelectList = sl;
vm.CalledOrganizationSelectList = db.MyOrganizations.OrderBy(x => x.Name)
    .Select(x => new SelectListItem 
    {
        Text = x.Name, 
        //Id = x.Id.ToString()  // do not use "Id" for the property name,
        Value = x.Id.ToString() // use "Value" -- it is a SelectListItem property
    });

// if you want to set the selected item on the dropdownlist, do it here
vm.CalledOrganizationId = 37;

return View(vm);