0

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

我的观点 :

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

1 回答 1

0

您需要一个 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>

标记用于 Bootstrap。

于 2013-10-26T10:46:09.737 回答