MVC 4 Custom Template for bool (razor)

I use the twitter bootstrap framework, so in order to get the EditorFor and DisplayFor methods to display what I need, I created custom templates for each of the types, such as string, text, password, etc. For my login page, I need the RememberMe bool function, as before, I created the following template and entered it in Boolean.cshtml:

@model bool <div class="control-group"> <div class="controls"> <label class="checkbox"> @Html.CheckBoxFor(m => m, new {@class = "checkbox"}) @Html.LabelFor(m => m) </label> </div> </div> 

Pretty simple, but when I use:

 @Html.EditorFor(m => m.RememberMe) 

I get an exception saying that the value based on the bass cannot be null:

 The model item passed into the dictionary is null, but this dictionary requires a non-null model item of type 'System.Boolean'. 

What am I missing? Looks like it should be straight. The field of the model object is as follows:

 [Display(Name = "Remember me?")] public bool RememberMe { get; set; } 

Thank.

UPDATE:. It seems that ultimately it is a matter of creating an empty view model object and passing it to the view instead of letting MVC create it on its own.

+16
c # asp.net-mvc razor
Jan 20 '13 at 15:22
source share
4 answers

I wouldn’t do that. If the value can be null, I would make sure your editor template has a nullable boolean value as the model type. So your editor template (in Views \ Shared \ EditorTemplates \ Boolean.cshtml) will look like this:

 @model Boolean? @Html.CheckBox("", Model.HasValue && Model.Value) 

And then in the razor of your main look, you can:

 <div class="control-group"> <div class="controls"> <label class="checkbox"> @Html.EditorFor(m => m, new {@class = "checkbox"}) @Html.LabelFor(m => m) </label> </div> </div> 
+18
Mar 06 '13 at 19:08
source share

After reading the answers so far, I began to wonder how the model object is initialized. So it is rather strange, but I found the answer. Hope someone can explain the oddity. Perhaps MVC initializes the model object if you do not specify it.

As a default login template, the MVC Internet template is used:

 [AllowAnonymous] public ActionResult Login(string returnUrl) { ViewBag.ReturnUrl = returnUrl; return View(); } 

This gives an error. However, changing it to the following resolves the problem:

 [AllowAnonymous] public ActionResult Login(string returnUrl) { var loginModel = new LoginModel(); ViewBag.ReturnUrl = returnUrl; return View(loginModel); } 

Thus, this answers the question of how to solve the problem, but still leaves an unresolved reason. Maybe because MVC creates an instance of an object in a different way, say, with reflection or something?

0
Jan 21 '13 at 4:10
source share

You can change your model so that it accepts zero values ​​as yes / no

 public bool? RememberMe { get; set; } 
0
Jul 29 '16 at 16:07
source share

You must initialize the RememberMe bool value inside the constructor, as shown below.

Remember that using uninitialized variables in C # is not allowed.

 using System.ComponentModel; public class ClassName { public ClassName () { RememberMe = false; } [DefaultValue(false)] [Display(Name = "Remember me?")] public bool RememberMe { get; set; } } 

For More Information About Checking Default Value Table

Hope this helps you.

-one
Jan 20 '13 at 16:42
source share



All Articles