In the same place:
Partialview, Editorfor, and displayfor can all be used to implement the public part of the page, and other pages can be referenced according to the requirements.
The difference:
Partialview is handled from the point of view of the page , so the main page and Partialview generally use the same model, which enables editing of the corresponding fields in the Partialview If Partialview is using the model's sub-model in the main page, then only the display of the model for the partial view can be implemented.
Specific references are: @Html. Partial ("~/views/shared/_productpartial.cshtml")
Editorfor and Displayfor are from the model angle to deal with, at this time Displayfor implementation is the sub-model of the display function, editorfor to achieve the sub-model editing function.
The specific citation is: @Html. editorfor (M = m.products) @Html. displayfor (M = m.products)
Here's a simple example to illustrate
1): Engineering code structure
2): Model structure
Public classOrdermodel { Public stringOrderId {Get;Set; } Public stringOrdername {Get;Set; } PublicList<productmodel> Products {Get;Set; } } Public classProductModel { Public stringProductId {Get;Set; } Public stringProductName {Get;Set; } Public DoublePrice {Get;Set; } }
3): Partialview
3.1): Action Index
Public actionresult Index () { = getmyorder (); return View (MyOrder); } [HttpPost] Public ActionResult Index (Ordermodel model) { = 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> <inputtype= "Submit"value= "Order" />}
3.3): _productpartial.cshtml
@model MvcApplication2.Models.OrderModel<Div>@foreach (var item in model.products) {<Div> <label>ProductName:</label> <inputvalue= "@item. ProductName " /> </Div> <Div> <label>Price:</label> <inputvalue= "@item. Price " /> </Div> }</Div>
3.4): Run result
4): Editorfor
4.1): Action indexforeditor
Public actionresult indexforeditor () { = getmyorder (); return View (MyOrder); } [HttpPost] Public ActionResult Indexforeditor (Ordermodel model) { = 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> <inputtype= "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): Run result
5): Displayfor
5.1): Action indexfordisplay
Public actionresult Indexfordisplay () { = getmyorder (); return View (MyOrder); } [HttpPost] Public ActionResult Indexfordisplay (Ordermodel model) { = 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> <inputtype= "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): Run result
6): Public method
PrivateOrdermodel Getmyorder () {Ordermodel order=NewOrdermodel (); Order. OrderId="o001"; Order. Ordername="Order001"; List<ProductModel> productlist =NewList<productmodel>(); ProductModel product001=NewProductModel (); product001. ProductId="P001"; product001. ProductName="Product001"; product001. Price=1000.00; ProductModel product002=NewProductModel (); product002. ProductId="P002"; product002. ProductName="Product002"; product002. Price=2000.00; Productlist.add (product001); Productlist.add (product002); Order. Products=productlist; returnorder; }
For more detailed information, see: Http://stackoverflow.com/questions/5037580/asp-net-mvc-3-partial-vs-display-template-vs-editor-template