MVC3 partial View

Source: Internet
Author: User

When it comes to the partial view in MvC3, it's reminiscent of the ascx partial controls inside the WebForm, which makes it easier to control the partial view in the MVC era. MVC is divided into two major categories of partial views, namely Html.partial (Partialname), html.renderpartial (Partialname) and Html.action, Html.renderaction ();

(1) Html.partial (Partialname), html.renderpartial (Partialname)

These two methods we can specify any. cshtml file, not necessarily just a partial view. The view in MVC is the head, the body tag, you can define JS, you can specify the master page, but when we add a partial view with VS, we will see just some basic HTML tags, no head, body, but this does not mean that we can not add, So, partialname can be any view file. But it's a good idea to create a partial view, because we end up merging these partial views into one page, and if it's just the HTML tag of the partial view, the HTML tag will be clean in the overall page.

It is best not to define or reference CSS and JS in the partial view. If you need to be able to define or reference the overall page, because: the partial view file and the overall page path may not be in the same directory, will cause the CSS or JS file can not be found, may also make the overall page code confusion.

(2) html.action, html.renderaction ()

A view of the Action, renderaction Load method, executes the order of the Controller→model→view, and then returns the resulting page back to the original view.

● Partial, renderpartial Direct load View file contents

The following example shows the partial view

Example 1

Returns the action of a partial view

Public ActionResult GetUser () {    return Partialview ();}

In other foreground views, you need to write this

@Html. Partial ("GetUser") @{        html.renderpartial ("GetUser");    }

Example 2

The main view passes the model to the partial views

@Html. Partial you can give the data entity and data dictionary to the partial view, and then the partial view can be regenerated into a view by passing the data over the view.

namespace mvcstart.controllers{public    class Homecontroller:controller    {public        actionresult Index ()        {            Man_model man = new Man_model ();            Man. Id = 1;            Man. Name = "Zhang Fei";            Man. age = All;            return View (man);        Public ActionResult GetName (object o)        {            Man_model man = o as Man_model;            return Partialview (man);        }    }    public class Man_model    {public        int Id {get;  Set     Public        string Name {get; set;} public int Age {get; set;}}    }

View Code: index.cshtml

@model mvcstart.controllers.man_model@model.id@model.age@html.partial ("GetName", model);

Getname.cshtml


@model mvcstart.controllers.man_model<div style= "background-color:red" > @Model .name</div>

The generated HTML code is:


Example 3

Methods such as Html.renderaction () also support incoming parameters

@Html. Renderaction ("Left_nav", "Nav", new{parentid = 3});

@{html.renderaction ("Datagrid", "DataList", new {area = "Common"});    

parameter values can be obtained by adding a parameter actionresult (int parentid) directly inside the action. Area is intercepted by MVC and analyzed for cross-region invocation.



MVC3 partial View

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.