ASP.NET MVC - Automatically getting entity object when form is posted

Posts   
 
    
Posts: 1
Joined: 05-Dec-2008
# Posted on: 05-Dec-2008 23:05:11   

Hi,

I watched Phil Haacks PDC event - http://mschnlnine.vo.llnwd.net/d1/pdc08/WMV-HQ/PC21.wmv In this, he is using entity framework.

I have started to play with ASP.NET MVC, trying to create the same using LLBLGen instead of using Entity Framework or Linq.

When a View contains the fields with same names as fields od the entity, on postback the function magically gets the entity object with all the fields filled in.

View Code

<% using (Html.BeginForm()) { %>

<%= Html.TextBox("Question.Title")%>
<br />
<%= Html.TextBox("Question.Body")%>
<br />
<input type="submit" value="Submit" />

<% } %>

Controller Code

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Ask(QuestionEntity question)
{
    return View();
}

This magic as I understand is done using reflection. But in case of LLBLGen is am getting the question paramater as null.

Any ideas???

Thanks!

Seth avatar
Seth
User
Posts: 204
Joined: 25-Mar-2006
# Posted on: 06-Dec-2008 03:39:08   

Nothing is as magical as that! You have to create a custom model binder in order to get that to work properly. See this. I suspect it is because the entity objects are a bit more complicated than simple POCO objects.

--Edit So I remember building a generic model binder (before there were model binders) that did use reflection. See it here. It works well if you are careful to name your form elements properly and also if the objects are simple.