我有一个看起来像这样的视图模型:
public class EventVm
{
public int Id { get; set; }
public int GroupId { get; set; }
public string Title { get; set; }
public EventLayout EventLayout { get; set; }
}
EventLayout 是一个自定义对象,如下所示:
public class EventLayout
{
private const string SingleColumnLayoutLocalKey = "MyOrg.SingleColumnLayout";
...
//Removed for breviety
public static EventLayout SingleColumnLayout = new EventLayout(SingleColumnLayoutLocalKey);
...
//Removed for breviety
public string Value
{
get { return _layoutLocalKey; }
}
private readonly string _layoutLocalKey;
private EventLayout(string layoutLocalKey)
{
_layoutLocalKey = layoutLocalKey;
}
public static EventLayout LayoutFromLocalString(string localString)
{
...
}
public override string ToString()
{
return _layoutLocalKey;
}
public override bool Equals(object obj)
{
if (obj.GetType() != this.GetType())
{
return false;
}
if (this._layoutLocalKey == obj.ToString())
{
return true;
}
return false;
}
public override int GetHashCode()
{
return this._layoutLocalKey.GetHashCode();
}
}
基本上 EventLayout 只是一个提供字符串后备存储的自定义枚举。EventLayout 绑定到一个 Select 表单控件(T() 只是一个本地化扩展方法):
<select id="eventLayoutSelect" name="EventVm.EventLayout">
@foreach (var option in Model.EventLayoutOptions)
{
<option value="@option.Value" @((Model.EventLayout != null && Model.EventLayout.Equals(option))
? "selected=selected"
: string.Empty)>@T(option.Value)</option>
}
</select>
当我将此表单发布到服务器时,当操作尝试绑定时,EventVm.EventLayout 属性为空。但是我可以看到在表单数据中发布了一个 EventLayout 实例:
我的动作是这样的:
[HttpPost]
public ActionResult Update(EventVm eventVm)
{
_eventService.UpdateEvent(eventVm);
return RedirectToAction("Index", new { groupId = eventVm.GroupId });
}
有人可以告诉我我做错了什么吗?