【发布时间】:2014-05-06 18:51:12
【问题描述】:
我有这个模型:
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>
}
谢谢
【问题讨论】:
-
你用我的回答解决了你的问题吗?
-
很抱歉我离开了几天,我现在就试试!谢谢!
标签: asp.net asp.net-mvc binding model http-post