我有這樣的模式:模型綁定不工作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>
}
謝謝
你用我的答案解決了你的問題嗎? – ramiramilu
對不起,我離開了幾天,我現在就試試吧!謝謝! – caj