提交后,模型属性为null

时间:2015-10-19 12:36:42

标签: asp.net-mvc-4

我有这个型号:

 public partial class Group
    {
        public Group()
        {
            this.ParameterGroup = new HashSet<ParameterGroup>();
        }

        public string GroupId { get; set; }
        public string Responsibility { get; set; }

        public virtual Text GroupDescText { get; set; }
        public virtual Text GroupNameText { get; set; }
        public virtual ICollection<ParameterGroup> ParameterGroup { get; set; }
    }

public partial class Text
    {
        public Text()
        {
            this.ParamName = new HashSet<Parameter>();
            this.ParamDesc = new HashSet<Parameter>();
            this.EnumElemName = new HashSet<EnumElem>();
            this.IoDeviceInfoText = new HashSet<IoDeviceInfo>();
            this.IoCatText = new HashSet<IoDeviceInfo>();
            this.GroupDesc = new HashSet<Group>();
            this.GroupName = new HashSet<Group>();
            this.Type = new HashSet<Type>();
            this.ParamDispPath = new HashSet<Parameter>();
            this.EnumElemText = new HashSet<EnumElem>();
            this.TextValue = new HashSet<TextValue>();
        }

        public string TextId { get; set; }
        public string XmlId { get; set; }

        public virtual ICollection<Parameter> ParamName { get; set; }
        public virtual ICollection<Parameter> ParamDesc { get; set; }
        public virtual ICollection<EnumElem> EnumElemName { get; set; }
        public virtual ICollection<IoDeviceInfo> IoDeviceInfoText { get; set; }
        public virtual ICollection<IoDeviceInfo> IoCatText { get; set; }
        public virtual ICollection<Group> GroupDesc { get; set; }
        public virtual ICollection<Group> GroupName { get; set; }
        public virtual ICollection<Type> Type { get; set; }
        public virtual ICollection<Parameter> ParamDispPath { get; set; }
        public virtual ICollection<EnumElem> EnumElemText { get; set; }
        public virtual ICollection<TextValue> TextValue { get; set; }
    }

这是我的控制器:

 public class GroupController : Controller
    {
        // GET: Group
        public ActionResult Index()
        {
            return PartialView("Index", GroupModel.Instance.getGroups());
        }

        public ActionResult Edit(string id)
        {
            Group group = KebaContext.SessionBasedContext().GroupSet.Where(g => g.GroupId == id).FirstOrDefault();
            List<Language> langs = KebaContext.SessionBasedContext().LanguageSet.ToList();
            foreach(Language l in langs)
            {
                if(group.GroupDescText == null)
                {
                    group.GroupDescText = new Text();
                    TextValue value = new TextValue();
                    value.TextId = Guid.NewGuid().ToString("N");
                    value.LangId = l.LangId;
                    value.Value = "";
                    group.GroupDescText.TextValue.Add(value);
                }
                if (group.GroupNameText == null)
                {
                    group.GroupNameText = new Text();
                    TextValue value = new TextValue();
                    value.TextId = Guid.NewGuid().ToString("N");
                    value.LangId = l.LangId;
                    value.Value = "";
                    group.GroupNameText.TextValue.Add(value);
                }
                if (group.GroupDescText != null && group.GroupDescText.TextValue.Where(x => x.LangId == l.LangId).FirstOrDefault() == null) //just one lang is available
                {
                    TextValue value = new TextValue();
                    value.TextId = group.GroupDescText.TextValue.First().TextId;
                    value.LangId = l.LangId;
                    value.Value = "";
                    group.GroupDescText.TextValue.Add(value);
                }
                if (group.GroupNameText != null && group.GroupNameText.TextValue.Where(x => x.LangId == l.LangId).FirstOrDefault() == null) //just one lang is available
                {
                    TextValue value = new TextValue();
                    value.TextId = group.GroupNameText.TextValue.First().TextId;
                    value.LangId = l.LangId;
                    value.Value = "";
                    group.GroupNameText.TextValue.Add(value);
                }
            }
            return View(group);
        }

        [HttpPost]
        public ActionResult Edit(Group xyz)
        {

            return RedirectToAction("Index", "Types");
        }
    }

这是我的观点:

@using System.Web.Mvc.Html;
@model Keba.Data.EF.Group
@{
    ViewBag.Title = "Group Editing";
}
<h2>Edit Group</h2>
<div id="groupEdit">
    @using (Html.BeginForm("Edit", "Group", FormMethod.Post))
    {
        @Html.HiddenFor(model => model.GroupId);
        <table class="userEditAddTable">
            <tr><th>Responsibility</th><td>@Html.EditorFor(model => model.Responsibility)</td></tr>
            @foreach (var name in Model.GroupNameText.TextValue)
            {
                @Html.HiddenFor(model => name.LangId)
                @Html.HiddenFor(model => name.Value)
                <tr><th>GroupNameText(@Html.DisplayFor(model => name.LangId))</th><td> @Html.TextBoxFor(model => name.Value)</td></tr>;
            }
            @foreach (var desc in Model.GroupDescText.TextValue)
            {
                @Html.HiddenFor(model => desc.LangId)
                @Html.HiddenFor(model => desc.Value)
                <tr><th>GroupDescText(@Html.DisplayFor(model => desc.LangId))</th><td> @Html.TextBoxFor(model => desc.Value)</td></tr>;
            }
        </table>
        <br />
        <div id="buttons">
            <input name="Save" type="submit" value="Save" class="button" />
            <input name="Cancel" type="submit" value="Cancel" class="button" />
        </div>
    }
</div>

问题:

如果我尝试更改组模型中文本的值,例如GroupNameText.TextValue.Value将其发送给控制器(提交)。 GroupNameText和GroupDescText属性为null。 我也尝试了使用propertybinding([Bind(Include = "GroupDescText,GroupNameText")] Group xyz)的解决方案,但也没有工作

2 个答案:

答案 0 :(得分:3)

首先,请记住,只会填充已发布的属性(即表单输入元素代表它们)。

其次,输入元素的名称必须与模型绑定器在帖子上期望的内容相匹配,否则它将丢弃这些值,因为它不知道如何处理它们。特别是,对于enumerables,这意味着您需要使用for循环而不是foreach,以便Razor可以创建正确的名称绑定:

@for (var i = 0; i < Model.GroupNameText.TextValue; i++)
{
    @Html.HiddenFor(m => m.GroupNameText.TextValue[i].LangId)
    @Html.HiddenFor(m => m.GroupNameText.TextValue[i].Value)
    ...
}

这将导致名称属性如GroupNameText.TextValue[0].LangId,模型绑定器应该能够正确绑定,而您的字段名称当前只是LangId,这在帖子上毫无意义。

答案 1 :(得分:0)

看看this类似于你的方法是在视图中有一个列表,你可能需要有部分。