=============================== helper methods, models, view data
--------------------a simple helper method
Controller code:
Public ActionResult Edit (ID)
{
viewbag.price=10.0;
return View ();
}
View:
@Html. TextBox ("Price")
Render markup:
<input id= "Price" name= "Price" type= "text" value= "ten" >
--------------------View property values in an object
Controller code:
Public ActionResult Edit (int id)
{
Viewbag.price=new album{price=11};
return View ();
}
View:
@Html. TextBox ("Album.price")
Render markup:
<input id= "Album_price" name= "Album.price" type= "text" value= "one" >
--------------------strongly typed view data
Controller code:
Public ActionResult Edit (int id)
{
var album=new album{price=12.0m};
Return View (album);
}
View:
@Html. TextBox ("Price")
Render markup:
<input id= "Album_price" name= "Album.price" type= "text" value= "12.0" >
--------------------provide a displayed value to the form helper method
Controller code:
Public ActionResult Edit (int id)
{
var album=new album{price=12.0m};
Return View (album);
}
View:
@model MvcApplication3.Models.Album
@Html. TextBox ("Price", Model.st_name)
Render markup:
<input id= "Album_price" name= "Album.price" type= "text" value= "12.0" >
--------------------strongly typed view data (lambda instead of string)
Controller code:
Public ActionResult Edit (int id)
{
var album=new album{price=12.0m};
Return View (album);
}
View:
@model MvcApplication3.Models.Album
@Html. Textboxfor (M=>m.price)
Render markup:
<input id= "Album_price" name= "Album.price" type= "text" value= "12.0" >
--------------------helper methods and model metadata
View Code:
[DisplayName ("School type name")]
public virtual string St_name{get;set;}
View:
@Html. lable ("St_name")
Render markup:
<lable for= "St_name" > School type name </lable>
--------------------template-Assisted methods
(Editorfor is powerful than textboxfor, it can change the generated HTML via data annotations)
View Code:
[Stringlength (10)] //Enter up to 10 characters in length
[DataType (Datatype.multilinetext)]//Multiline text
public virtual string St_name{get;set;}
View:
@Html. editorfor (model = model.st_name)
@Html. validationmessagefor (model = model.st_name, "Maximum length of 10 characters")
Render markup:
<textarea name= "St_name" class= "Text-box multi-line" id= "St_name" data-val= "true" data-val-length-max= "10" Data-val-length= "Field st_name must be a string with a maximum length of 10. "></textarea>
This article is from the "program Ape's Home--hunter" blog, please be sure to keep this source http://962410314.blog.51cto.com/7563109/1597386
Use of MVC3----auxiliary methods 2