在Asp.Net MVC中PartialView与EditorFor和DisplayFor的区别
相同之处:
PartialView, EditorFor 和 DisplayFor 都可以用作来实现页面的公共部分,其他页面可以根据需求来引用。
不同之处:
PartialView 是从Page的角度来处理,因此主 Page 和 PartialView 一般用相同的Model,这样做可以实现PartialView中所对应字段的编辑功能;如果PartialView使用的是主Page中Model的子Model,那么只能实现Partial View所对应Model的显示功能。
具体的引用方式为: @Html.Partial("~/Views/Shared/_ProductPartial.cshtml") @Html.Partial("~/Views/Shared/_ProductPartial.cshtml", Model.Products) @Html.RenderPartial("~/Views/Shared/_ProductPartial.cshtml")
EditorFor和DisplayFor是从Model的角度来处理,此时DisplayFor实现的是子Model的显示功能,EditorFor实现的是子Model的编辑功能。
具体的引用方式为: @Html.EditorFor(m => m.Products) @Html.DisplayFor(m => m.Products)
下面将用一个简单的例子加以说明
1): 工程代码结构

2):Model 结构
public class OrderModel
{
public string OrderId { get; set; }
public string OrderName { get; set; }
public List<ProductModel> Products { get; set; }
} public class ProductModel
{
public string ProductId { get; set; }
public string ProductName { get; set; }
public double Price { get; set; }
}
3):PartialView
3.1):Action Index
public ActionResult Index()
{
OrderModel myOrder = GetMyOrder();
return View(myOrder);
} [HttpPost]
public ActionResult Index(OrderModel model)
{
OrderModel myOrder = GetMyOrder();
return View(myOrder);
}
3.2):Index.cshtml
@model MvcApplication2.Models.OrderModel
@{
ViewBag.Title = "Index";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div>
<label>OrderName: </label>
@Html.TextBoxFor(m => m.OrderName)
</div>
<div>
@Html.Partial("~/Views/Shared/_ProductPartial.cshtml")
</div>
<input type="submit" value="Order" />
}
3.3):_ProductPartial.cshtml
@model MvcApplication2.Models.OrderModel <div>
@foreach (var item in Model.Products)
{
<div>
<label>ProductName: </label>
<input value="@item.ProductName" />
</div> <div>
<label>Price: </label>
<input value="@item.Price" />
</div>
}
</div>
3.4):运行结果截图

4):EditorFor
4.1):Action IndexForEditor
public ActionResult IndexForEditor()
{
OrderModel myOrder = GetMyOrder();
return View(myOrder);
} [HttpPost]
public ActionResult IndexForEditor(OrderModel model)
{
OrderModel myOrder = GetMyOrder();
return View(myOrder);
}
4.2):IndexForEditor.cshtml
@model MvcApplication2.Models.OrderModel
@{
ViewBag.Title = "IndexForEditor";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div>
<label>OrderName: </label>
@Html.TextBoxFor(m => m.OrderName)
</div>
<div>
@Html.EditorFor(m => m.Products)
</div>
<input type="submit" value="Order" />
}
4.3):ProductModel.cshtml
@model MvcApplication2.Models.ProductModel <div>
@Html.LabelFor(m => m.ProductName)
@Html.TextBoxFor(m => m.ProductName)
</div>
<div>
@Html.LabelFor(m => m.Price)
@Html.TextBoxFor(m => m.Price)
</div>
4.4):运行结果截图

5):DisplayFor
5.1):Action IndexForDisplay
public ActionResult IndexForDisplay()
{
OrderModel myOrder = GetMyOrder();
return View(myOrder);
} [HttpPost]
public ActionResult IndexForDisplay(OrderModel model)
{
OrderModel myOrder = GetMyOrder();
return View(myOrder);
}
5.2):IndexForDisplay.cshtml
@model MvcApplication2.Models.OrderModel
@{
ViewBag.Title = "IndexForDisplay";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div>
<label>OrderName: </label>
@Html.TextBoxFor(m => m.OrderName)
</div>
<div>
@Html.DisplayFor(m => m.Products)
</div>
<input type="submit" value="Order" />
}
5.3):ProductModel.cshtml
@model MvcApplication2.Models.ProductModel <div>
@Html.LabelFor(m => m.ProductName)
@Html.DisplayTextFor(m => m.ProductName)
</div>
<div>
@Html.LabelFor(m => m.Price)
@Html.DisplayTextFor(m => m.Price)
</div>
5.4):运行结果截图

6):公共方法
private OrderModel GetMyOrder()
{
OrderModel order = new OrderModel();
order.OrderId = "o001";
order.OrderName = "Order001"; List<ProductModel> productList = new List<ProductModel>();
ProductModel product001 = new ProductModel();
product001.ProductId = "p001";
product001.ProductName = "Product001";
product001.Price = 1000.00;
ProductModel product002 = new ProductModel();
product002.ProductId = "p002";
product002.ProductName = "Product002";
product002.Price = 2000.00;
productList.Add(product001);
productList.Add(product002); order.Products = productList; return order;
}
更多详细信息请看: http://stackoverflow.com/questions/5037580/asp-net-mvc-3-partial-vs-display-template-vs-editor-template
在Asp.Net MVC中PartialView与EditorFor和DisplayFor的区别的更多相关文章
- ASP.NET MVC中viewData、viewBag和templateData的区别
在MVC3开始,视图数据可以通过ViewBag属性访问,在MVC2中则是使用ViewData.MVC3中保留了ViewData的使用.ViewBag是动态类型(dynamic),ViewData是一个 ...
- 在asp.net mvc中使用PartialView返回部分HTML段
问题链接: MVC如何实现异步调用输出HTML页面 该问题是个常见的 case, 故写篇文章用于提示新人. 在asp.net mvc中返回View时使用的是ViewResult,它继承自ViewRes ...
- [转]在asp.net mvc中使用PartialView返回部分HTML段
本文转自:http://blog.csdn.net/sandy945/article/details/6307750 问题链接: MVC如何实现异步调用输出HTML页面 该问题是个常见的 case, ...
- 《Entity Framework 6 Recipes》中文翻译系列 (20) -----第四章 ASP.NET MVC中使用实体框架之在MVC中构建一个CRUD示例
翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 第四章 ASP.NET MVC中使用实体框架 ASP.NET是一个免费的Web框架 ...
- 在 ASP.NET MVC 中使用 HTML Helpers 的那些事
在 ASP.NET MVC 中使用 HTML Helpers 方法,可以返回得到标准的 HTML 标签,就像 <input>.<button> 或者 <img> 等 ...
- Asp.net Mvc中利用ValidationAttribute实现xss过滤
在网站开发中,需要注意的一个问题就是防范XSS攻击,Asp.net mvc中已经自动为我们提供了这个功能.用户提交数据时时,在生成Action参数的过程中asp.net会对用户提交的数据进行验证,一旦 ...
- asp.net mvc 中的部分视图
使用方法:@Html.Action(action, controller)加载局部页面.例如在模板页中使用:@Html.Action("Contact", "Compan ...
- Asp.net MVC 中Controller返回值类型ActionResult
[Asp.net MVC中Controller返回值类型] 在mvc中所有的controller类都必须使用"Controller"后缀来命名并且对Action也有一定的要求: 必 ...
- Asp.net mvc 中View 的呈现(二)
[toc] 上一节介绍了 Asp.net mvc 中除 ViewResult 外的所有的 ActionResult,这一节介绍 ViewResult. ViewResultBase ViewResul ...
随机推荐
- ls常用选项总结
参考: http://billie66.github.io/TLCL/book/index.html ls - List directory contents 选项 长选项 描述 -a --all 列 ...
- svn: Commit failed (details follow): svn: Authorization failed
我的原因是我没有使用账户密码,匿名用户没有写权限,只有只读的权限 修改下svn配置文件中的anon-access=read为anon-access=write 还有一点要注意:选项前面不能留空格,必须 ...
- Linux释放内存空间
Linux服务器运行一段时间后,由于其内存管理机制,会将暂时不用的内存转为buff/cache,这样在程序使用到这一部分数据时,能够很快的取出,从而提高系统的运行效率,所以这也正是linux内存管理中 ...
- ACM/ICPC 之 四道MST-Prim解法(POJ1258-POJ1751-POJ2349-POJ3026)
四道MST,适合Prim解法,也可以作为MST练习题. 题意包括在代码中. POJ1258-Agri Net 水题 //Prim-没什么好说的 //接受一个邻接矩阵,求MST //Time:0Ms M ...
- percona-toolkit 之 【pt-summary】、【pt-mysql-summary】、【pt-config-diff】、【pt-variable-advisor】说明
摘要: 通过下面的这些命令在接触到新的数据库服务器的时候能更好更快的了解服务器和数据库的状况. 1:pt-summary:查看系统摘要报告 执行: pt-summary 打印出来的信息包括:CPU.内 ...
- 【leetcode】Flatten Binary Tree to Linked List (middle)
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...
- 【leetcode】Number of Islands(middle)
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- 【C语言】二维指针做形参
转自:http://hi.baidu.com/gpmzccqceabimqq/item/f499f057aa1520404eff208b 关键: 传入时强制类型转换 + 使用时自己手工寻址 今天写程序 ...
- 【linux】gcc命令
来源:http://man.linuxde.net/gcc 语法 gcc(选项)(参数) 选项 -o:指定生成的输出文件: -E:仅执行编译预处理: -S:将C代码转换为汇编代码: -wall:显示警 ...
- 20145213祁玮のJava课程总结
20145213のJava学习总结 每周学习笔记 1.第一周读书笔记 2.第二周读书笔记 3.第三周读书笔记 4.第四周读书笔记 5.第五周读书笔记 6.第六周读书笔记 7.第七周读书笔记 8.第八周 ...