简体   繁体   English

mvc 3嵌套对象绑定

[英]mvc 3 nested objects binding

I am very new to MVC 3 so this could be an easy one.我对 MVC 3 很陌生,所以这可能很容易。 I have a viewmodel with a nested object like this:我有一个带有嵌套 object 的视图模型,如下所示:

 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:这是我的活动 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.当我回发到我的 controller 时,事件的标题是 null 而不是我在表格中输入的内容。 Do I need some kind of custom binder?我需要某种定制活页夹吗? Am I doing something unconventional when I use nested objects in my viewmodel?当我在视图模型中使用嵌套对象时,我是否在做一些非常规的事情?

It's the name of your parameter.这是您的参数的名称。 Change it from @event to eventViewModel.将其从 @event 更改为 eventViewModel。 Seems like the model binder cannot bind it like that.似乎 model 活页夹无法像那样绑定它。 Possibly it gets mixed up with the Event property on your model.它可能与 model 上的 Event 属性混淆。

public ActionResult Create(EventViewModel eventViewModel) {..}

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. HtmlHelper 创建一个名为 Event.Title 的表单输入,当您对帖子进行绑定时,绑定程序将首先尝试将 Event.Title 绑定到名为 event 的参数,绑定到名为 Title 的属性。 Since there is no such property on the EventViewModel this will be null.由于 EventViewModel 上没有这样的属性,因此它将是 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.您可以通过将参数类型从 EventViewModel 更改为 Event(它有一个名为 Title 的属性)来测试这一点,在这种情况下,绑定不会是 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.相反,如果参数被命名为其他名称,它将尝试通过首先查找名为 Event 的属性,然后查找 Title 来将其绑定到该参数。

Changing the name of my nested class to TheEvent fixed the problem somehow...this is what my viewmodel looks like now..将我的嵌套 class 的名称更改为 TheEvent 以某种方式解决了问题......这就是我的视图模型现在的样子......

public class EventViewModel
{     
    public Event TheEvent { get; set; }
}

Oh right.啊对。 When you request the page for the first time, ie your default action, you need to instantiate the ViewModel and pass that to the view.当您第一次请求页面时,即您的默认操作,您需要实例化 ViewModel 并将其传递给视图。

public ActionResult DefaultActionForEventView()
{
    var eventViewModel = new EventViewModel();
    eventViewModel.Event = new Event() // I can't remember if you need this line
    return View(eventViewModel) 
}

Let me know about the line above.让我知道上面的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM