0

My Model

 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; }
}

My Controller

 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);
    }

My View

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

}

Helper method

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());
    }
}

Here is the code i'm using... not sure how to give a onclick event for the control. In the helper method i could not find any appropriate htmlattributes parameter. as per my requirement. on click of any radiobutton in the list i need to call a js function with few parameters. which i'm not able to do. Someonce please help. Thanks in advance.

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 类并绑定到 click 事件。例如,

<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 回答