Auxiliary methods, models, view data

Source: Internet
Author: User

EG1:

If you want to display prices in a form, you can use the following controller code:

 Public ActionResult Edit (int  ID) {    viewbag.price=10.0;     return  View ();}

In the corresponding view, using the values in ViewBag to name the TextBox helper method, you can implement a text box that renders the price of the display:

@Html. TextBox ("price")

The following HTML markup will be generated

<input id= "Price" name= "Price" type= "text" value= "ten"/>

EG2:

When the helper methods view the contents of the ViewData, they can also see the object properties in them.

Controller code:

 Public ActionResult Edit (int  ID) {    new album{price=one};     return  View ();}

To use code in a view:

@Html. TextBox ("Album.price")

The rendered HTML tag is as follows:

<id= "Album_price"  name= "Album.price"  type= " Text "  value=" one "/>

If there is no value in ViewData that matches "Album.price", the secondary method attempts to find a value that matches the part name (Album) that precedes the first point. In other words, it is to find an object of type album. The secondary method then estimates the remainder of the name (price) and finds the corresponding value.

The id attribute value of the rendered INPUT element uses an underscore instead of a point because it is illegal to include a point in the id attribute.

EG3:

The TextBox helper method relies on strongly typed view data to work well.

Controller code:

 Public ActionResult Edit (int  ID) {    varnew album{price=12.0m};     return  View (album);}

Page code:

@Html. TextBox ("price")

Rendering the generated HTML markup

<id= ' price ' name= ' price '  type= ' text '  Value= "12.0"/>

If you want to avoid finding data automatically, you can provide a display value to the form that shows that it is necessary to provide a method that is worthwhile.

Controller code:

 Public ActionResult Edit (int  ID) {    var album = StareDB.Albums.Single (a=>a.albumid==ID );     New SelectList (StoreDB.Genres.OrderBy (g=>g.name),                     "genreid"." Name " , album. Genreid);     return  View (album);}

Code in the View:

@Html. TextBox ("Title", Model.title)

Helper methods before finding a strongly typed model object, the ViewBag is first viewed, and if there is a title in the ViewBag, it receives the title in ViewBag

Auxiliary methods, models, view data

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.