How can I pass the edited modified object from the view back to the controller without the form?

Short: how does modeling translate objects from view to controller?

Long
First, based on the parameters set by the user through the search form, some objects are retrieved from the database. These objects are provided with metadata that is visible (but not definitive) to the customer (for example: the naming and pricing of objects differs from region to region).
Later in the site, the user can click links that should contain information about these objects. Since this metadata is important for display but not definition, I need to return the previously modified object to the controller. When I use the default asp.net mvc binding the .ToString () method is used. This course does not return the appropriate string to recreate the complete object.
I would assume the ISerializable interface would be involved, but it is not. How can I achieve the desired effect? I can't imagine that I am the first to come across this question, so I must have missed something somewhere ...

0


source to share


2 answers


Model binding by default takes form parameters by name and maps them to properties of the type specified in the argument list. For example, your model has properties "Price" and "Name", then the form should contain inputs with IDs / names "Price" and "Name" (I suspect this is a fuzzy match). The binder uses reflection to convert the shape values ​​associated with these keys to the appropriate type and assigns it to the properties of a newly created object of the type specified by the parameter (again obtained by reflection).

You can actually look (and download) the source for this at http://www.codeplex.com/aspnet , although you'll have to deploy the MVC source from there. I would give a link to the source of the DefaultModelBinder, but the way they were built I believe the link changes with the change.

So, to answer your question, you need to have parameters (can be hidden) in your form that correspond to the properties of the object you want to recreate. When you submit a form (in a view) to a controller, the binder must recreate an object of the specified type using the form parameters. If you need to translate from values ​​in a form parameter to object properties, you probably need to implement your own custom binder.



[EDIT] In response to your second post:

Let's say that we want to return a link to an action that uses a customized object. We can store the configured object in TempData (or session if we need it to continue across more than one postback) with a specific key. We can then build a link to the action and provide the object key as the ActionLink value in the anonymous class. This will return the key as a Request parameter. In our action, we can use the key from this parameter to retrieve the object from the TempData.

<%= Html.ActionLink( ViewData["CustomObject1",
                     "Select",
                     new { TempDataKey = ViewData["CustomObject1_Key"] }
                   ) %>
&nbsp;

public ActionResult Select()
{
    Entity custObj = null;
    string objKey = Request.Params["TempDataKey"];
    if (!string.IsNullOrEmpty(objKey))
    {
       custObj = (Entity)TempData[objKey];
    }

    ... continue processing
}

      

+2


source


@tvanfosson



Thanks for your explanation, but what about links? (no forms) Html.ActionLink (c => c.Action (parameter), "label") currently accepts objects as a parameter. They must be translated into parts of the URL. For this MVC ALWAYS goes to the .ToString () method. I don't want to serialize my object in the ToString method.
Can't I somehow help rationalize the serialization of my object? Tell through the ISerialize interface or something like that?

0


source







All Articles