3
我有一個頁面包含形式(下圖):是否會有一段時間我應該將ViewModel傳遞到我的控制器的一個Action Action中?
@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>
}
}
正如你可以看到,這種形式是聯繫在一起的「過程」控制器的裏面的「GetResults」操作方法。因此,當用戶點擊「提交」時,傳遞的對象應該是表單中定義的對象,對吧?
這裏是我的「GetResults」的操作方法:
public ActionResult GetResults(ProcessViewModel vm)
{
vm.NumberOfCapsules = 100;
List<Results> results = context.GetResults();
return View(results);
}
是通過我的ViewModel到我的控制器一個很好的做法,如果沒有,我怎麼會捕獲所有的數據從形式?
謝謝..!
謝謝...代碼的原因是我希望這個問題儘可能完整,以防其他人對我的問題的上下文有疑問。 – 2013-02-21 01:04:07
我只需輸入「Yes」即可。 ;) – 2013-02-21 01:05:36
哈哈,冷靜的人。不用擔心,謝謝。 – 2013-02-21 01:06:27