我正在生成一个单选按钮组,以使用MVC&Razor和强类型视图在页面显示中显示选择选项。虽然循环有效,但我无法将单选按钮组的选择从控制器返回的数据更改为第二个值。我的HTML / Razor标记/代码如下:
@model IEnumerable<XXXX.App.Models.SettingsModel>
@{
ViewBag.Title = "Application Settings";
}
@using (Html.BeginForm("Index", "Settings", FormMethod.Post))
{
<div class="container body-content">
<div id="settings-page" class="row">
<div class="row">
<div class="input-field col l12 m12 s12 center">
<p class="center collection-header">Application Settings.</p>
</div>
</div>
<table class="striped">
@foreach (var item in Model)
{
<tr>
<td>
@if (item.CONFIG_TYPE != 4)
{
@Html.DisplayFor(modelItem => item.CONFIG_NAME)
}
else
{
<strong>@Html.DisplayFor(modelItem => item.CONFIG_NAME)</strong>
}
</td>
<td>
@{
switch (item.CONFIG_TYPE)
{
case 1:
//@Html.TextBoxFor(modelItem => item.CONFIGURATION_VALUE, "{0:c}", new { @id = item.URN.ToString(), @class = "input-field col l4 m4 s12", Title = item.CONFIG_DESCRIPTION, Type = "number" })
break;
case 2:
//@Html.TextBoxFor(modelItem => item.CONFIGURATION_VALUE, null, new { @id = item.URN.ToString(), @class = "input-field col l4 m4 s12", Title = item.CONFIG_DESCRIPTION, Type = "text" })
break;
case 3:
@Html.RadioButtonFor(modelIten => item.CONFIGURATION_VALUE, "1", new { @id = item.URN.ToString() + "_0", @class = "with-gap", @title = item.CONFIG_DESCRIPTION, Type = "radio"})@Html.Label("Yes")
@Html.RadioButtonFor(modelIten => item.CONFIGURATION_VALUE, "0", new { @id = item.URN.ToString() + "_1", @class = "with-gap", @title = item.CONFIG_DESCRIPTION, Type = "radio"})@Html.Label("No")
break;
case 4:
break;
}
}
</td>
</tr>
}
</table>
<br/>
<div class="row center">
<input id="saveChanges" type="submit" value="Save" title="Save Changes." class="btn waves-effect waves-light center-align" />
</div>
</div>
视图生成的HTML如下:
<form action="/Settings" method="post">
<div class="container body-content">
<div id="settings-page" class="row">
<div class="row">
<div class="input-field col l12 m12 s12 center">
<p class="center collection-header">Application Settings.</p>
</div>
</div>
<table class="striped">
<tr>
<td>
<strong>Account Lockout</strong>
</td>
<td>
</td>
</tr>
<tr>
<td>
Invalid Login Count </td>
<td>
<input Type="radio" checked="checked" class="with-gap" id="da352600-8342-445c-9b9e-d27725ada4f8_0" name="item.CONFIGURATION_VALUE" title="Account lockout maximum number of invalid logins permitted within the Invalid Login Window." type="radio" value="1"
/><label for="Yes">Yes</label><input Type="radio" class="with-gap" id="da352600-8342-445c-9b9e-d27725ada4f8_1" name="item.CONFIGURATION_VALUE" title="Account lockout maximum number of invalid logins permitted within the Invalid Login Window."
type="radio" value="0" /><label for="No">No</label>
</td>
</tr>
<tr>
<td>
Invalid Login Window </td>
<td>
</td>
</tr>
</table>
<br/>
<div class="row center">
<input id="saveChanges" type="submit" value="Save" title="Save Changes." class="btn waves-effect waves-light center-align" />
</div>
</div>
</div>
</form>
我注意到生成的所有控件都使用相同的名称(item.CONFIGURATION_VALUE),但是我希望它们使用数据集中的实际值吗?我误会什么?
只要有需要,这就是控制器的代码:
public async Task<ActionResult> Index(SettingsModel model)
{
ApiHandler api = new ApiHandler();
CookieData cookie = new CookieData();
// get the company configuration settings from the web api
HttpResponseMessage response = await api.ReturnApiHttpClient(true).GetAsync("Settings/ReturnCompanyConfig?companyURN=" + cookie.ReturnCompanyURN());
// check that the response received is valid
api.CheckHttpResponse(response);
// check that we have received company settings back
string result = response.Content.ReadAsStringAsync().Result.ToString();
if (result.Length == 0)
{
TempData["popupMessage"] = "Error.|Unable to get the company identifier for the logged in user.";
TempData["popupScript"] = "popup.mPopup('open');";
return RedirectToAction("Index", "Home");
}
// convert the json result into the model and return the view with the model
return View(api.ConvertJsonToModel<SettingsModel>(result));
}
}