RadioButtonFor()をデフォルトでチェックされるように設定するにはどうすればよいですか?
<%=Html.RadioButtonFor(m => m.Gender,"Male")%>
(Html.RadioButton)には道がありますが、(Html.RadioButtonFor)には道がありません
何か案は?
RadioButtonFor()をデフォルトでチェックされるように設定するにはどうすればよいですか?
<%=Html.RadioButtonFor(m => m.Gender,"Male")%>
(Html.RadioButton)には道がありますが、(Html.RadioButtonFor)には道がありません
何か案は?
簡単な方法を使用します。
<%= Html.RadioButtonFor(m => m.Gender, "Male", new { Checked = "checked" })%>
あまりきれいではありませんが、サイト全体に非常に少数のラジオ ボタンのみを実装する必要がある場合は、次のようなものもオプションになる可能性があります。
<%=Html.RadioButtonFor(m => m.Gender,"Male",Model.Gender=="Male" ? new { @checked = "checked" } : null)%>
ラジオボタンのグループが必要だと思います。何かが似ている可能性があります
<%=Html.RadioButtonFor(m => m.Gender,"Male")%>
<%=Html.RadioButtonFor(m => m.Gender,"Female")%>
<%=Html.RadioButtonFor(m => m.Gender,"Unknown")%>
コントローラーから m.Gender = "Unknown" (または何か) のデフォルト値を指定できます。
この StackOverflow に関する質問は RadioButtonListFor を扱い、回答はあなたの質問にも対応しています。選択したプロパティは、RadioButtonListViewModel で設定できます。
このヘルパーは式を評価し、値と等しい場合はラジオ ボタンをチェックし、RadioButtonFor と同じパラメーターを持ちます (このため、名前は異なります)。
public static MvcHtmlString CheckedRadioButtonFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object value)
{
return CheckedRadioButtonFor(htmlHelper, expression, value, null);
}
public static MvcHtmlString CheckedRadioButtonFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object value, object htmlAttributes)
{
var func = expression.Compile();
var attributes = new RouteValueDictionary(htmlAttributes);
if ((object)func(htmlHelper.ViewData.Model) == value) {
attributes["checked"] = "checked";
}
return htmlHelper.RadioButtonFor(expression, value, attributes);
}
使用法:
<%= Html.CheckedRadioButtonFor(m => m.Gender, "Male", new { id = "gender-male" })%>
結果:
<!-- For Model.Gender = "Male" -->
<input checked="checked" id="gender-male" name="Gender" type="radio" value="Male">
<!-- For Model.Gender = "Female" -->
<input id="gender-male" name="Gender" type="radio" value="Male">
テンプレートで@Html.EditorFor()を使用できるように、別のオプションを見つけました。
この列挙型があるとします:
public enum EmailType { Pdf, Html }
このコードをViews/Shared/EditorTemplates/EmailType.cshtmlに配置できます
@model EmailType
@{
var htmlOptions = Model == EmailType.Html ? new { @checked = "checked" } : null;
var pdfOptions = Model == EmailType.Pdf ? new { @checked = "checked" } : null;
}
@Html.RadioButtonFor(x => x, EmailType.Html, htmlOptions) @EmailType.Html.ToString()
@Html.RadioButtonFor(x => x, EmailType.Pdf, pdfOptions) @EmailType.Pdf.ToString()
いつでも使用したい場合は、これを簡単に使用できます。
@Html.EditorFor(x => x.EmailType)
この方法の方がはるかに普遍的であり、変更しやすいと感じています。
同じ ID を持つラジオ ボタンに関連付けられたラベルを追加することもできます。これにより、ユーザーはラジオ ボタンまたはラベルをクリックしてそのアイテムを選択できるようになります。ここでは「男性」、「女性」、「不明」に定数を使用していますが、明らかにこれらはモデル内の文字列である可能性があります。
<%: Html.RadioButtonFor(m => m.Gender, "Male",
new Dictionary<string, object> { { "checked", "checked" }, { "id", "Male" } }) %>
<%: Html.Label("Male") %>
<%: Html.RadioButtonFor(m => m.Gender, "Female",
new Dictionary<string, object> { { "id", "Female" } }) %>
<%: Html.Label("Female")%>
<%: Html.RadioButtonFor(m => m.Gender, "Unknown",
new Dictionary<string, object> { { "id", "Unknown" } }) %>
<%: Html.Label("Unknown")%>
<%: Html.RadioButtonFor(m => m.Gender, "Male", new { @checked = true } )%>
また
@checked = checked
もし良かったら
jqueryを使用している場合は、ラジオボタンの直前にこれを呼び出すことができます。
$('input:radio:first').attr('checked', true);
^これにより、最初のラジオボックスがチェックされますが、さらにjqueryを調べて、選択したいものに切り替えることができます。
デフォルトのラジオボタンをtrueに設定するコードは次のとおりです
@Html.RadioButtonFor(m => m.Gender, "Male", new { @checked = "checked", id = "rdGender", name = "rbGender" })
モデルのコンストラクターメソッドにデフォルト値を入れるのが最善だと思います。
Gender = "Male";
@Html.RadioButton("Insured.GenderType", 1, (Model.Insured.GenderType == 1 ))
@Web.Mvc.Claims.Resources.PartyResource.MaleLabel
@Html.RadioButton("Insured.GenderType", 2, Model.Insured.GenderType == 2)
@Web.Mvc.Claims.Resources.PartyResource.FemaleLabel