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