Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 Partial View and Model not posting back changes

I have a Model than contains a collection of items that can be modified.

I render the collection using a Partial View, which in turn uses EditorForModel to output the HTML for each element in the collection.

@model Footy.Models.EventModel

    <h2>@Model.Team1Name vs @Model.Team2Name</h2>
    @using (Html.BeginForm("Index", "Event"))
    {
        @Html.HiddenFor(m => m.EventID)
        <h1>
            Team 1 Squad</h1>
        @Html.Partial("EventPlayers", Model.Team1Players);
        <h1>
            Team 2 Squad</h1>
   Html.RenderPartial("EventPlayers", Model.Team2Players);

        <input type="submit" value="Confirm Changes" />
    }

Partial View

@model IEnumerable<Footy.Models.PlayerModel>
@Html.EditorForModel()

PlayerModel View

@model Footy.Models.PlayerModel

@Model.PlayerName @Html.DropDownListFor(p => p.ParticipationStatusID, new SelectList(Model.ParticipationTypes, "Key", "Value"))

It all renders correctly, but when the user clicks the input, the controller method is not passed the child collection in the model, e.g. Model.Team1Players is null

What am I missing?

EDIT: Generated HTML is

    <form action="/Footy/Event/Index/1" method="post"><input data-val="true" data-val-number="The field EventID must be a number." data-val-required="The EventID field is required." id="EventID" name="EventID" type="hidden" value="1" />            
<h1>Team 1 Squad</h1>
      si <select data-val="true" data-val-number="The field ParticipationStatusID must be a number." data-val-required="The ParticipationStatusID field is required." name="[0].ParticipationStatusID"><option value="1">Team</option>
    <option value="2">Sub</option>
    <option value="3">Squad</option>
    </select>
    <h1>Team 2 Squad</h1>
 <input type="submit" value="Confirm Changes" />
 </form>

Thanks

I think it is related to this question, which doesn't yet have an answer: Posting data back to a controller from a Partial View rendered by Ajax

like image 710
Duncan Avatar asked Oct 07 '11 12:10

Duncan


People also ask

Can we use model in partial view?

To create a partial view, right click on Shared folder -> select Add -> click on View.. Note: If the partial view will be shared with multiple views, then create it in the Shared folder; otherwise you can create the partial view in the same folder where it is going to be used.

Can you just update a partial view instead of full page post?

Partial views in ASP.NET MVC are great. They allow you to update only a part of the DOM without having to perform a full page refresh or a postback.


1 Answers

If you inspect the rendered source, can you check that the names and id's of the rendered child model inputs correspond to the model hierarchy?

I believe that you will need EditorFor in order for the child models to be properly "namespaced".

So in the EventModel view, use something like this:

@Html.EditorFor(m => m.Team1Players, "EventPlayers")

I am not sure however. I've had similar problems with the MVC framework.

like image 178
Christoffer Avatar answered Oct 18 '22 16:10

Christoffer