Asp.net MVC 2 pass values through viewdata. Model

Source: Internet
Author: User

1. Create a model: Member. CS in the models folder.

Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. web; </P> <p> namespace mvcapplication2.models <br/> {<br/> public class member <br/>{< br/> // <summary> <br/> /// attributes of the model, the name does not need to correspond to the database field, but it must be the same as the database field for ease of reading. <Br/> // </Summary> <br/> Public string name {Get; Set ;}</P> <p> Public String age {Get; set ;} </P> <p> Public String sex {Get; Set ;}< br/>}< br/>}

Member can be created in accountmodel. CS in the default models, or a class file. The code above creates a new file,

2. Create another action: memberindex ()

If the newly created class file is in the original Mo file, memberindex should also be created in the corresponding controller. If it is a newly created class file, create the action in the corresponding controller as follows:

Public actionresult memberindex () <br/>{< br/> mvcapplication2.models. member M = new mvcapplication2.models. member (); <br/> M. name = "James"; <br/> M. sex = "male"; <br/> M. age = "24"; <br/> return view (m); <br/>}

3. Create a view page file for the action:

1. Name: Same as memberindex and action

 

Generate the page after adding:

<Fieldset>
<Legend> fields </legend>

<Div class = "display-label"> name: <% = html. encode (model. Name) %> </div>
<Div class = "display-field"> </div>

<Div class = "display-label"> Age: <% = html. encode (model. Age) %> </div>
<Div class = "display-field"> </div>

<Div class = "display-label"> sex: <% = html. encode (model. Sex) %> </div>
<Div class = "display-field"> </div>

</Fieldset>
<P>
<% = Html. actionlink ("edit", "edit", new {/* id = model. primarykey */}) %> |
<% = Html. actionlink ("back to list", "Index") %>
</P>

Run, http: // localhost: 4116/admin/memberindex

Next article: Asp.net MVC three actionresult returns other values

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.