ASP.MVC3中的下拉列表存在一个奇怪的问题。 我有一个看起来像这样的视图模型:
public class dto{
[Key]
public Int32 ClientID { get; set; }
[Required, MaxLength(50)]
public Name{get;set;}
public int ClientTypeID { get; set; }
public SelectList ClientTypes { get; set; }
}
Razor使用:
<div class="editor-label">
@Html.LabelFor(model => model.ClientTypeID)
</div>
<div class="editor-field">
@Html.DropDownListFor(x => x.ClientTypeID,Model.ClientTypes)
</div>
在控制器中,我用数据填充ClientTypes。 当我使用它来创建一个新的dto对象时,它工作正常,并将选择转移回控制器并保存。 当我使用它编辑现有对象时,它也可以正常工作,当页面打开时选择正确的值。
问题是,当我更改选择时,它不会回发到控制器,而其他编辑的值会正确返回。
有人能指出我的方向吗?
谢谢你。答案 0 :(得分:0)
所以,在我的代码中我做了类似的事情 - 在ViewModel中我有一个IEnumerable:
public IEnumerable<Choice> Choices { get; set; }
将在控制器操作中填充。
和Choice
看起来像
public class Choice
{
public int Id { get; set; }
public string Text { get; set; }
}
在我的ViewModel中我也有一个看起来像这样的成员:
public int TheChoice { get; set; }
然后在我的视图中我有一些看起来像这样的东西:
@Html.DropDownListFor(model => model.TheChoice, new SelectList(Model.Choices, "Id", "Text", @Model.TheChoice))
这看起来效果很好。
你做的不同吗?