我有这个型号:
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 :(得分:1)
使用For loop -
代替Foreach @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>
}
同样,您也可以继续使用上下文属性。
更新:请查看以下示例,了解如何在Parent模型中获取子模型的属性。我们可以使用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();
}
现在我们运行应用程序 -
然后在提交按钮上,您以这种方式获取SubmitData控制器操作中的值 -