[RESOLVED]Partial View Model

Hi,

i am new to MVC.

i am using partial view. i am binding the List of object to partial view.

when i click on submit button,i am not able to get list of object values in controller action method.

Am getting null object. i need list of value what i entered in text box using partial view.

my model,

public
class
Stud   
{

       public
string
Name
     {  
get
;
set;    
}

       public
string
Ans   
{   set; get;  
}

    }

my View

  @for
(
int
i = 0; i < Model.Count; i++)
    {

<h3
id="h_@i"><a
href="#">@Model[i].Name</a></h3>

<div
id="div_@i">

       @Html.Partial("partial",
Model[i])

</div>

}

my partial view

<table><tr><td>

                @Html.LabelFor(x=>Model.Name,Model.Name)

                @Html.TextBoxFor(x => Model.Ans)

          </td></tr></table>

My
Action Method

[HttpPost]

public
ActionResult
Sample(
List<Stud>
obj,
FormCollection
frm)

 {  return
View();
    }

 

Here obj is null..but i need list of values what i have entered in the Textbox.

if i use without partial view ,am getting list of values.

 

please help on this.

thanks,

Sundar.

Model binding works with naming convention. As you are using partial class with a list, you need to follow the naming conventions manually. Please see this blog

Model Binding To A List

Alternatively try moving the for loop inside the partial class

Hi prasanna,

i tried with moving for loop inside the partial view.

but no luck..

 help me on this..

 

Thanks,

Sundar.

Hi,

You pass the same parent view model to partial view and try.

I has the similar issue before i solved it using this.

Try it like this -

my View

  @for (int i = 0; i < Model.Count; i++)    {

<h3 id="h_@i"><a href="#">@Model[i].Name</a></h3>

<div id="div_@i">

       @Html.Partial("partial", Model[i])   --->  change it to @Html.Partial("partial", Model)

</div>

}

and make necessary changes in partial view since you change the model.

The magic of MVC and strongly typed models comes into play here and it works perfectly if and only if…. you set the view binding correctly.

Web browsers only deal with query strings.  So how does MVC turn name/value pairs into concrete class types?  It works like this.  On a post back, MVC will initialize a new object of the controller strong typed parm.  This is even before the controller gets
control, in addition all of the validation work is done too.  Then the razor binding engine will take the names of the properties from the views and their contents and fill in the properties automatically.  If there are any missing names, then it’s most likely
a view issue as follows:

On post backs every property that you want to persist on the post, must be included in the Begin form logic.  In addition if you use the HTML helpers with the FOR in their name, you learn quickly that the very first parm in all those Helper methods is to
identify the property NAME.  Some have a 2nd property to allow further binding to lists<ofType>…   So, to make this work the way you want, ensure that the Begin form has the names of all the properties as well as the values being returned.  Then and only
then will MVC be able to fill in the blanks.

Leave a Reply