I am writing a survey app in MVC3.
I have this class (simplified):
public class Survey
{
public Respondent Respondent { get; set; }
}
And in my View:
@model Survey
// bla bla bla
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
@Html.Partial("_Respondent", Model.Respondent)
}
When I post it back, the survey.Respondent = null 🙁
[HttpPost]
public ActionResult Survey(Survey survey)
{
return RedirectToAction("Index");
}
Simplified code for _Respondednt partial view:
@model Mercer.FITT.Respondent
<fieldset>
<legend>Respondent Information</legend>
<table>
<tr>
<td>
@Html.LabelFor(model => model.Name)
</td>
<td>
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(model => model.JobTitle)
</td>
<td>
@Html.EditorFor(model => model.JobTitle)
@Html.ValidationMessageFor(model => model.JobTitle)
</td>
</tr>
</table>
</fieldset>
If I get rid of partial view and just copy partial view content into the main view, everything’s fine. Any idea why data is not collected from the partial view?
Maybe I should call the Partial View somehow differently?
Thanks a lot.
Use EditorTemplate instead of partial view
and don’t forget to create
Respondent.cshtmlfile in theEditorTemplatesfolder and put in it the Respondent fields to edit.