views:

30

answers:

3

Hi

I have the model

public class PersonViewModel
{
    public Guid Id { get; set; }
    public string Firstname { get; set; }
    public string Lastname { get; set; }
}

which is nested in an other view model:

public class ApprovalModel
{
    [UIHint("MyDisplayTemplate")]
    public PersonViewModel User { get; set; }

    [Required]
    public bool? Approve { get; set; }
}

Under Views -> Shared -> DisplayTemplates I have the template MyDisplayTemplate.ascx

In my view ApprovalModel view I use the following line, which displays the nested model:

<div class="display-field"> <%: Html.DisplayFor(model => model.User) %> </div>

And in my controller I have the action

[HttpPost]
public virtual ActionResult ApproveRequest(ApprovalModel vm)
{
    //access bound vm.User  here
}

Is there a simple way to bind the nested model back with the post request? Or what else can I do to bind it back?

thanks

+1  A: 

You need input fields in the display template:

<%: Html.HiddenFor(x => x.Id) %>
<%: Html.LabelFor(x => x.Firstname) %>
<%: Html.TextBoxFor(x => x.Firstname) %>
<br/>
<%: Html.LabelFor(x => x.Lastname) %>
<%: Html.TextBoxFor(x => x.Lastname) %>

And in your controller action indicate the User prefix so that the model binder is capable of correctly recognizing the request values and binding them to ApprovalModel.

[HttpPost]
public ActionResult ApproveRequest([Bind(Prefix = "User")] ApprovalModel vm)

Also editor templates seems more appropriate (<%: Html.EditorFor(model => model.User) %>) for generating forms and input fields instead of display templates.

Darin Dimitrov
but I don't want to edit the fields. I want to display them and reuse them on the post action
Fabiano
Without input fields you won't get the values back in the controller action. When you use `ApprovalModel` as action parameter it will look for those values passed in the request. You could also include them as hidden fields in your form if you don't want those input fields to be visible but the values need to be sent to the server somehow.
Darin Dimitrov
+1  A: 

Save your view model in Session and render the id as a hidden field. Then your post action can retrieve the model from Session or load it by the id if it isn't there.

View:

<%: Html.HiddenFor(x => x.Id) %>

Controller:

[HttpPost]
public virtual ActionResult ApproveRequest(int id)
{
    //access bound vm.User  here
}
Thomas Eyde
+1  A: 

You don't need to put the fileds in textboxes you just need to put it in some input fields, so when the page is posted back those values are sent back to the server. Therefore simply use HiddenFor(....) for each field you want to recovery on post back

Francesco Abbruzzese