2016-10-19 78 views
-1

我的模型:Asp.net MVC - 查看到控制器,列表<model>爲空

public class AdminPageModel 
{ 
    public int? JobID { get; set; } 

    [DisplayName("Description")] 
    public string Description { get; set; } 

    [DisplayName("Weight")] 
    public decimal Weight { get; set; } 

    public int ID { get; set; }   
} 

表1:

using (Html.BeginForm("EditTemplate", "Config", FormMethod.Post, new { @class = "form-horizontal" })) 
{ 
    for (var i = 0; i < Model.Count; i++) 
    { 
     <div class="form-group"> 
     @if (Model[i].JobID != null) 
     { 
      @Html.LabelFor(m => m[i].Description) 
      @Html.TextBoxFor(m => m[i].Description) 

      @Html.LabelFor(m => m[i].Weight) 
      @Html.TextBoxFor(m => m[i].Weight) 

      @Html.HiddenFor(m => m[i].ID) 
      @Html.HiddenFor(m => m[i].JobID) 
      <br />@Html.ActionLink("Delete", "DeleteTemplate", new { id = @Model[i].ID, jobID = selectedJobID }) 
     } 

     </div> 
    } 
    <input type="submit" value="Update" class="btn btn-success" id="editSubmit" /> 
} 

表2:

using (Html.BeginForm("EditCompetency", "Config", FormMethod.Post, new { @class = "form-horizontal" })) 
{ 
    for (var i = 0; i < Model.Count; i++) 
    { 
     <div class="form-group"> 
      @if (Model[i].JobID == null) 
      { 
       @Html.LabelFor(m => m[i].Description) 
       @Html.TextBoxFor(m => m[i].Description) 

       @Html.LabelFor(m => m[i].Weight) 
       @Html.TextBoxFor(m => m[i].Weight) 

       @Html.HiddenFor(m => m[i].ID) 
       @Html.HiddenFor(m => m[i].JobID) 
       <br />@Html.ActionLink("Delete", "DeleteCompentency", new { id = @Model[i].ID}) 
      } 

     </div> 
    } 
    <input type="submit" value="Update" class="btn btn-success" id="editSubmit" /> 

} 

控制器:

[HttpPost] 
public ActionResult EditCompetency(List<AdminPageModel> modelList) 
{ 
    // stuff 
} 

[HttpPost] 
public ActionResult EditTemplate(List<AdminPageModel> modelList) 
{ 
    // stuff 
} 

這是問題所在。我有模板和能力。我可以整天添加/刪除。嘗試編輯時遇到問題。

模板沒有問題,只有能力。如果我添加了一個能力並且沒有模板存在,那麼編輯工作得很好。但是,如果我在嘗試編輯能力時存在模板,則List<AdminPageModel> modelList(我的視圖被強制鍵入的類型)爲空。如果我刪除了模板,我可以將該能力編輯到我的心中。我今天早上花了一大塊時間在這個上做了些調整。

+0

這兩種形式都在同一頁上?如果他們是那麼你將有相同的名稱和ID控制,這可能會以幾種不同的方式導致問題。 – sam2929

+0

他們確實在同一頁面上。 –

+1

你可以嘗試把它們放在不同的頁面上嗎?或者,也可以讓頁面的視圖模型包含兩個列表:一個用於模板,另一個用於Compentencies。然後將這兩個列表綁定到適當的表單。現在你基本上將相同的數據綁定到兩種形式。如果這對你沒有意義,我可以在答案中寫出一些代碼。 – sam2929

回答

0

下面是如何將您的能力數據和模板數據組合爲單個視圖模型以便您沒有重疊控件名稱的示例。

ConfigController.cs

[HttpGet] 
public ActionResult Index() 
{ 
    AdminViewModel viewModel = // Get Data Somehow 
    return View(viewModel); 
} 

[HttpPost] 
public ActionResult EditCompetency(AdminViewModel viewModel) 
{ 
    // access competency data with viewModel.Competencies 
} 

[HttpPost] 
public ActionResult EditTemplate(AdminViewModel viewModel) 
{ 
    // access template data with viewModel.Templates 
} 

AdminViewModel.cs新類

public class AdminViewModel 
{ 
    public List<AdminPageModel> Templates { get; set; } 
    public List<AdminPageModel> Competencies { get; set; } 
} 

AdminPage.cshtml

@model AdminViewModel 

using (Html.BeginForm("EditTemplate", "Config", FormMethod.Post, new { @class = "form-horizontal" })) 
{ 
    for (var i = 0; i < Model.Templates.Count; i++) 
    { 
     <div class="form-group"> 
     @if (Model.Templates[i].JobID != null) 
     { 
      @Html.LabelFor(m => m.Templates[i].Description) 
      @Html.TextBoxFor(m => m.Templates[i].Description) 

      @Html.LabelFor(m => m.Templates[i].Weight) 
      @Html.TextBoxFor(m => m.Templates[i].Weight) 

      @Html.HiddenFor(m => m.Templates[i].ID) 
      @Html.HiddenFor(m => m.Templates[i].JobID) 
      <br />@Html.ActionLink("Delete", "DeleteTemplate", new { id = @Model.Templates[i].ID, jobID = selectedJobID }) 
     } 

     </div> 
    } 
    <input type="submit" value="Update" class="btn btn-success" id="editSubmit" /> 
} 

using (Html.BeginForm("EditCompetency", "Config", FormMethod.Post, new { @class = "form-horizontal" })) 
{ 
    for (var i = 0; i < Model.Competencies.Count; i++) 
    { 
     <div class="form-group"> 
      @if (Model.Competencies[i].JobID == null) 
      { 
       @Html.LabelFor(m => m.Competencies[i].Description) 
       @Html.TextBoxFor(m => m.Competencies[i].Description) 

       @Html.LabelFor(m => m.Competencies[i].Weight) 
       @Html.TextBoxFor(m => m.Competencies[i].Weight) 

       @Html.HiddenFor(m => m.Competencies[i].ID) 
       @Html.HiddenFor(m => m.Competencies[i].JobID) 
       <br />@Html.ActionLink("Delete", "DeleteCompentency", new { id = @Model.Competencies[i].ID}) 
      } 

     </div> 
    } 
    <input type="submit" value="Update" class="btn btn-success" id="editSubmit" /> 

} 
相關問題