asp.net-mvc 带有 MVC 剃刀的输入类型复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27494809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:20:39  来源:igfitidea点击:

Input type checkbox with MVC razor

asp.net-mvcrazorcheckboxviewmodelform-submit

提问by JoshYates1980

Why is the value of my checkbox not passed to my ViewModel?

为什么我的复选框的值没有传递给我的 ViewModel?

My View (I omitted input tags not relevant for this post):

我的观点(我省略了与这篇文章无关的输入标签):

@model Pro.WebUI.ViewModels.UserViewModel

@using (Html.BeginForm("ManageUsers", "Administration", FormMethod.Post, 
new { id = "request-form", @class = "form-horizontal" }))
{
 <div class="form-group">
  <label for="inputAuthorize" class="col-lg-2 control-label">Authorize</label>
   <div class="col-lg-8">
     <input type="checkbox" id="Authorized" name="Authorized" value="@Model.Authorized" />
   </div>
 </div>
 <div class="form-group">
  <div class="col-lg-10 col-lg-offset-2">
   <br /><br />
   <button type="submit" class="btn btn-primary">Submit Request</button>
   </div>
 </div>
 }

My ViewModel:

我的视图模型:

public class UserViewModel
{
    [Key]
    public string UserID { get; private set; }

    public string UserName { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public bool Authorized { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
    public string Notes { get; set; }
}

My Controller:

我的控制器:

 [HttpPost]
    public ActionResult ManageUsers(UserViewModel model)
    {
        if (ModelState.IsValid)
        {
            ProcurementUser obj = new ProcurementUser();
            obj.UserName = model.Email;
            obj.FirstName = model.FirstName;
            obj.LastName = model.LastName;
            obj.Email = model.Email;
            obj.Phone = model.Phone;
            obj.Authorized = model.Authorized;
            UserRepository.SaveUser(obj);
            //success message
        }
        return View(model);
    }

I did not include all input tags but when I step through the code without the checkbox, all values are passed. I looked at other checkbox questions on SOF but they mostly use the @Html.Checkbox or @Html.CheckboxFor. I would like to just use input type="checkbox"

我没有包含所有输入标签,但是当我在没有复选框的情况下单步执行代码时,所有值都会被传递。我查看了 SOF 上的其他复选框问题,但它们大多使用 @Html.Checkbox 或 @Html.CheckboxFor。我只想使用 input type="checkbox"

回答by Dush

If we need to use <input>filed instead of @Html.CheckboxFor, we can use "checked=\"checked\""syntax as in this code:

如果我们需要使用<input>归档而不是@Html.CheckboxFor,我们可以使用"checked=\"checked\""如下代码中的语法:

<input type="checkbox" id="Authorized" name="Authorized" value="true" @(Model.Authorized ? "checked=\"checked\"" : "") />

回答by Zhaph - Ben Duguid

As has been hinted at in the comments the issue you're having is that you're not really creating your checkbox correctly:

正如评论中所暗示的,您遇到的问题是您没有真正正确地创建复选框:

Assuming your model has Authorized = trueyour mark-up would be:

假设您的模型有Authorized = true您的标记将是:

<input type="checkbox" id="Authorized" name="Authorized" value="true" />

Similarly the falsestate would result in:

同样,false状态将导致:

<input type="checkbox" id="Authorized" name="Authorized" value="false" />

But these aren't "checked" checkboxes - they're still "unchecked", and need the checkedattribute setting:

但这些不是“选中”复选框 - 它们仍然“未选中”,并且需要checked属性设置:

<input type="checkbox" id="Authorized" name="Authorized" value="true" checked />

As Stephen points out - an unchecked checkbox will not send any data back to the server so that you don't get confused about which options where selected.

正如斯蒂芬指出的那样 - 未选中的复选框不会将任何数据发送回服务器,因此您不会对选择了哪些选项感到困惑。

Finally, as has also been noted, your <label>element is for an non-existent field looking for inputAuthorizeinstead of Authorized.

最后,正如也已经指出的,您的<label>元素是针对不存在的字段寻找inputAuthorize而不是Authorized.

All of these issues would be taken care of for you if you were to use the @Html.CheckboxForand @Html.LabelForhelper classes.

如果您要使用@Html.CheckboxFor@Html.LabelFor帮助类,所有这些问题都会为您解决。