February 10, 2014
RenderPartial vs RenderAction vs Partial vs Action in MVC Razor
นี้เป็นวิธีการในการ render partal view ใน MVC Razor. หาย ๆ นักพัฒนาคิดความสับสนว่าใช้งานอย่างไร ระหว่าง RenderPartial หรือ RenderAction หรือ Partial หรือ Action helper methods จากการ render ตัว partial view. ในบทความนี้ จะอธิบายความแตกต่างระว่าง @Html.RenderPartial, @Html.RenderAction, @Html.Partial และ @Html.Action
@Html.RenderPartial
1. method นี้ ผลลัพท์จะเป็นการ directly written แบบ Http response stream หมายความว่ามันถูกใช้เหมือนกับ TextWriter object เช่นเดียวกับการใช้ในา webpage/template ปัจจุบัน
2. ตัว method จะ return void
3. ใช้งานง่ายโดยไม่ต้องสร้างตัว Action ใด ๆ ใน Controller
4. method @Html.RenderPartial มีประโยชน์ในการใช้เมื่อแสดง data ใน partial view in already in the corresponding view model.
@{ Html.RenderPartial("_Comments"); }
5. method นี้ เร็วกว่า method @Html.Partial โดย result ถูกส่งข้อความกับเป็น response stream ทำให้ทำงานได้เร็ว
@Html.RenderAction
1. ผลลัพท์ของ method จะส่งกลับเป็น HTTP response stream หมายถึงมันใช้วิธีเดียวกับ TextWriter object เช่นเดียวกับ webpage/template
2. จะ method นี้, เราจำเป็นต้องสร้าง child action สำหรับ render ตัว partial view.
3. method @Html.RenderAction มีประโยชน์เมื่อแสดงข้อมูล partial view ที่เป็นอิสระโดยจาก view model"
@Html.RenderAction("Category", "Home")
4. method นี้เป็นตัวเลือกที่ดีเมื่อคุณต้องการ cache ตัว partial view.
5. method นี้เร็วกว่า Action method เมื่อ result เป็นแบบตอบกลับแบบ HTTP response stream เพื่อให้ทำงานรวดเร็ว
@Html.Partial
1. render ตัว partial view เช่นเดียวกับ HTML-encoded string.
2. method นี้ result สามารถถูกเก็บในตัวแปล, ตั่งแต่มัน return ค่า string type.
3. ง่ายต่อการใช้งานและไม่ต้องสร้าง action ใด ๆ
4. partial method คือมีประโยชน์ในาการใช้เมื่อแสดงข้อมูลใน partial view ที่สอดคล้องกับ view model
@Html.Partial("_comments")
@Html.Action
1. render ตัว partial view แบบ HtmlString.
2. จาก method นี้, คุณต้องสร้าง child action จากการ render ต้ว partial view.
3. result ของ method สามารพถูกเป็นไว้ในตัวแปล, เมื่อมัน return ค่า string
4. Action method ถูกใช้เมื่อการแสดงข้อมูลใน partial view ไม่ขึ้นกับ view model
@Html.Action("Category", "Home")
5. method นี้เป็นตัวเลือกเมื่อคุณต้องการ cache ตัว partial view.
tanslate from: http://www.dotnet-tricks.com/Tutorial/mvc/Q8V2130113-RenderPartial-vs-RenderAction-vs-Partial-vs-Action-in-MVC-Razor.html
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment