我没有使用过单选按钮,而且我是MVC的新手。
我正在尝试从视图中获取所选单选按钮的值到控制器Post方法。所有其他元素值都会出现在单选按钮上。他们总是返回false
这是我的代码
查看
<label for="first" class="col-sm-3 control-label">Patient Name:</label>
<div class="col-sm-3">
<input type="text" class="form-control" id="first" placeholder="First" name="FirstName">
</div>
<div class="col-sm-2">
<input type="text" class="form-control" id="middle" placeholder="Middle" name="MiddleName">
</div>
<div class="col-sm-3">
<input type="text" class="form-control" id="last" placeholder="Last" name="LastName">
</div>
<div class="col-sm-1"></div>
</div>
<div class="form-group">
<label for="month" class="col-sm-3 control-label">DOB:</label>
<div class="col-sm-2">
@Html.DropDownList("MonthofBirth", Acc.Web_Code.viewmodels.Calendar.MonthsSelectList, "Month", new { @class = "form-control", name = "month", id = "month" })
</div>
<div class="col-sm-1">
@Html.DropDownList("DayofBirth", Acc.Web_Code.viewmodels.Calendar.DaysSelectList, "Day", new { @class = "form-control", name = "day", id = "day" })
</div>
<label class="radio-inline">
<input type="radio" name="Gender" id="male" value="Male"> Male
</label>
<label class="radio-inline">
<input type="radio" name="Gender" id="female" value="Female"> Female
</label>
发布方法
public ActionResult AddNewPatient(AddPView objAddPView)
我的模特是
public class AddPView
{
[Required]
public string FirstName { get; set; }
public string MiddleName { get; set; }
[Required]
public string LastName { get; set; }
public bool Gender { get; set; }
[Required]
public string YearofBirth { get; set; }
[Required]
public string MonthofBirth { get; set; }
[Required]
public string DayofBirth { get; set; }
[Required]
public string Address1 { get; set; }
[Required]
public string City { get; set; }
[Required]
public string State { get; set; }
public string Zip { get; set; }
[Required]
public string Phone { get; set; }
}
在视图中尝试了很多变化,但每件事都会返回false
答案 0 :(得分:0)
性别是模型中的一个bool,因此值也需要bool:
<label class="radio-inline">
<input type="radio" name="Gender" id="male" value="true"> Male
</label>
<label class="radio-inline">
<input type="radio" name="Gender" id="female" value="false"> Female
</label>
答案 1 :(得分:-2)
尝试这样的事情。我从我们的项目中复制了它。
<div class="form-group">
@Html.LabelFor(model => model.Type, new {@class = "control-label col-md-2"})
<div class="col-md-8">
@Html.RadioButtonFor(model => model.Type, ChangeType.Change) Änderung
@Html.RadioButtonFor(model => model.Type, ChangeType.New) Neuanlage
@Html.ValidationMessageFor(model => model.Type, "", new {@class = "text-danger"})
</div>
</div>