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

本文详细介绍了ASP.NET MVC中的辅助方法,包括@Html.Raw()直接输出HTML内容、@Html.ActionLink生成链接、@Url.Action返回URL、@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则是用于页面中嵌入部分视图/用户控件/动态内容,具体区别参见下表:

 

转载于:https://www.cnblogs.com/sccd/p/5580140.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值