MVC UI 用于 ASP.NET 复选框对于问题

本文关键字:于问题 问题 复选框 UI 用于 ASP NET MVC | 更新日期: 2023-09-27 18:30:37

我对MVC Telerik UI有一个大问题 ASP.NET我正在尝试为布尔字段设置一个复选框。我知道我们有两个输入字段可以在未触摸框时返回值 false。当我不接触 CBox 时,我按预期得到值"false"。当我选中该框时,我也得到 false,因为 CBOx 返回一个字符串 = "true,false",这使得无法直接转换为布尔值。

视图

public class role
{
    public string role_name { get; set; }
    public bool add_school { get; set; }
}

控制器

    public ActionResult test()
    {
        return View();
    }
    [HttpPost]
    public async Task<ActionResult> test(Models.role role)
    {
        var z = Request["cb_addschool"];
        var x = 1;
        return RedirectToAction("Index");
    }

视图

    @model Models.role
    @using (Html.BeginForm("test", "Home", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
    {
        @Html.AntiForgeryToken()
        <h2>Add a New Role</h2>
        <hr />
        @Html.ValidationSummary("", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(m => m.role_name, new { @class = "col-md-1 control-label" })
            <div class="col-md-10">
                @Html.TextBoxFor(m => m.role_name, new { @class = "form-control form-control-big" })
            </div>
        </div>
        <div class="form-group">
        @Html.Kendo().CheckBoxFor(m=>m.add_school).Name("cb_addschool").Label("Add School")
        </div>
        <div class="form-group">
            <div class="col-md-offset-3 col-md-9">
                <input type="submit" class="btn btn-login" value="Register" />
            </div>
        </div>
    }

拜托,有什么帮助吗?

MVC UI 用于 ASP.NET 复选框对于问题

从操作方法中删除此代码:

var z = Request["cb_addschool"];

你的榜样中有这个价值。所以在这种情况下这是没有意义的。

从剑道复选框中删除此属性:

.Name("cb_addschool")

您不必需要它(如果没有它,属性将被正确绑定)。

小提示:如果您使用的是 Kendo - 请使用 Kendo().TextBoxFor 方法而不是 @Html.TextBoxFor(或将"k-textbox"类添加到您的 TextBoxFor - 它将使用 Kendo CSS 样式)。

下面是一个示例:

@(Html.Kendo().TextBoxFor(model => model.role_name)
.HtmlAttributes(new { placeholder = "Select role", @class = "form-control form-control-big" })
)