ASP. net mvc key tutorial 11th back to the master page, user-defined controls, and file upload

Source: Internet
Author: User
ArticleDirectory
    • Renderpartial
    • Renderaction
Master page (master)

1. The master page is not related to the controller. The master page is only a view file, and no controller corresponds to it.

2. In fact, the aspx of the view in ASP. net mvc is not closely associated with the master page as in webform.

For example, if you want to replace An ASPX master page, you only need to specify the master to be used when returning in the action:

I have 2 master files, while/views/home/index. aspx is

If you want to use Site2 as the master, you only need to specify the mastername parameter in return view of the action:

 
1: PublicActionresult index (){

 
2:ReturnView (Null,"Site2");

 
3:}

Custom Control renderpartial

In ASP. net mvc, if you want to customize the control, you cannot reference it like webform, but use helper.

For example, we create a CT. ascx

Its content:

 
1:<% @ Control Language ="C #"Inherits ="System. Web. MVC. viewusercontrol"%>

 
2:I'm CT. ascx.

Then, call it in index. aspx.

 
1:<% Html. renderpartial ("CT"); %>

OK (note that the statement is not displayed but executed here)

The final result is:

 

In addition to the caller's directory, user-defined controls can also be placed in view/shared. In addition, this custom control is not supported by the Controller. It only extracts the view part for public use.

What should we do if the ascx we want to call has logic processing or database calling, that is, when the controller is required.

Renderaction

OK. For example, I want to have an ascx with independent logic.

Microsoft. Web. MVC (http://aspnet.codeplex.com/Release/ProjectReleases.aspx? Releaseid = 24471)

Web. config/configuration/system. Web/pages/namespaces add <add namespace = "Microsoft. Web. MVC"/>

It is the same as creating an action.

Action:

 
1: PublicActionresult ctaction (){

 
2:ReturnPartialview ();// Note that this is not a view

 
3:}

View (ctaction. ascx)

 
1:<% @ Control Language ="C #"Inherits ="System. Web. MVC. viewusercontrol"%>

 
2:

 
3:Show current action: <% =This. Viewcontext. routedata. Values ["Action"] %>

View (index. aspx ):

 
1:<%

2:Html. renderaction ("Ctaction","Home"); %>

OK, and the result is:

File Upload

The topic that has nothing to do with this article is File Upload. I will not explain it here,CodeIs the best language.

View:

 
1:<Form action ="<% = URL. Action ("Process") %>"Enctype ="Multipart/form-Data"Method ="Post">

2:<Input name ="Up1"Type ="File"/> <Input type ="Submit"/>

 
3:</Form>

Action (process ):

 
1: PublicActionresult process (httppostedfilebase up1)

 
2:{// The parameter name must be the same as the name.

 
3:Up1.saveas (server. mappath ("~ /"+ Up1.filename ));

 
4:ReturnContent (up1.filename );

 
5:}

 

 

 

Display:

After submission:

Check the folder. The file has been uploaded successfully:

 

So far, I think there is no foundation for ASP. NET MVC. If you want to learn more about ASP. NET MVC, please pay attention to the update of the Asp.net MVC Framework series at any time.

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.