Model returned to controller by edit view is alway

2019-08-24 08:32发布

问题:

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

回答1:

You can try this. I had similar problem when I was working on MVC 3 site in a project. The reason being that MVC platform is not able to generate the model back from the values in the View, because when we apply foreach loop and create control for any item in the loop as "@Html.DisplayFor(modelItem=>item.PrivaceTypeId)" the id/name assigned to the HTML control will be "item.PrivaceTypeId". But in the example given below the id/name assigned to HTML control would be "Model[0].PrivaceTypeId", "Model[1].PrivaceTypeId", and so on... and this would help to create Model (collection) from the values in the view.

@using (Html.BeginForm()) { 
@Html.ValidationSummary(true) 
<fieldset> 

    @for(int i = 0; i <= Model.Count; i++)
    { 
        <div class="display-label"> 
        @Html.DisplayFor(modelItem=>Model[i].PrivaceTypeId) 
        </div>  
        <div class="editor-field"> 
        @Html.EditorFor(modelItem=>Model[i]) 
        </div> 
    } 

    <p> 
        <input type="submit" value="Create" /> 
    </p> 
</fieldset> 
} 

I am not sure if the MVC platform will be able to create the model from editor template because I don't have VS now. You can check and see. But this will surely work as it has worked for me 4-5 times.