我的 MVC 应用程序中有几个单选按钮,我应用了 CSS 样式来显示图像而不是实际的单选按钮圆圈。我一直在寻找一种方法将选定的单选按钮值传递给 Controller 。我曾尝试在线寻找解决方案,但找不到任何解决方案。任何帮助将不胜感激。
@model xxxx.xxxxx.xxxx.AddEmployeeCommand
@using (Html.BeginForm("Index", "EmployeeDetails", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<label class="label"> Name </label> @Html.TextBoxFor(model => model.Name)
<input type="radio" class="radio_item" value="Male" name="item" id="Male">
<label class="label_item" for="Male">
<img src="~/Images/Test1.png" width="40" height="40" />
</label>
<input type="radio" class="radio_item" value="Female" name="item" id="Female">
<label class="label_item" for="Female">
<img src="~/Images/Test2.png" width="40" height="40" />
</label>
}
这是我的 CSS
.radio_item {
display: none !important;
}
.label_item {
opacity: 0.5;
}
.radio_item:checked + label {
opacity: 1;
}
这是我的 Controller 类,
public class EmployeeDetailsController : Controller
{
[HttpPost]
public ActionResult Index(AddEmployeeCommand command)
{
employeeCommandHandler.Save(command);
return View();
}
}
最佳答案
您必须将所有单选选项绑定(bind)到一个组中,即。到同一个属性。然后您可以将其发布到 Controller /操作..
引用下面的链接会有帮助.. When using .net MVC RadioButtonFor(), how do you group so only one selection can be made?
关于css - 如何将单选按钮值从 MVC 中的 View 传递给 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49362956/