有没有一段时间我应该把ViewModel传递到我的控制器中';s行动方法
本文关键字:方法 控制器 我的 我应该 一段时间 ViewModel 有没有 | 更新日期: 2023-09-27 18:00:17
我有一个页面,其中包含一个表单(如下):
@using (Html.BeginForm("GetResults", "Process"))
{
@Html.ValidationSummary(true)
{
<div data-role="fieldcontain">
<label for="name">API #1:</label><br />
@Html.EditorFor(model => model.Active1.mg)
@Html.ValidationMessageFor(model => model.Active1.mg)
</div>
<div data-role="fieldcontain">
<label for="name">API #2:</label><br />
@Html.EditorFor(model => model.Active2.mg)
@Html.ValidationMessageFor(model => model.Active2.mg)
</div>
<div data-role="fieldcontain">
<label for="name">API #3:</label><br />
@Html.EditorFor(model => model.Active3.mg)
@Html.ValidationMessageFor(model => model.Active3.mg)
</div>
<div data-role="fieldcontain">
<label for="name">API #4:</label><br />
@Html.EditorFor(model => model.Active4.mg)
@Html.ValidationMessageFor(model => model.Active4.mg)
</div>
<div data-role="fieldcontain">
<label for="name">API #5:</label><br />
@Html.EditorFor(model => model.Active5.mg)
@Html.ValidationMessageFor(model => model.Active5.mg)
</div>
<div data-role="fieldcontain">
<label for="name">Key Active:</label><br />
@Html.EditorFor(model => model.KeyActive.mg)
@Html.ValidationMessageFor(model => model.KeyActive.mg)
</div>
<div data-role="fieldcontain">
<label for="name">Key Active Pack Stat:</label><br />
@Html.EditorFor(model => model.KeyActive.key_active_pack_stat)
@Html.ValidationMessageFor(model => model.KeyActive.key_active_pack_stat)
</div>
<div data-role="fieldcontain">
<label for="lble4m">E4M: </label>
<br />
@Html.DropDownList("ddle4m", new[] { new SelectListItem() { Text = "Off", Value = "False" }, new SelectListItem() { Text = "On", Value = "True" } }, new { data_role = "slider" })
</div>
<div data-role="fieldcontain">
<label for="lblk100m">K100M: </label>
<br />
@Html.DropDownList("ddlk100m", new[] { new SelectListItem() { Text = "Off", Value = "False" }, new SelectListItem() { Text = "On", Value = "True" } }, new { data_role = "slider" })
</div>
<div data-role="fieldcontain">
<label>Choose Filler:</label><br />
<fieldset data-role="controlgroup">
@foreach (var i in Model.Fillers)
{
<input type="radio" name="radio-choice" id="@i.pk" />
<label for="@i.pk">@i.name</label>
}
</fieldset>
</div>
<p>
<input type="submit" value="Create" />
</p>
}
}
正如您所看到的,此表单绑定到"Process"控制器内部的"GetResults"操作方法。因此,当用户单击"提交"时,传递的对象应该是表单中定义的对象,对吗?
这是我的"GetResults"操作方法:
public ActionResult GetResults(ProcessViewModel vm)
{
vm.NumberOfCapsules = 100;
List<Results> results = context.GetResults();
return View(results);
}
将我的ViewModel传递到我的控制器中是一种好的做法吗?如果不是,我将如何从表单中捕获所有数据?
谢谢。。!
你确实在那里发布了很多代码。然后你问一个问题。这个问题的答案是:是的。