模型的编辑视图返回控制器总是空 [英] Model returned to controller by edit view is always null

查看:128
本文介绍了模型的编辑视图返回控制器总是空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使该编辑predefined机型列表的视图。
因此,它是一个强类型的观点,即需要为参数的机型列表。
我使用自定义HTML辅助编辑个别型号。
在获取视图正确显示,但后面的后视图模型(型号列表)总是空。
我知道这里对于这个话题很多问题,但我一直在努力,现在2天做到这一点。

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.

下面是基本型号:

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; }



}

下面是控制器操作:

     //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);
        }
    }

这是一种尝试编辑的列表视图 PrivacyManagerModel

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>

和最终的HTML辅助EditTemplate为 PrivacyManagerModel

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>

同样,GET操作工作正常(它显示了正确的观点),但邮政操作始终接收一个空模型参数。
我将不胜AP preciate你的帮助。
 谢谢

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

推荐答案

您可以试试这个。我有类似的问题,当我工作的MVC 3网站的项目。原因是,MVC平台无法生成模型从数值回来查看,因为当我们使用foreach循环,并在循环中为任何项目创建控制@ Html.DisplayFor(modelItem => item.PrivaceTypeId) 分配到HTML控件的ID /名称将是item.PrivaceTypeId。但是,在分配到HTML控件的ID /名称下面给出的例子是模范[0] .PrivaceTypeId,模型[1] .PrivaceTypeId,等等...,这将有助于创造模型(集合)从视图中的值。

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> 
} 

我不知道,如果MVC平台将能够创建编辑模板的模型,因为我没有VS现在。您可以检查看看。但是,这肯定会工作,因为它已经为我工作4-5倍。

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.

这篇关于模型的编辑视图返回控制器总是空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆