How MVC forms Bind properties or variables of type bool

Source: Internet
Author: User

First look at a set of code:

View Code:

@model mvctest.controllers.test@{Layout = null;}<!DOCTYPE HTML><HTML><Head>    <Metaname= "Viewport"content= "Width=device-width" />    <title>Form submission</title></Head>    <Body>        <formMethod= "POST"Action="">            <inputname= "Tester"value= "@Model. Tester" />            <inputtype= "Submit" />        </form>    </Body></HTML>

Model class:

 Public class test{     publicboolgetset;}

Controller code:

 Public ActionResult Formpost () {      new  Test ();       true ;       return View (model);}

The question is what is the value of the input property named Tester in view? It is believed that many people will think it is true, but the result is value.

Also put Model.tester in other HTML tags, the value will correspond to its attribute one by one.

<id= "id"  name= "name"></span  >

So how to solve this problem? Take a look at the following code:

<name= "Tester"  value= "@Model. Tester.tostring ()"/> 

The string interpretation for the bool type conversion is "to convert the value of this instance to its equivalent string representation (" True "or" False "), as

It is not difficult to understand that Model.Tester.ToString returns TRUE. But why not just return true? This could be razor.

This is how the picture is handled. There is no such situation in ASP.

Here to thank the Park Friends (519740105) for their help, the problem was resolved.

How MVC forms Bind properties or variables of type bool

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.