ASP. NET 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 I want to change An ASPX master page, I only need to specify the master to be used when return is in the action: I have two master files, while/views/home/index. 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:}

In the custom control renderpartialasp. Net MVC, if you want to use a custom control, you must use helper instead of using it like webform. For example, we create a CT. ascx with the following 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 <% = %> is not used to display but execute the statement.) The final result is that the custom control can be placed in view/shared in addition to the caller's directory. 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. Renderactionok for example I want to have an ascx with independent logic. First I need to reference Microsoft. Web. MVC (http://aspnet.codeplex.com/Release/ProjectReleases.aspx? Releaseid = 24471) web. config/configuration/system. add <add namespace = "Microsoft. web. MVC "/> is the same action as creating an 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:}

Show: After submitting: Check the folder again. The file has been uploaded successfully. This article was written for my big cousin because he is a tireless ASP instructor, I hope you can use the tutorials I have written. Thank you for your support.

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.