MVC 4 Custom template for bool (razor)

后端 未结 4 1562
夕颜
夕颜 2021-02-07 09:37

I am using the twitter bootstrap framework, so to get the EditorFor and DisplayFor methods to output what I need, I created custom templates for each of the types like string, t

4条回答
  •  青春惊慌失措
    2021-02-07 10:15

    Reading the responses so far, I started wondering about how the model object was being initialized. So this is rather weird, but I found the answer. Hopefully someone can explain the weirdness. Might be how MVC initializes a model object if you don't specify one.

    The default MVC Internet template has the following for the Login action:

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

    That gives the error. Changing it to the following however, fixes the problem:

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

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

提交回复
热议问题