• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ASP.NETMVCHtml.Partial/Html.RenderPartial/Html.Action/Html.RenderAction区别

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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则是用于页面中嵌入部分视图/用户控件/动态内容,具体区别参见下表:

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Asp.NetMvc使用Autofac实现依赖注入发布时间:2022-07-10
下一篇:
Asp.Net 上传大文件专题(1)--概述:上传大文件的难点发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap