ASP. net mvc view (3), asp. netmvc View

Source: Internet
Author: User

ASP. net mvc view (3), asp. netmvc View
ASP. NET MVCView(3.)Preface

In the previous article, I gave a rough explanation of the Razor view engine and view type. I am sure you have some knowledge about the view itself. This article will use the IoC framework to inject dependencies into the view implementation, this process will give you a better understanding of the view. At last, we will briefly introduce how the custom view helper is defined and used.

 

ASP. NET MVC View
  • Custom view Engine
  • RazorView engine Execution Process
  • RazorView dependency injection and custom view guides
  • Use of segment and segment views
  • RazorSyntax and view guides

 

Razor view dependency Injection

First, let's take a look at defining functional interface specifications and default implementations for implementing dependency injection. Example code 1-1.

Code 1-1

using System.Web.Mvc;using Ninject;namespace MvcApplication.Models{    public interface IStringManage    {        MvcHtmlString CombinationString(string strPar1, string strPar2);    }    public class DefaultStringManage : IStringManage    {        public MvcHtmlString CombinationString(string strPar1, string strPar2)        {            return new MvcHtmlString(strPar1 + strPar2);        }    }}

The CombinationString () method is defined in the IStringManage type to concatenate the values of the two string types. The DefaultStringManage type is implemented by default.

Next we will define the type of the view to be inherited at the time of compilation. Example code 1-2

Code 1-2

using System.Web.Mvc;using Ninject;namespace MvcApplication.Models{    public abstract class StringManageView : WebViewPage    {        [Inject]        public IStringManage StringManage { get; set; }    }}

This definition won't be problematic at first, because the cshtml View File inherits from the WebViewPage type during compilation. Now we need to make the cshtml view inherit the StringManageView type, therefore, StringmanageView must be inherited from WebViewPage. Because WebViewPage is of the abstract type and we do not want to implement anything, we need to define it as an abstract type. In the StringManageView type, I have defined StringMange, an attribute of the IStringManage type, and used the Inject feature in the IoC framework to describe it. In this way, IoC can be used for property dependency injection during compilation.

Next let's take a look at the view code. The view code here still references the most widely used example in the previous section. Code 1-3 and code 1-4

Code 1-3

public ActionResult Index(List<Product> model){    ViewBag.StrPar1 = "This is";    ViewBag.StrPar2 = "ViewIoCCase";    return View(model);           }

Code 1-4

@inherits MvcApplication.Models.StringManageView@{    ViewBag.Title = "Index";}

The Code definition in the Controller method is correct. In code 1-4, that is, the Index view definition, the @ inherits command is used to make the View File inherit from a certain type during compilation, the StringManage attribute is used in the following operations, and the method is also called. It seems that there is no problem here, but there is a problem when it is used here, because foreach is used to traverse the Model, when we define StringManageView, there is no constraint on the Model type, and the Controller method also needs to pass the List <Product> type to the view, which leads to a conflict, figure 1.

Figure 1

In this case, we only need to modify the definition in code 1-2 so that the Model type is deterministic during compilation rather than the object type. For details, refer to code 1-5.

Code 1-5

public abstract class StringManageView : WebViewPage<dynamic>{   [Inject]   public IStringManage StringManage { get; set; }}

Right, let StringManageView implement the generic WebViewPage. in the previous article, the base class of the View Graph is also defined as this, not to say that what is defined in the above Code 1-2 is incorrect, the definition is correct, but the view of the application is not suitable, because the Index view itself needs to perform some operations on the Model, you do not want to modify the dependency injection function added to the view to cause this error. If you use it in a common view, there is no problem.

The above are all defined. Next we need to implement the custom IDependencyResolver type, in order to bind the functional modules we need to perform dependency injection to IoC, code 1-6.

Code 1-6

using Ninject;using System.Web.Mvc;namespace MvcApplication.CustomDependencyResolver{    public class NinjectDependencyResolver:IDependencyResolver    {        private IKernel Kernel;        public NinjectDependencyResolver()        {            Kernel = new StandardKernel();            AddBinding();        }        private void AddBinding()        {            Kernel.Bind<Models.IStringManage>().To<Models.DefaultStringManage>();        }        public object GetService(Type serviceType)        {            return this.Kernel.TryGet(serviceType);        }        public IEnumerable<object> GetServices(Type serviceType)        {            return this.Kernel.GetAll(serviceType);        }    }}

I have not explained much about code 1-6 and explained almost similar injection encapsulation types in the Controller activation section.

Finally, in the Application_Start () method of the Global. asax file, add the Model binder and NinjectDependencyResolver type to the MVC Framework, code 1-7

Code 1-7

ModelBinders.Binders.Add(typeof(List<Product>), new CustomListModelBinder());DependencyResolver.SetResolver(new CustomDependencyResolver.NinjectDependencyResolver());

The final result is shown in figure 2.

Figure 2

 

Custom view helper

In fact, the custom view helper is the definition of the extension method. First, let's look at the definition. The implementation function is the same as code 1-1, code 2-1

using System.Web.Mvc;namespace MvcApplication.CustomHtmlHelper{    public static class MyCustomHtmlHelper    {        public static MvcHtmlString CombinationString(this HtmlHelper htmlHelper, string strPar1, string strPar2)        {            return new MvcHtmlString(strPar1 + strPar2);        }    }}

Code 2-1 is a type like custom view helper. Of course, this is just a simple example. Now we need to use it in the view, we must first add the namespace of the custom view helper to the Web in the Views file. in Config, code 2-2.

Code 2-2

 <system.web.webPages.razor>    

Then, reference the namespace in which the extension method is located in the view. After this configuration, you can use the custom view helper in the view. Code 2-3.

Code 2-3

@inherits MvcApplication.Models.StringManageView@using MvcApplication.CustomHtmlHelper@{    ViewBag.Title = "Index";}

Finally, let's take a look at result 3.

Figure 3

 

 

 

Author: Jin Yuan

Source: http://www.cnblogs.com/jin-yuan/

The copyright of this article is shared by the author and the blog Park. You are welcome to reprint this article. However, you must keep this statement without the author's consent and go to the Article Page.


Aspnet mvc3 (C #) restricts the entry of some view URLs in the address bar to directly view some views. How can this problem be solved?

Check the Reference URL (
HttpRequest. UrlReferrer
) Is it linked from the page you want? If not, it will jump to or display forbidden information. If yes, it will display normal pages. Hope to help you.

ASPNET MVC30 RAZOR mode returns multiple sets bound to the VIEW

No one will answer you here. If you don't even know what MVC is, you don't understand it. You just need to know that there is no DataSet in MVC3,
MVC advocates that all data is transmitted using models, that is, mvc m (model ).
I can go to many tutorials online, and I will do some exercises for a few hours.

Related Article

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.