Use of MVC3----auxiliary methods 2

Source: Internet
Author: User

=============================== 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

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.