I recently learned ASP. net mvc. Although I just typed a simple routine, I still got some results.
The most basic thing in MVC is: model, view, and controller ). The relationship between the three must be very clear, especially inProgramIn running, how do these three work together. Simply put, the Controller determines the behavior, the model stores the data, and the View displays the results (or simply displays the data) after the behavior is processed. In fact, this is much more complicated than this.
The Controller is composed of a series of operations. These operations generally return an actionresult, And the actionresult includes JSON and view. Let's talk about view first.
The view () method is very simple to use. By default, the overload version without parameters returns a view with the same name as the operation, as shown below:
PublicShoppingcontroller: controller {PublicActionresult index (){ReturnView ();}}
The returned result is/views/shopping/index. cshtml. A good MVC programming method is to put related things together, for example, it is more convenient to put the views related to shoppingcontroller in a separate folder shopping.
However, you can also specify the view displayed for this operation, such:
ReturnView ("Shopping");
Instead of displaying the default index. cshtml, It is shopping. cshtml.
What's amazing is that we can also pass the model to the corresponding view through view.
Like this:
PublicActionresult index (IntID ){ReturnView (ID );}
You can pass the model item system. int32 to the View index. cshtml. Magic! Int Is a model item! This is not surprising, because in MVC, the so-called model is just some C # type, and system. int32 is a type, so it is normal to process it as a model item.
The purpose of passing a model item is to use this model item, as shown below:
@ Model system. int32...<H2> @ Model </H2>
We can use this model item in the view, but the model must be imported through @ (equivalent to C # using, Java import ). The namespace must be specified for the import model, and the namespace of int32 is system. int32. However, if it is a custom type, the namespace of this type must be completely specified. The @ (razor syntax) is also used, and the @ model is the value of the model ID. Of course, if it is a custom type and contains a method type, we can call the relevant method through @ model. + method.
When I write a routine, I find a problem with the following error message: the object reference is not set to an object instance (nullreference ). At this time, we should carefully check whether there are imported models, whether there are input parameters in the operation, and whether the input model items are null.
It is very important to pass a model to a view. For example, if we want to display the state of a model in the view, we must pass the model of the current State to the view, these are all done through simple parameter passing actions. Maybe we can reference this model in the view. Indeed, we can use jquery to obtain the relevant state of the model through httpcontext, such:
Function Handleupdate (){ // Load and deserialize the returned JSON data VaR JSON = Context. get_data (); VaR Data = SYS. serialization. javascriptserializer. deserialize (JSON ); // Update the page elements If (Data. itemcount = 0 ) {$ ( "# Row-" + data. deleteid). fadeout ("slow");} Else {$ ( "# Item-count-" + Data. deleteid). Text (data. itemcount);} $ ( "# Cart-total" ). Text (data. carttotal); $ ( "# Update-message" ). Text (data. Message); $ ( "# Cart-status"). Text ("cart (" + data. cartcount + ")" );}
View SectionCodeFor example:
@ Foreach ( VaR Item In Model. cartitems ){ <Tr id = "row-@item.RecordId"> <TD> @ Html. actionlink (item. album. title, "Details", "Store ", New {Id = item. albumid }, Null ) </TD> <TD> @ Item. album. Price </TD> <TD id = "item-count-@item.RecordId"> @ Item. Count </TD> <a href = "#" class = "removelink" data-id = "@ item. recordid "> remove from cart </a> </TD> </tr> } <Tr> <TD> Total </TD> <TD id = "cart-total"> @ Model. carttotal </TD> </tr>
As you can see, jquery is executed through $. For example, $ ("# cart-total") is used to obtain the HTML element whose ID is cart-total (well, this is similar to Javascript, but jquery is also an open source JavaScript library ). $ Is not a symbol. It is a function name and a function alias, such:
$ (Function(){...});
It is the alias of the anonymous function in the parameter. When passing an anonymous function as a parameter, it is equivalent to assuming that the function is executed immediately after the HTML Document Object Model is built in the browser. This assumption is advantageous to prevent scripts in the function that conflict with the DOM (Document Object Model), resulting in Dom loading failure.
If you think $ is just a function alias, then it is wrong. Its role is far from the same. For example, if the above element ID is obtained, the selector is used. We can further specify the child element in the relevant element:
$ ("# Cart-total IMG ")
In this way, the IMG element in the HTML element whose ID is cart-total is specified. For JavaScript, several functions are required.
The magic of $ is that when it acts as a selector, it returns a wrapped set that contains zero or multiple matching elements ), we can also continue to call related methods on this encapsulation set (these methods must be dependent on the encapsulation set method, of course, they are also returned to the encapsulation set ). This is the method chain.
I don't know if this is a problem with vs2012. Sometimes, when I pass model items through view (), the model items are empty. What's terrible is that my model is passed without errors, and the view is also a strong view of the model. Even if debugging fails, I will shut down and restart the system, then all the problems disappear! Shutdown and restart are not a solution, but because I want to go to bed at that time! If you have encountered a similar problem, you can also consider the same as me: first shut down and sleep, and then look at the situation.