English 中文(简体)
Post back complex object from client side
原标题:

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.

最佳回答

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.

问题回答

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.

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.





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

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 ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签