gpt4 book ai didi

c# - Asp.Net MVC 4 - 绑定(bind)到在 POST 中不起作用的复杂对象

转载 作者:太空宇宙 更新时间:2023-11-03 13:28:09 25 4
gpt4 key购买 nike

我有一个如下所示的 View 模型:

 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绑定(bind)一个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>

当我将此表单发布到服务器时,当操作尝试绑定(bind)时,EventVm.EventLayout 属性为 null。但是我可以看到在表单数据中发布了一个 EventLayout 实例:

enter image description here

我的 Action 是这样的:

 [HttpPost]
public ActionResult Update(EventVm eventVm)
{
_eventService.UpdateEvent(eventVm);
return RedirectToAction("Index", new { groupId = eventVm.GroupId });
}

有人可以告诉我我做错了什么吗?

最佳答案

自定义绑定(bind)解决了这个问题。谢谢大家的有用评论。

Brad Christie 指出,因为我的操作试图绑定(bind)到的对象只有一个私有(private)构造函数,所以需要自定义绑定(bind)。

自定义绑定(bind):

public class EventVmBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{

HttpRequestBase request = controllerContext.HttpContext.Request;

var eventVm = new EventVm()
{
Id = Int32.Parse(request.Form.Get("Id")),
GroupId = Int32.Parse(request.Form.Get("GroupId")),
Title = request.Form.Get("Title"),
HeaderMarkup = request.Form.Get("HeaderMarkup"),
LeftNavigationMarkup = request.Form.Get("LeftNavigationMarkup"),
CenterContentMarkup = request.Form.Get("CenterContentMarkup"),
RightNavigationMarkup = request.Form.Get("RightNavigationMarkup"),
EventLayout = EventLayout.LayoutFromLocalString(request.Form.Get("EventLayout")),
DisplayOrder = Int32.Parse(request.Form.Get("DisplayOrder")),
Active = request.Form.Get("Active").As<bool>(),
CanEdit = request.Form.Get("CanEdit").As<bool>()
};

return eventVm;
}

}

在 Action 中连接它:

 [HttpPost]
public ActionResult Create([ModelBinder(typeof(EventVmBinder))]EventVm eventVm)
{
_groupService.AddEventToGroup(eventVm);
return RedirectToAction("Index", new {groupId = eventVm.GroupId});
}

关于c# - Asp.Net MVC 4 - 绑定(bind)到在 POST 中不起作用的复杂对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21586927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com