Asp.net mvc 能够为ASP.NET MVC中的单选按钮选择多个选项

Asp.net mvc 能够为ASP.NET MVC中的单选按钮选择多个选项,asp.net-mvc,asp.net-mvc-3,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 3,Asp.net Mvc 4,我正在开发一个投票页面机制。这里我将有一个问题列表,每个问题我有3个选项,我使用单选按钮。我已附上我的视图和控制器方法。我正在正确地获取保存到DB的值,但我的问题是我能够在使用单选按钮的地方选择多个选项。我想确保,如果为一个问题选择了一个选项,则必须自动取消选择其他选项,这对我来说是不可能的 我的看法是: @using (Html.BeginForm()) { <div> @foreach (var a in ViewBag.Questio

我正在开发一个投票页面机制。这里我将有一个问题列表,每个问题我有3个选项,我使用单选按钮。我已附上我的视图和控制器方法。我正在正确地获取保存到DB的值,但我的问题是我能够在使用单选按钮的地方选择多个选项。我想确保,如果为一个问题选择了一个选项,则必须自动取消选择其他选项,这对我来说是不可能的

我的看法是:

 @using (Html.BeginForm())
 {   
        <div>
         @foreach (var a in ViewBag.Questions)
         {
             <h4>@a.Questions</h4>
             <div>
                @foreach (var b in Model)
                {
                   if (b.QuestionsID == a.id)
                   {                                  
                      @Html.RadioButton(b.AnswersOptions, 
                               new {Answerid= b.id, Questionid=a.id })                     
                      @b.AnswersOptions
                   }
                }
             </div>
         }

        </div>
        <br/>

        <div >
           <input type="submit" value="Vote Now!!" 
                onclick="return confirm('Are you sure you want to                                         
                submit your choices?');"/>
        </div>
 }
我的控制器:

    public ActionResult VotingResult_Post(FormCollection resultcollection)
    { 
        int resultcollectionCount = resultcollection.Count;

        if (resultcollectionCount == CountofQuestionsDisplayed)
        {
            for (int i = 0; i < resultcollectionCount; i++)
            {
                string SelectedIDArray = resultcollection[i];
                string SelectedAnswerIDValue = GetValue("Answerid", SelectedIDArray);                                                                                
                string SelectedQuestionID = GetValue("Questionid", SelectedIDArray);                 

                InsertUsersReponses(SelectedQuestionID, SelectedAnswerIDValue);                   
            }
        }

        List<Voting_Questions> QuesList = PopulateQuestions();
        ViewBag.Questions = QuesList;
        List<Voting_Answers> Answers = aobj.Voting_Answers.ToList();
        return View(Answers);
    }

您需要一个如下所示的HTML帮助程序

public static System.Web.Mvc.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();            
        string ForFormat = String.Empty;
        if (listOfValues != null)
        {
            // Create a radio button for each item in the list 
            //  need to create correct ID here
            var baseID = metaData.PropertyName;

            foreach (SelectListItem item in listOfValues)
            {
                // Generate an id to be given to the radio button field 
                var id = string.Format("{0}_{1}", baseID, item.Value);

                // Create and populate a radio button using the existing html helpers 

                var label = htmlHelper.Label(id, HttpUtility.HtmlEncode(item.Text));

                //  extracting the text for="##" from the label and using this for the control ID
                // ASSUMES the format<label for="TestRadio_1">Line 1</label> and splitting on the quote means that the required value is in the second cell of the array
                String[] temp = label.ToString().Split('"');
                var radio = htmlHelper.RadioButtonFor(expression, item.Value, new { id = temp[1] }).ToHtmlString();

                // Create the html string that will be returned to the client 
                // e.g. <input data-val="true" data-val-required="Option1" id="TestRadio_1" name="TestRadio" type="radio" value="1" /><label for="TestRadio_1">Line 1</label>
                // e.g. <input data-val="true" data-val-required="Option2" id="TestRadio_2" name="TestRadio" type="radio" value="2" /><label for="TestRadio_2">Line 2</label>
                sb.AppendFormat("<div class=\"RadioButtonList\">{0}{1}</div>", radio, label);
            }
        }
        return MvcHtmlString.Create(sb.ToString());
其名称如下:

@Html.ValidationMessageFor(m => m.myProperty)
@Html.LabelFor(m => m.myProperty, new { @class = "editor-label control-label" })
<div class="editor-field controls radio">
    @Html.RadioButtonForSelectList(
        m => m.myProperty,
        ListOfOptionsForRadioButton
    )
</div>

标记用于引导。

请发布一个最简单的示例:在@Html.RadioButtonb.AnswersOptions中,新建{Answerid=b.id,Questionid=a.id},检查你的b.AnswersOptions。如果没有相同的按钮,您可以选择多个单选按钮。这是一个自定义html助手类吗??我不能使用现有的html帮助吗?我正在使用MVC 3,是的,它是一个自定义类,因为我找不到适用于MVC 3的类。MVC4中可能有一个内置类,我不知道。另一个选择是直接使用razor编写所需的HTML来创建选项