【问题标题】:Not getting Radiobutton value in mvc没有在 mvc 中获取 Radiobutton 值
【发布时间】:2015-04-04 23:03:15
【问题描述】:

我很少使用单选按钮,而且我是 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

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4 radio-button


    【解决方案1】:

    Gender 是模型中的 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>
    

    【讨论】:

      【解决方案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>
      

      【讨论】:

      • 你能告诉我ChangeType.ChangeChangeType.New 是什么,因为我是新手,无法理解这一点。 @Codeman01101001
      • @user3665063,请忽略这个 - 它完全是垃圾,与您的问题无关
      • ChangeType 是一个枚举。你必须为你的问题使用真/假。
      猜你喜欢
      • 2018-09-19
      • 1970-01-01
      • 1970-01-01
      • 2017-04-06
      • 1970-01-01
      • 2011-09-12
      • 2017-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多