2014-05-06 60 views
1

我有這樣的模式:模型綁定不工作ASP.NET

public class TaskCreation 
{ 
    public task_description task_desc { get; set; } 
    public List<Metric> metric { get; set; } 
    public List<Context> context { get; set; } 
    public int scenarioId { get; set; } 
    public short meas_id { get; set; } 

} 
public class Metric 
{ 
    public string name { get; set; } 
    public string value { get; set; } 
} 
public class Context 
{ 
    public string name { get; set; } 
    public string values { get; set; } 
    public string upper_bound { get; set; } 
    public string lower_bound { get; set; } 
} 

這是我的控制器方法:

public ActionResult CreateTask2() 
    { 
     TaskCreation tc = new TaskCreation(); 
     tc = TempData["TCObject"] as TaskCreation; 
     return View(tc); 
    } 
    [HttpPost] 
    public ActionResult CreateTask2(TaskCreation TaskCreation) 
    { 
     //some code 
    } 

所以認爲收到TaskCreation對象和有田進入每個度量和值的值,每個上下文的上限和下限,但在[httppost] CreateTask2中,度量和上下文對象爲空! task_description對象中的其他屬性被正確綁定!我如何解決它?

這是我的看法:

@model Project.Models.TaskCreation 
@using (Html.BeginForm("CreateTask2", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
@Html.AntiForgeryToken() 

<div class="form-horizontal"> 

    <hr /> 
    @Html.ValidationSummary(true) 
<div class="form-group"> 
     @Html.LabelFor(model => model.task_desc.Is_valid, new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.task_desc.Is_valid) 
      @Html.ValidationMessageFor(model => model.task_desc.Is_valid) 
     </div> 
    </div> 
    <br /> 
    <div class="form-group"> 
     @Html.LabelFor(model => model.task_desc.repeat_interval, new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.task_desc.repeat_interval) 
      @Html.ValidationMessageFor(model => model.task_desc.repeat_interval) 
     </div> 
    </div> 
    <div class="form-group"> 
     <p>Enter the metrics values:</p> 
    </div> 
    @foreach(var m in Model.metric) 
    { 
     <div class="form-group"> 
     @Html.Label(m.name, new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(x => m.value, new { placeholder = "Value" }) 
      @Html.ValidationMessageFor(x => m.value) 
     </div> 
      <br /> 
    </div> 
    } 
    <div class="form-group"> 
     <p>Choose the constraints for each context:</p> 
    </div> 
     @foreach(var c in Model.context) 
    { 
      <div class="form-group"> 
       @Html.Label(c.name, new { @class = "control-label col-md-2" }) 
       <div class="col-md-10"> 
        <table> 
         <tr> 
          <td> 
           @Html.TextBoxFor(con => c.lower_bound, new { placeholder = "Lower Bound" }) 
           @Html.ValidationMessageFor(con => c.lower_bound)<br /> 
           @Html.TextBoxFor(con => c.higher_bound, new { placeholder = "Upper Bound" }) 
           @Html.ValidationMessageFor(con => c.higher_bound) 
          </td> 
          <td>OR</td> 
          <td> 
           @Html.TextBoxFor(con => c.values, new { placeholder = "Selected Values Separated by Spaces" }) 
           @Html.ValidationMessageFor(con => c.values) 
          </td> 
          <br /> 
         </tr> 
        </table> 
       </div> 
      </div> 
    } 
    <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" class="btn btn-default" /> 
      </div> 
    </div> 
</div> 
} 

謝謝

+0

你用我的答案解決了你的問題嗎? – ramiramilu

+0

對不起,我離開了幾天,我現在就試試吧!謝謝! – caj

回答

1

相反的foreach,for循環 -

@for(int i = 0; i < Model.metric.Count; i++) 
    { 
     <div class="form-group"> 
     @Html.Label(Model.metric[i].name, new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(m => m.metric[i].value, new { placeholder = "Value" }) 
      @Html.ValidationMessageFor(m => m.metric[i].value) 
     </div> 
      <br /> 
    </div> 
    } 

同樣你可以繼續爲上下文屬性了。

更新:請查看下面的示例,瞭解如何在父模型中獲取子模型的屬性。我們可以使用FOR循環迭代子模型。如果我們使用For循環,我們可以在服務器端獲得子模型的值,而無需額外的工作。檢查下面的示例 -

比方說,我們有以下型號 -

public class DataModel 
{ 
    public string Name { get; set; } 
    public string Email { get; set; } 
    public List<XhrViewModel> eles { get; set; } 
} 

public class XhrViewModel 
{ 
    public string data1 { get; set; } 
    public string data2 { get; set; } 
} 

然後,我們有以下的控制器操作將返回創建視圖 -

public ActionResult CreateData() 
    { 
     DataModel m = new DataModel(); 
     m.eles = new List<XhrViewModel>(); 
     m.eles.Add(new XhrViewModel()); 
     m.eles.Add(new XhrViewModel()); 
     m.eles.Add(new XhrViewModel()); 
     return View(m); 
    } 

而創建視圖很簡單 -

@model Rami.Vemula.Dev.Mvc.Controllers.DataModel 

@{ 
    ViewBag.Title = "CreateData"; 
} 

<h2>CreateData</h2> 

@using (Html.BeginForm("SubmitData", "Home", FormMethod.Post)) 
{ 
    @Html.AntiForgeryToken() 

    <div class="form-horizontal"> 
     <h4>DataModel</h4> 
     <hr /> 
     @Html.ValidationSummary(true) 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Name) 
       @Html.ValidationMessageFor(model => model.Name) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Email, new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Email) 
       @Html.ValidationMessageFor(model => model.Email) 
      </div> 
     </div> 

     @for (int i = 0; i < Model.eles.Count; i++) 
     { 
      <div class="form-group"> 
       @Html.LabelFor(model => model.eles[i].data1, new { @class = "control-label col-md-2" }) 
       <div class="col-md-10"> 
        @Html.EditorFor(model => model.eles[i].data1) 
        @Html.ValidationMessageFor(model => model.eles[i].data1) 
       </div> 
      </div> 
     } 

     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Create" id="ClickMe" class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 
} 

如果你在上面的查看,我用FOR循環代替Foreach來渲染子模型屬性即xhrViewModel。

而且提交按鈕的點擊,你會打SubmitData控制器動作 -

public ActionResult SubmitData(DataModel m) 
    { 
     return View(); 
    } 

現在,當我們運行應用程序 -

enter image description here

然後提交按鈕,你得到的SubmitData控制器中的值以這種方式動作 -

enter image description here

+0

爲了進一步解釋......現在發佈的是幾個鍵,比如「m.value」。模型聯編程序並不知道如何將其綁定回模型中。如果您使用for循環,每個都將看起來像:Model.metric [1] .value。因此,它會將該信息綁定到模型上,索引爲1. –

+0

是的,我正在爲您提供一個簡單的代碼 – ramiramilu

+0

@ErikElkins請檢查我的更新答案。希望它解決了這個問題。 – ramiramilu