0

私のモデル

 public class IndexViewModel
{
    public IEnumerable<SelectListItem> TestRadioList { get; set; }

    [Required(ErrorMessage = "You must select an option for TestRadio")]
    public String TestRadio { get; set; }

    [Required(ErrorMessage = "You must select an option for TestRadio2")]
    public String TestRadio2 { get; set; }
}

public class aTest
{
    public Int32 ID { get; set; }
    public String Name { get; set; }
}

私のコントローラー

 public ActionResult Index()
    {
        List<aTest> list = new List<aTest>();
        list.Add(new aTest() { ID = 1, Name = "Yes" });
        list.Add(new aTest() { ID = 2, Name = "No" });
        list.Add(new aTest() { ID = 3, Name = "Not applicable" });
        list.Add(new aTest() { ID = 3, Name = "Muttu" });
        SelectList sl = new SelectList(list, "ID", "Name");
        var model = new IndexViewModel();
        model.TestRadioList = sl;
        return View(model);
    }

私の見解

@using (Html.BeginForm()) {
 <div>
    @Html.RadioButtonForSelectList(m => m.TestRadio, Model.TestRadioList)
</div>

}

ヘルパー メソッド

public static class HtmlExtensions
{
    public static MvcHtmlString RadioButtonForSelectList<TModel, TProperty>(
        this HtmlHelper<TModel> htmlHelper,
        Expression<Func<TModel, TProperty>> expression,
        IEnumerable<SelectListItem> listOfValues)
    {
        var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
        var sb = new StringBuilder();

        if (listOfValues != null)
        {
            // Create a radio button for each item in the list
            foreach (SelectListItem item in listOfValues)
            {
                // Generate an id to be given to the radio button field
                var id = string.Format("{0}_{1}", metaData.PropertyName, item.Value);

                // Create and populate a radio button using the existing html helpers
                var label = htmlHelper.Label(id, HttpUtility.HtmlEncode(item.Text));
                var radio = htmlHelper.RadioButtonFor(expression, item.Value, new { id = id }).ToHtmlString();

                // Create the html string that will be returned to the client
                // e.g. <input data-val="true" data-val-required="You must select an option" id="TestRadio_1" name="TestRadio" type="radio" value="1" /><label for="TestRadio_1">Line1</label>
                sb.AppendFormat("<div class=\"RadioButton\">{0}{1}</div>", radio, label);
            }
        }

        return MvcHtmlString.Create(sb.ToString());
    }
}

これが私が使用しているコードです...コントロールのonclickイベントを与える方法がわかりません。ヘルパー メソッドで、適切な htmlattributes パラメータが見つかりませんでした。私の要件に従って。リスト内のラジオボタンをクリックすると、いくつかのパラメーターを使用して js 関数を呼び出す必要があります。私にはできないことです。誰か助けてください。前もって感謝します。

4

1 に答える 1

0

現時点ではこれをテストする手段はありませんが、大まかなアイデアはIDictionary htmlAttributesをメソッドに追加し、そこに渡すことです。ビューに必要なonClickコードがない場合は、パラメーターを省略して、拡張メソッドで実行できます。

    public static class HtmlExtensions
    {
        public static MvcHtmlString RadioButtonForSelectList<TModel, TProperty>(
            this HtmlHelper<TModel> htmlHelper,
            Expression<Func<TModel, TProperty>> expression,
            IEnumerable<SelectListItem> listOfValues,
            IDictionary<string, Object> htmlAttributes)
        {
            var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            var sb = new StringBuilder();

            if (listOfValues != null)
            {
                // Create a radio button for each item in the list
                foreach (SelectListItem item in listOfValues)
                {
                    // Generate an id to be given to the radio button field
                    var id = string.Format("{0}_{1}", metaData.PropertyName, item.Value);

                    // Create and populate a radio button using the existing html helpers
                    var label = htmlHelper.Label(id, HttpUtility.HtmlEncode(item.Text));

                    htmlAttributes["id"] = id;
                    var radio = htmlHelper.RadioButtonFor(expression, item.Value, htmlAttributes).ToHtmlString();

                    // Create the html string that will be returned to the client
                    // e.g. <input data-val="true" data-val-required="You must select an option" id="TestRadio_1" name="TestRadio" type="radio" value="1" /><label for="TestRadio_1">Line1</label>
                    sb.AppendFormat("<div class=\"RadioButton\">{0}{1}</div>", radio, label);
                }
            }

            return MvcHtmlString.Create(sb.ToString());
        }
    }

次に、次のようなものを使用して呼び出します。

@Html.RadioButtonForSelectList(m => m.TestRadio, Model.TestRadioList, new { onclick="someFunction();" })

または、cssクラスを設定して、クリックイベントにバインドすることもできます。例えば、

<script type="text/javascript>
    $('.someClassName').click( function() {
    alert('clicked');
});
</script>

@Html.RadioButtonForSelectList(m => m.TestRadio, Model.TestRadioList, new { @class="someClassName" })
于 2012-07-12T12:57:29.087 に答える