I have a big problem with MVC Telerik UI for ASP.NET I am trying to get a checkbox up for a boolean field. I know we have two input fields to return the value false when the box is not touched. When I do not touch the CBox, I get the value 'false' as expected. When I check the box, I get false too because the CBOx is returning a string = "true,false" which makes it impossible to convert directly to bool.
View
public class role
{
public string role_name { get; set; }
public bool add_school { get; set; }
}
Controller
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");
}
View
@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>
}
Please, any help?
Remove this code from your action method:
var z = Request["cb_addschool"];
You have this value inside your role model. So this is pointless in this case.
Than remove this attribute from Kendo CheckBoxFor:
.Name("cb_addschool")
You don't have to need that (the property will be bound correctly without that).
Small hint: if you are using Kendo - use the Kendo().TextBoxFor
method instead of @Html.TextBoxFor
(or add "k-textbox
" class to your TextBoxFor - it will use Kendo CSS styles).
Here is an example:
@(Html.Kendo().TextBoxFor(model => model.role_name)
.HtmlAttributes(new { placeholder = "Select role", @class = "form-control form-control-big" })
)