2015-10-19 101 views
1

我有这样的模式:型号属性为null后提交

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为空。 我也尝试过使用属性绑定([Bind(Include = "GroupDescText,GroupNameText")] Group xyz)的解决方案,该解决方案也不起作用

+0

而不是foreach中的标签尝试拥有代表列表中每个元素的局部视图 –

回答

2

首先,请记住只有发布的属性(即具有表示它们的表单输入元素)才会被填充。

其次,输入元素的名称必须与模型联编程序在发布后期望的内容相匹配,否则将放弃这些值,因为它不知道如何处理它们。特别是,可枚举,这意味着你需要使用for循环,而不是foreach,使剃须刀可以创建正确的名称绑定:

@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) 
    ... 
} 

这将导致name属性一样GroupNameText.TextValue[0].LangId,模型绑定应其能够正确绑定,而您的字段名称目前只是LangId,这在后期是没有意义的。

+0

我现在拥有此代码@ Html.HiddenFor(m => m.GroupNameText.TextValue.ToArray()[i] .Value) ,仍然不起作用。 TextValue是一个ICollection,所以我不能直接使用索引器。 – user547995

+0

您不能将其转换为'HiddenFor'中的数组。您仍然没有生成正确的字段名称,并且我很坦白地意识到您没有收到运行时错误。无论如何,您需要首先在模型上使用可索引的东西。如果您无法直接修改模型类以适应这种情况,那么您需要使用视图模型。 –

0

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