css - 如何将单选按钮值从 MVC 中的 View 传递给 Controller

标签 css asp.net-mvc

我的 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/

相关文章:

基于通配符的属性名称的 CSS 选择器

html - 菜单周围的蓝色方 block

css - 让一些渐变在进度条中无限移动,就像在 Windows 7 中一样

javascript - 脚本部分仅在一种 View 中起作用

asp.net-mvc - ASP.net mvc 2 解决方案文件夹结构

css - IE 中的对 Angular 渐变

javascript - 加载页面时,">"符号显示为 "&gt;"

asp.net-mvc - .NET 自定义成员资格与。自定义登录/注册 : Authentication/Authorization

c# - 内存流中的电子邮件附件出错,但仅在第一次发送后发生

css - 导航栏固定顶部内的 Bootstrap 4 下拉问题