0

I'm using ASP.NET MVC and Entity Framework. I'm going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don't know how to do that and whether the client side entity will lost relationship tracking of EF as it is detached from object context.

1
  • Just in case you start manipulating Javascript objects and wanted to send them to server, there's a simple jQuery plugin that does it (also able to post dates etc.). So sending complex Javascript objects to controller actions: erraticdev.blogspot.com/2010/12/… Apr 22, 2011 at 7:55

3 Answers 3

2

ASP.NET MVC is capable of Model Binding complex objects and it's quite good at it. An easy way to do this is to name your view fields that same as the properties for your object. That way in your action method that the form posts to you only need the complex object as a parameter. Example:

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

    <%= Html.TextBox("Property1") %>
    <%= Html.TextBox("Property2") %>
    <%= Html.TextBox("Property3") %>
    <%= Html.TextBox("Property4") %>
    <%= Html.CheckBox("PropertyBool") %>
    <%= Html.TextBox("Property5") %>

<% } %>

Which posts to an action method like so:

public ActionResult Index(ComplexObject complexoObject)
{

}

That's a fairly simple example as you could have different form controls in the view for corresponding properties of the object. If your object is very complex you could always write your own model binder for it and over-ride the default model binder.

3
  • Yeah, that's true. But what I'm posting back is a big questionaire which contains more than 100 questions. they are contained in a list. How can I post that back? Thanks!
    – Roy
    Nov 13, 2009 at 8:20
  • 2
    marcomagdy.com/2009/09/03/…
    – queen3
    Nov 13, 2009 at 13:20
  • Check out queen3's link and take a look at "Binding to Collections of Simple Types" & "Binding to Collections of Custom Types". If you name your form elements accordingly you can accept a list as a parameter and MVC will do the model binding to that list automatically.
    – DM.
    Nov 13, 2009 at 13:44
0

The entity will be detached. You will need to fetch the entity again, update the properties, and then commit the changes. Alternatively you could reattach the entity, but this can be tricky when you are dealing with multiple related entities being attached at the same time. If this is the case for you, check out http://www.codeproject.com/KB/architecture/attachobjectgraph.aspx.

-2

You are saying that you display a list of 100 questions on one single page? I hope you are not doing that. First of all, this is not very user friendly. It involves a lot of scrolling down, user can get lost, its error prone and if something goes wrong he has to do it all over again.

A better approach is one or few questions at a time. With Next/Prev buttons for moving between them.

You could make it even easier for yourself and forget about postbacks and go the AJAX way - issue a post request to some Json service the AJAX way. And save the progress after each question, so if something goes wrong user can resume later from where he left off. It will be much more maintable, faster and user friendlier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.