The building blocks of Asp.net MVC: How to modularize the Visual View logic and use the Controller component in Asp.net MVC

Source: Internet
Author: User
I am arui, the first time I write Technology Article I have no experience, and I would like to make everyone more considerate about incoherent speech.
Let's talk about the first topic: Use usercontrol to assemble your view in Asp.net MVC.
In Asp.net MVC, you can use usercontrols
To simplify or modularize your view logic.
Of course, it can be used as in traditional webform.
<Uc1: News id = control1 runat = server listcount = "3"/>
However, using this method has two disadvantages:
1. An annoying Declaration must be added at the top of the ASPX page. For the view, the more concise the better.
2. Another disadvantage is that the passed parameters must be of the basic data volume type.
Object. What should we do?

Fortunately, Asp.net MVC provides a series of renderusercontrol methods.
You can also pass any parameters and construct the initial attribute value without making a sound at the top of Aspx.

Ming. Very clean and refreshing.
<% = Html. renderusercontrol ("~ /Usercontrols/newslist. ascx ") %>
<% = Html. renderusercontrol

("~ /Usercontrols/newslist. ascx ", viewdata. News) %>

The third parameter uses an anonymous method to construct the default attribute of ascx.
<% = Html. renderusercontrol

("~ /Usercontrols/newslist. ascx ", viewdata. News, New

{Newid = 2}) %>
Now, usercontrol can cut and assemble the view layer logic,
However, no matter how it is assembled, It passively accepts controller calls.
Is there any problem? Please think about the following scenarios, which is also a problem I have previously asked about producer creation:
--------------->
There is a scenario where I don't know how to implement it using MVC
For example, in a news system, the homepage reads the latest n news titles in each news category,
Under webform, I encapsulate the homepage News list into ascx and customize two attributes:

News category and display number. In this case, set

Set these two attributes to OK. That is to say, if you change the homepage aspx, you can only obtain

.
In MVC, the Data fetch part and page are completely separated. The above logic requires the artist

Modify the page.ProgramModify controler?
The answer to this question is concise: Use rendercomponent.
<-------------------------
This introduces the second topic:
The use of web components. This Web component not only contains views like ascx, but more importantly

He has his own Controller behavior. This component can be directly called by other views,
Although it is like a tongue twister, it can be understood in combination with the above scenario.
The Controller directly called by the view layer must inherit from componentcontroller,
This is different from the common controller. Similar to this:
Public class newscomponentcontroller: componentcontroller
{
Public void contentlist ()
{
Renderview ("newslist", new
Vstarapp. Dal. Service. contentinfo_service (). getalldata ());
}
}
The View File Path of this componentcontroller render is also specified, yes

~ /Components/yourcontrollername/views/viewname. aspx
For newscomponentcontroller
~ /Components/newscomponent/views/newslist. aspx
Therefore, the view path must be correct.

The rest of the work is to call the Controller component in other views.
<% = Html. rendercomponent <cmsweb. controllers. newscomponentcontroller> (F => F. contentlist () %>

Okay, everything is OK.

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.