ASP.NET MVC Html.Partial/Html.RenderPartial/Html.Action/Html.RenderAction区别

1. @Html.Raw() 方法输出带有html标签的字符串:

<div style="margin:10px 0px 0px;border:1px;border-color:red;border-style:dotted;">
<h4>Title:Html.Raw</h4>
Html.Raw:@Html.Raw("<div style=\'background-color:red \'>HelloWorld!</div>")
</div>
View Code

  自我分析:直接输出HTML内容!

2. @html.ActionLink生成一个<a href=".."></a>标记:

<div style="margin:10px 0px 0px;border:1px;border-color:red;border-style:dotted;">
        <h4>Title:Html.ActionLink</h4>
        Example Of Usage: Html.ActionLink("linkText","ActionName","ControlName",new { id = "911"},new{ target="_blank"})
        <br />
        @Html.ActionLink("Click Me!", "GetView", "Test", new { id = " 11" }, new { target = "_blank" })
    </div>
View Code

 自我分析:直接输出一个<a></a>标签,即返回一个/Controller/Action/形式的Link!

3. @Url.Action

<div style="margin:10px 0px 0px;border:1px;border-color:purple;border-style:dotted;">
        <h4>Title:Url.Action</h4>
        Example Of Usage:Url.Action("ActionName","ControlName",new { id = "911" })
        <br />
        @Url.Action("GetView", "Test", new { id = "12" })
    </div>
View Code

  自我分析:返回一个/Controller/Action形式的url,这应该是对Url.Action最形象的说明了吧!

4. @Html.Action

View:

<div style="margin:10px 0px 0px;border:1px;border-color:blue;border-style:dotted;">
        <h4>Title:Html.Action</h4>
        Example Of Usage: Html.Action("ActionName", "ControlName")  PS.Invoke the Partial View
        <br />
        @Html.Action("GetMyPartialView", "Test")
    </div>
View Code

Action:

public ActionResult GetMyPartialView()
{
    EmployeeBusinessLayer bl = new EmployeeBusinessLayer();
    List<Employee> empList = bl.GetEmployeeList();
    EmployeeListViewModels empVMEmp = new EmployeeListViewModels();

    for (int i = 0; i < empList.Count; i++)
    {
        EmployeeViewModels newEmp = new EmployeeViewModels();
        newEmp.EmployeeName = empList[i].FirstName + " " + empList[i].LastName;

        if (empList[i].Salary > 1000)
        {
            newEmp.SalaryColor = "Red";
        }
        else
        {
            newEmp.SalaryColor = "Yellow";
        }
        newEmp.Salary = empList[i].Salary.ToString();
        empVMEmp.employeeList.Add(newEmp);
    }
    empVMEmp.UserName = "admin";
    return View("MyPartialView", empVMEmp);
}
View Code

 自我分析:加载公共部分的代码如当前登录用户在各个页面的信息显示,也可以理解为返回一个由/Controller/Action/构造的HTML文本内容并进行呈现。

5. @Html.RenderAction

View:

<div style="margin:10px 0px 0px;border:1px;border-color:orange;border-style:dotted;">
    <h4>Title:Html.RenderAction</h4>
    Usage Of Example:@{Html.RenderAction("PartialView Name","ControlName");}
    <br />
    @{Html.RenderAction("GetMyPartialView", "Test");}
</div>
View Code

Action:

public ActionResult GetMyPartialView()
{
    EmployeeBusinessLayer bl = new EmployeeBusinessLayer();
    List<Employee> empList = bl.GetEmployeeList();
    EmployeeListViewModels empVMEmp = new EmployeeListViewModels();

    for (int i = 0; i < empList.Count; i++)
    {
        EmployeeViewModels newEmp = new EmployeeViewModels();
        newEmp.EmployeeName = empList[i].FirstName + " " + empList[i].LastName;

        if (empList[i].Salary > 1000)
        {
            newEmp.SalaryColor = "Red";
        }
        else
        {
            newEmp.SalaryColor = "Yellow";
        }
        newEmp.Salary = empList[i].Salary.ToString();
        empVMEmp.employeeList.Add(newEmp);
    }
    empVMEmp.UserName = "admin";
    return View("MyPartialView", empVMEmp);
}
View Code

6.总结:@Html.ActionLink和@Url.Action都是为了实现页面跳转而用于生成链接用的。而Html.Partial/Html.RenderPartial/Html.Action/Html.RenderAction则是用于页面中嵌入部分视图/用户控件/动态内容,具体区别参见下表:

 

posted on 2016-07-31 23:58  Joye_Zhou  阅读(1266)  评论(0编辑  收藏  举报

导航