asp.net-mvc ASP.NET MVC 2 中的 RadioButtonFor

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2588699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:14:39  来源:igfitidea点击:

RadioButtonFor in ASP.NET MVC 2

asp.net-mvcasp.net-mvc-2radiobuttonfor

提问by Larsenal

Can someone provide a simple example of how to properly use Html.RadioButtonFor? Let's say it's a simple scenario where my model has a string property named Gender. I want to display two radio buttons: "Male" and "Female".

有人可以提供一个如何正确使用 Html.RadioButtonFor 的简单示例吗?假设这是一个简单的场景,我的模型有一个名为 Gender 的字符串属性。我想显示两个单选按钮:“男”和“女”。

What is the most clean way to implement this while retaining the selected value in an Edit view?

在“编辑”视图中保留所选值的同时实现此目的的最简洁方法是什么?

采纳答案by Mac

This question on StackOverflowdeals with RadioButtonListFor and the answer addresses your question too (@Larsenal it also includes labels with the "for" attribute)

StackOverflow 上的这个问题涉及 RadioButtonListFor 并且答案也解决了您的问题(@Larsenal 它还包括带有“for”属性的标签)

回答by Darin Dimitrov

Male: <%= Html.RadioButtonFor(x => x.Gender, "Male") %>
Female: <%= Html.RadioButtonFor(x => x.Gender, "Female") %>