A simple summary of the MVC project

Source: Internet
Author: User

can not be said to be fortunate, is catching up with the company to develop a small MVC project, so the task is only my own commitment, in this project I just thoroughly feel their ignorance of the knowledge of MVC, think that contact with the fur feel can do the project, as long as the mind to move the brain thinking can complete a complete small project, In fact, it is not so, or the actual combat is the most effective bar, before also said to learn that, but from the beginning of the North drift seems to be no quiet down to learn it, always feel there are countless things to do, but always busy, but to think about it, in fact, there is nothing to do, Not because you do not want to learn, is a stable heart, so the opportunity to come, the job has been completed, everything is stable, the next day may repeat the same rhythm, work, knock Code, work ... That's the rhythm of life that I've had over three months to pick up my studies to fill my life. Well, it must be a lot of friends are big God, but I still have I need to remember the east that, or summarize some I want to remember it.

one. Controller to the view to pass the parameter

<1> using viewdate to transfer parameters

                   viewdata["name"] = request.form["name"];                   @Html. Encode (viewdata["Name"])

In fact, the above viewdate is to get the value to set a key, and then in the view to get this button again, this is the simplest kind of bar, in fact, here said viewdate the problem of value, I think I encountered a situation, It's a question that you didn't know and didn't dare to try, that is, one of the actions in the controller gets a value, but what do you want to do with the other action of the current controller? I simply created the session in the project, although this is not a good way, but it is still very convenient to use Oh, hehe, as follows:

                   Model. Name = request.form["name"];                   session["Name"] = model. Name;

so we get the value and then we create a session, and then we get the value of the session.

Another thing to say is that our viewdate is not just passing a string value to view, it can even pass a list to the view as follows:

                   list<student> Stumodel = Stubll.getallstudentbyid (ID);                   viewdata["Stumodel"] = Stumodel;

                               above is the value we create in the controller, and the next simple summary of how to get the value in view:

 @{list<student> Stumodel = Viewdata[ " stumodel  ] as  list<student                   > > 0  ) {  foreach  (var  item 
     
      in 
       Stumodel) { <span class  = stuid   " > @item. Name</span> 

so we can get a collection in view that we want to traverse, in fact, why don't we return the model entity class, that's why we use Viewdate to return a collection, only one model in a view, So in the last resort time we can only use other methods, hey.
In addition to a simple summary of a list set, how do we traverse an array if it is stored?

@{list<string[]> Stumodel = viewdata["Stumodel"] aslist<string[]>;} @if (Stumodel.count>0)                    {                      foreach(string[] StuinchStumodel) {                          for(inti =0; I < Stu. Length; i++)                         {                           <spanclass="Id"href="#">&rsaquo; @stu [1]</span> <spanclass="Name"hidden>&rsaquo; @stu [3]</span> <spanclass="Gender"hidden>&rsaquo; @stu [4]</span>                         }                       }                    }

the above summary of the list in the array traversal is mainly because the previous days to contact the project of the workflow, has been useless to this reason it, hey, it is easy to forget, here remember to traverse time plus @.

Two. The controller returns how the model should refer to how to use

only learn not to practice is a big pit, in fact, no practice is nothing to learn, until I started to do the project I just know that I did not know the model, that time is an accident ah.

First we return to model time, we need to return model in the controller, and then reference and iterate through the data in the view, the example in the controller is as follows:

                  Public actionresult Index ()                 {                   List<Student> stumodel = Adbll.getallstumodel (ID);                    return View (models);                 }

a simple summary of what we should write about in view.

                 @using Demo.model;                 @model List<Student>                   foreach (var in model)                  {                     <td > @Html. displayfor (ModelItem = Item. Name) </td>                  }

Well, today is temporarily summed up here, hey, to the Dragon Boat Festival, the pro are all good fun ha, I wish the Dragon Boat Festival happy, a see dumpling love Oh, 0.0

A simple summary of the MVC project

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.