在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 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM