Cantinos
Cantinos

Reputation: 282

Form mvc3 razor partialview post

I have a partialView _BasicInfoPartView associate to BasicInfoModel. This partialview contains some input and dropdownlist as:

public class BasicInfoModel 
{       public string Name { get; set; }
        public string selectedRubric { get; set; }
        public IEnumerable<SelectListItem> Rubrics { get; set; }
}

Partial _BasicInfoPartView.cshtml

@model ProjectZeroWebSite.Models.PleGroupModel
@Html.ListBoxFor(m => m.selectedRubric, @Model.Rubrics })
@Html.TextBoxFor(m => m.Name )

This partialview can be call in a FORM in any page. Just need to add BasicInfoModel in the page model.

public class RandomPageModel
{
        public basicInfoModel { get; set; }
        public string info2{ get; set; }
        ...
        public RandomPageModel()
        { this.basicInfoModel = new basicInfoModel ();}
}

RandomPage.cshtml

@using (Html.BeginForm())
{
    @Html.Partial("_BasicInfoPartView ", @Model.BasicInfoModel)
    @Html.TextBoxFor(m => m.info2)
    ...
    <input type="submit"  />
}

Now the problem: I can populate the page without problem. But when I try to retrieve the information in the controller from the model: RandomPageModel.info2 OK! RandomPageModel.BasicInfoModel is empty...

I think i miss understand the databinding concept :s I try this because i did not want overload my pages by small JS functions and dont like the idea t copy past my forms (code maintenance).

Thanks by advance.

Cantinos.

Upvotes: 1

Views: 495

Answers (1)

Forty-Two
Forty-Two

Reputation: 7605

You need to use an editor template instead of a partial view in this case.

So you define an editorTemplate (strongly typed)

@model BasicInfoModel

@Html.ListBoxFor(m => m.selectedRubric, @Model.Rubrics })
@Html.TextBoxFor(m => m.Name )

Name this editor template the same as the model and put it in a folder called EditorTemplates in the Shared view folder

~/Views/Shared/EditorTemplates/BasicInfoModel.cshtml

And then swap out this line

@Html.Partial("_BasicInfoPartView ", @Model.BasicInfoModel)

for this one

@Html.EditorFor(m => m.BasicInfoModel)

Upvotes: 4

Related Questions