I am trying to make a view that edits a list of predefined models. Therefore it is a strongly typed view that takes as parameter a list of models . I use custom Html helpers to edit the individual models. The Get view is displayed properly but the post back view model (the list of models) is always null. I know there are many questions on here about this topic but I ve been trying to do this for 2 days now.
Here is the base Model:
public class PrivacyManagerModel
{
[Required]
[Display(Name="Privacy Type Id")]
public int PrivaceTypeId { get; set; }
[Required]
[Display(Name = "Visibility Level Id")]
public int VisibilityLevelId { get; set; }
}
Here are the Controller Actions:
//GET: /Profile/ManagePrivacy
[Authorize]
public ActionResult ManagePrivacy()
{
PrivacyTypeService _privacyTypeService=new PrivacyTypeService();
IEnumerable<PrivacyFlagType> privacyTypes = _privacyTypeService.GetPrivacyFlagTypes();
List<PrivacyManagerModel> model=new List<PrivacyManagerModel>();
foreach (PrivacyFlagType type in privacyTypes)
{
PrivacyManagerModel temp=new PrivacyManagerModel();
temp.PrivaceTypeId=type.PrivacyFlagTypeId;
model.Add(temp);
}
ViewBag.privacyTypes=privacyTypes;
return View(model);
}
//POST: /Profile/ManagePrivacy
[Authorize]
[HttpPost]
public ActionResult ManagePrivacy(IEnumerable<PrivacyManagerModel> model)
{
if (ModelState.IsValid)
{
do stuff
}
else
{
return View(model);
}
}
This is the view that tries to edit the List of PrivacyManagerModel
:
@model IEnumerable<Klever.PrivacyManagerModel>
@using Klever
@{
ViewBag.Title = "ManagePrivacy";
var _privacyTypes = ViewBag.privacyTypes as IEnumerable<PrivacyFlagType>;
}
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
@foreach(PrivacyManagerModel item in Model)
{
<div class="display-label">
@Html.DisplayFor(modelItem=>item.PrivaceTypeId)
</div>
<div class="editor-field">
@Html.EditorFor(modelItem=>item)
</div>
}
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
and finally the Html helper EditTemplate for PrivacyManagerModel
:
@model Klever.PrivacyManagerModel
@using Klever.Components
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@{
PrivacyTypeService _privacyService = new PrivacyTypeService();
var visibilityLevels=_privacyService.GetVisibilityLevels();
}
<fieldset>
<div class="editor-label">
@Html.LabelFor(model => model.PrivaceTypeId)
</div>
<div class="editor-field">
@Html.DropDownListFor(model=>model.VisibilityLevelId,new SelectList(visibilityLevels,"VisibilityLevelId","Name"))
@Html.ValidationMessageFor(model => model.VisibilityLevelId)
</div>
</fieldset>
Again, the GET action works fine ( It shows the view properly) but the Post action always receives a Null model as parameter. I would greatly appreciate your help. Thanks