ASP. NET MVC view (c)

Source: Internet
Author: User

ASP . NET MVC View ( three )

Objective

the previous article for Razor View engine and the type of view to do a general explanation, presumably we have some understanding of the views themselves, this article will use the IoC Framework view of the implementation of dependency injection, in this process will let you know more views, Finally, we will briefly explain how the Custom view helper is defined and used.


ASP . NET MVC View

l Custom View engine

l Razor View Engine Execution procedure

l Razor Dependency Injection, Custom view helper for views

l segmentation, use of partial views

l Razor syntax, view helper



Razor Dependency Injection of views

first, let's look at the functional interface specification and the default implementation to define the implementation of dependency injection, example code 1-1.

Code 1-1

Using system.web.mvc;using Ninject; namespace mvcapplication.models{public interface Istringmanage {mvchtmlstringcombinationstring (string strpa    R1, string strPar2); } public class Defaultstringmanage:istringmanage {public mvchtmlstring combinationstring (String strPar1, St        RINGSTRPAR2) {returnnew mvchtmlstring (strpar1+ strPar2); }    }}


in the The Combinationstring () method is defined in the Istringmanage type, which is used to stitch together the values of two string types,and the Defaultstringmanage type is the default implementation. There's not much to say here.

Let's define the type of inheritance that will be implemented at compile time view, example code 1-2

Code 1-2

Using system.web.mvc;using Ninject; Namespace mvcapplication.models{public abstract class Stringmanageview:webviewpage {[Inject] Publi    C Istringmanage stringmanage {get; set;} }}


This definition is not problematic at first because the cshtml view file is inherited from the Webviewpage type at compile time , and now we want to make the cshtml view inherit the type Stringmanageview, so Stringmanageview must inherit from webviewpage, because webviewpage is an abstract type, And we don't want to do anything, so to define it as an abstract type, in the Stringmanageview type, I define the property Stringmange of the istringmanage type,and use the the Inject attribute in the IOC framework describes it, so that the compilation is a dependency injection that can be implemented by the IOC to implement the attribute.

Let's take a look at the view code, where the view Code refers to one of the most used examples in the previous space, code 1-3 and code 1-4

Code 1-3

Public Actionresultindex (list<product>model) {viewbag.strpar1 = "thisis";    VIEWBAG.STRPAR2 = "Viewioccase";           Returnview (model); }


Code 1-4

@inherits mvcapplication.models.stringmanageview@{viewbag.title = "Index";} 


The code definition for the Controller Method section is no problem, in code 1-4, which is the definition of the Index view , to make the view file inherit from a type at compile time by @inherits directive. As well as the use of the Stringmanage property in the following, and also called the method, here does not seem to be a problem, but put in here with a problem, because it uses a foreach to traverse the Model , when we define Stringmanageview, there is no constraint on the Model type, and the Controller method needs to be list<product> Type is passed to the view, this causes a conflict, figure 1.

Figure 1

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/40/3E/wKiom1POTfTjiK1vAAFnLrzmTGY506.jpg "title=" Viewengine1.png "alt=" Wkiom1potftjik1vaafnlrzmtgy506.jpg "/>

In this case we just need to modify the definition in code 1-2 so that the Model type is deterministic at compile time rather than object type, see Code 1-5

Code 1-5

public abstract class stringmanageview:webviewpage<dynamic>{[Inject] public istringmanage stringmanage {get; Set }}


Yes, let stringmanageview implements the generic webviewpage Yes, the base class for the view in the last space is also defined, not the code 1-2 definition is wrong, definition is not wrong, only the application view is inappropriate, because index model do some things, and do not want to because of the dependency injection function added to the view, so it will cause this error, It's no problem if you put it in a normal view.

These are all defined above, we need to implement the custom Idependencyresolver type, which is designed to bind the function modules we need to do dependency injection to IoC , the Code 1-6.

Code 1-6

using ninject;using system.web.mvc; namespace mvcapplication.customdependencyresolver{     public class NinjectDependencyResolver:IDependencyResolver     {        privateIKernel Kernel;         publicninjectdependencyresolver ()         {             kernel = newstandardkernel () ;             addbinding ();         }         privatevoid addbinding ()         {             Kernel.Bind<Models.IStringManage> (). To<models.defaultstringmanage> ();        }         public object getservice ( Typeservicetype)         {             returnthis. Kernel.tryget (servicetype);        }          public ienumerable<object>getservices (Type servicetype)          {             Returnthis. Kernel.getall (servicetype);         }    }}

for Code 1-6 There is no explanation for this, and in the Controller activation section, almost similar injection package types are explained.

in the end we Global.asax of the file Application_Start () method, you will Model Binders and Ninjectdependencyresolver type added in MVC Framework, the Code 1-7

Code 1-7

ModelBinders.Binders.Add (typeof (List<product>), New Customlistmodelbinder ());D Ependencyresolver.setresolver (Newcustomdependencyresolver.ninjectdependencyresolver ());

Finally, look at the results, figure 2.

Figure 2

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/40/3E/wKioL1POTzagPr9mAADcTdSZOnk179.jpg "title=" Viewengine2.png "alt=" Wkiol1potzagpr9maadctdszonk179.jpg "/>

Custom View Helper

in fact, the Custom view helper is the definition of the extension method, first we look at the definition, the implementation of the function is the same as the code 1-1 , code 2-1

Using SYSTEM.WEB.MVC; namespace mvcapplication.customhtmlhelper{public static class Mycustomhtmlhelper {public static Mvchtmlstri Ngcombinationstring (This htmlhelperhtmlhelper, string strPar1, String strPar2) {returnnew mvchtmlstring        (strpar1+ strPar2); }    }}


The code 2-1 Such a type is a custom view helper, of course, this is just a simple example, now we need to use it in the view, we have to first put this custom view helper in the namespace to add to the views In the file , in Web. config , code 2-2.

Code 2-2

  <system.web.webpages.razor>    


Then you refer to the namespace where the extension method is referenced in the view, This configuration allows you to use the view helper we just customized in the view, code 2-3.

Code 2-3

@inherits mvcapplication.models.stringmanageview@usingmvcapplication.customhtmlhelper@{ViewBag.Title = "Index";} 


Finally, let's look at the results . 3.

Figure 3

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/40/3F/wKiom1POTkuzE0MoAADpgwWbUWU099.jpg "title=" Viewengine3.png "alt=" Wkiom1potkuze0moaadpgwwbuwu099.jpg "/>


This article is from the "Jinyuan" blog, please be sure to keep this source http://jinyuan.blog.51cto.com/8854733/1441508

ASP. NET MVC view (c)

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.