我的模型上的许多属性需要表示一组简单的是/否单选按钮.我需要检查相应的单选按钮以获取预先输入的值.我偏袒了.这是我做的:
RadioButtonsYesNo.ascx
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<KeyValuePair<string,string>>" %>
<div id="<%:Model.Key %>">
<%: Html.ValidationMessage(Model.Key)%>
<ul class='RadioButtonsYesNo'><li>
<%: Html.RadioButton(Model.Key, "Yes", Model.Value == "Yes", new { id = Model.Key + "_Yes" })%>
<label for='<%: Model.Key %>_Yes'>Yes</label>
</li><li>
<%: Html.RadioButton(Model.Key, "No", Model.Value == "No", new { id = Model.Key + "_No" })%>
<label for='<%: Model.Key %>_No'>No</label>
</li></ul>
</div>
Run Code Online (Sandbox Code Playgroud)
用法
<% Html.RenderPartial("RadioButtonsYesNo", new KeyValuePair<string, string> ("MyProp",Model.MyProp)); %>
Run Code Online (Sandbox Code Playgroud)
传递感兴趣的属性并正确渲染RadioButtonFor是否有最佳实践?
谢谢.