2015-10-19 103 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類似於你的方法是在視圖中有一個列表,你可能需要有偏差。