【发布时间】:2014-03-07 12:23:24
【问题描述】:
我以前在 MVC 项目中没有使用过单选按钮。 现在,自从我第一次使用它以来,我似乎遇到了一个问题。
我想要做的是,会有问题,用户可以从可用的答案中选择一个答案。每个答案都有一个单选按钮。
这是我的模型
public class CreateAppointmentSelectOptions
{
public Guid AppointmentId { get; set; }
//question1
[Display(Name = "Repeat invitation untill a common date is found")]
public bool RepeatTillCommonDateIsFound { get; set; }
[Display(Name = "Repeat times")]
[Range(1,5,ErrorMessage="Repeat times must be between {1} and {2}")]
public int RepeatTimes { get; set; }
//question 1
[Display(Name="Repeat invitation once")]
public Boolean RepeatOnce { get; set; }
//question 1
[Display(Name="Do not repeat invitation")]
public Boolean NoRepeat { get; set; }
//question 2
[Display(Name = "Cancel the invitation")]
public Boolean CancelInvitation { get; set; }
//question 2
[Display(Name="Plan appointment with my first available date")]
public Boolean FirstAvailableCommon { get; set; }
//question 2
[Display(Name="Plan with all participants available on the first available common date")]
public Boolean OwnerFirstAvailableCommon { get; set; }
}
和控制器
[HttpGet]
public ActionResult Create_WhatIf(Guid appointmentId)
{
var appointmentCondition = new CreateAppointmentSelectOptions
{
AppointmentId = appointmentId,
RepeatOnce = true,
NoRepeat = false,
RepeatTillCommonDateIsFound=false,
CancelInvitation = false,
OwnerFirstAvailableCommon=false,
FirstAvailableCommon = true
};
return View(appointmentCondition);
}
[HttpPost]
public ActionResult Create_WhatIf(CreateAppointmentSelectOptions options)
{
return View();
}
以及观点
@model CreateAppointmentSelectOptions
@{
ViewBag.Title = "Create_WhatIf";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>What If?</h2>
@using (Html.BeginForm("Create_WhatIf", "Appointment", FormMethod.Post))
{
@Html.HiddenFor(m=>m.AppointmentId)
<div class="col-md-10">
<h3>What would you like to do if a common appointment with all participants cannot be made after the first invitation?</h3>
<div class='well'>
<div class="form-group">
<div class="input-group">
@Html.RadioButtonFor(m => m.RepeatTillCommonDateIsFound,Model.RepeatTillCommonDateIsFound)
@Html.LabelFor(m => m.RepeatTillCommonDateIsFound)
</div>
<div id="RepeatTimes">
@Html.EditorFor(m=>m.RepeatTimes)
</div>
<div class="input-group">
@Html.RadioButtonFor(m => m.RepeatOnce,Model.RepeatOnce)
@Html.LabelFor(m => m.RepeatOnce)
</div>
<div class="input-group">
@Html.RadioButtonFor(m => m.NoRepeat,Model.NoRepeat)
@Html.LabelFor(m => m.NoRepeat)
</div>
</div>
</div>
</div>
<div class="col-md-10">
<h3>What would you like to do if a common appointment cannot be made in the end?</h3>
<div class='well'>
<div class="form-group">
<div class="input-group">
@Html.RadioButtonFor(m => m.CancelInvitation,Model.CancelInvitation)
@Html.LabelFor(m => m.CancelInvitation)
</div>
<div class="input-group">
@Html.RadioButtonFor(m => m.OwnerFirstAvailableCommon,Model.OwnerFirstAvailableCommon)
@Html.LabelFor(m => m.OwnerFirstAvailableCommon)
</div>
<div class="input-group">
@Html.RadioButtonFor(m => m.FirstAvailableCommon,Model.FirstAvailableCommon)
@Html.LabelFor(m => m.FirstAvailableCommon)
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input class="btn btn-default" value="<<Previous" />
<input type="submit" class="btn btn-default" value="Next>>" />
</div>
</div>
}
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/bundles/jqueryval")
这是当前在浏览器中呈现的视图 问题
(好吧,问题下方的编辑器是您可以忽略的。) 为什么默认选择所有单选按钮?为了确保每个问题只能选择 1 个单选按钮,我应该怎么做?
【问题讨论】:
标签: c# jquery asp.net-mvc radio-button