mvc 3 嵌套对象绑定
我对 MVC 3 很陌生,所以这可能很简单。我有一个带有这样的嵌套对象的视图模型:
public class EventViewModel
{
public Event Event { get; set; }
}
public class Event
{
[Required]
public int Id { get; set; }
public string Title { get; set; }
}
在我的“创建”视图中,我做了这样的事情:
@model EventViewModel
@Html.EditorFor(model => model.Event.Title)
这是我的事件控制器的代码:
public class EventController : Controller
{
[HttpPost]
public ActionResult Create(EventViewModel @event)
{
...
}
}
该编辑器位于表单标签内。当我回发到控制器时,事件的标题为空,而不是我在表单中输入的内容。我需要某种定制活页夹吗?当我在视图模型中使用嵌套对象时,我是否在做一些非常规的事情?
I am very new to MVC 3 so this could be an easy one. I have a viewmodel with a nested object like this:
public class EventViewModel
{
public Event Event { get; set; }
}
public class Event
{
[Required]
public int Id { get; set; }
public string Title { get; set; }
}
In my 'create' view I do something like this:
@model EventViewModel
@Html.EditorFor(model => model.Event.Title)
Here's the code form my event controller:
public class EventController : Controller
{
[HttpPost]
public ActionResult Create(EventViewModel @event)
{
...
}
}
This editor is inside of form tag. When i postback to my controller the title of the event is null instead of what i entered in the form. Do I need some kind of custom binder? Am I doing something unconventional when I use nested objects in my viewmodel?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是你的参数的名称。将其从 @event 更改为 eventViewModel。似乎模型绑定器不能这样绑定它。它可能会与模型上的 Event 属性混淆。
编辑:一些更粗略的解释。
HtmlHelper 创建一个名为 Event.Title 的表单输入,当您在帖子上进行绑定时,绑定器将首先尝试将 Event.Title 绑定到名为 event 的参数和名为 Title 的属性。由于 EventViewModel 上没有此类属性,因此该属性将为 null。您可以通过将参数类型从 EventViewModel 更改为 Event(它有一个名为 Title 的属性)来进行测试,在这种情况下,绑定将不为 null。
相反,如果参数被命名为其他名称,它将尝试通过首先查找名为“Event”的属性,然后查找“Title”来将其绑定到该参数。
It's the name of your parameter. Change it from @event to eventViewModel. Seems like the model binder cannot bind it like that. Possibly it gets mixed up with the Event property on your model.
Edit: Some more rough explanation.
The HtmlHelper creates a form input with the name Event.Title and when you are doing the binding on the post, the binder will first try to bind Event.Title to the parameter named event, to the property called Title. Since there is no such property on the EventViewModel this will be null. You can see test this by changing the parameter type from EventViewModel to Event (which has a property called Title), in which case the binding will not be null.
Instead if the parameter is named something else, it will try to bind it to that parameter by looking first for a property called Event, and then Title instead.
将嵌套类的名称更改为 TheEvent 以某种方式解决了问题...这就是我的视图模型现在的样子..
Changing the name of my nested class to TheEvent fixed the problem somehow...this is what my viewmodel looks like now..
哦,对了。当您第一次请求该页面时,即您的默认操作,您需要实例化 ViewModel 并将其传递给视图。
让我知道上面那行。
Oh right. When you request the page for the first time, i.e. your default action, you need to instantiate the ViewModel and pass that to the view.
Let me know about the line above.