首页 > 其他分享 >@Html.Partial vs @Html.Action

@Html.Partial vs @Html.Action

时间:2023-05-09 14:13:53浏览次数:52  
标签:partial Html Action Partial model view

@Html.Partial and @Html.Action are both used in ASP.NET MVC to include reusable content in a view.

@Html.Partial renders a partial view directly. It's useful for rendering small and simple views, like a header or footer.

@Html.Action is used to render a view through a controller action. It's useful when you need to perform some logic or data retrieval before rendering the view.

Here's a brief comparison between the two:

  • @Html.Partial directly renders the partial view. @Html.Action calls an action method which in turn renders the view.

  • @Html.Partial is generally used to render small, reusable views like header, footer, etc. @Html.Action is used to render complex views that require some logic or data retrieval.

  • @Html.Partial is faster than @Html.Action because it doesn't involve a round-trip to the server.

  • @Html.Partial can only render a partial view, while @Html.Action can render a full view with layout.

Overall, you should use @Html.Partial for simple and small views and @Html.Action for complex views that require some logic or data retrieval.

 

MVC Html.Partial or Html.Action

回答1

Here are what I consider my guidelines on using Html.Action or Html.Partial

Html.Partial

  1. Use Html.Partial when you are rendering static content or,
  2. If you are going to pass data from the ViewModel that is being sent to the main view

Html.Action

  1. Use Html.Action when you actually need to retrieve additional data from the server to populate the partial view

Basically, if is static, use Html.Partial(). If dynamic, model independent data, use Html.Action(). There are probably more scenarios, but this will give you a good idea of where/how to go. Html.RenderPartial() and Html.RenderAction() are interchangeable for the similarly named functions above.

 

回答2

tml.Partial: Render a Partial View without hitting a controller action first. See: Partial Views

Html.Action Call a Controller Action, which may return a view/partial view (or may not, it could throw an HttpNotFound or return Json, or other things).

Does your menu require a view model, or is it static?

If it's a static menu, Html.Partial will fit your needs. Place the static menu content inside the partial view and call Html.Partial where you want it to render.

If the menu is being generated off a view model, you can use either Html.Partial or Html.Action:

Chaining the view model using Html.Partial:

public class MenuViewModel {
    // menu data goes here
}

public class GeneralViewModel : MenuViewModel {
    // general view info goes here
}

public ActionResult Index() {
    return View(new GeneralViewModel());
}

// View Code
@model GeneralViewModel
<div>@Html.Partial("_partialName", model)</div>

Here we pass a complete view model to the view and the view calls Partial and hands its model off to the partial view.

** Separating the model using Html.Action:**

public ActionResult Index() {
    return View(new GeneralViewModel());
}

public ActionResult MenuView() {
    return PartialView(new MenuViewModel());
}

// View Code
@model GeneralViewModel
<div>@Html.Action("MenuView")</div>

Here the view calls the controller action MenuView which creates a new view model and passes it to the partial.

 

 

标签:partial,Html,Action,Partial,model,view
From: https://www.cnblogs.com/chucklu/p/17384674.html

相关文章

  • html 跳转到新的网址
    更新window.location.href后面的值即可文件名为index.html<!DOCTYPEhtml><html><head><metacharset='utf-8'><metahttp-equiv='X-UA-Compatible'content='IE=edge'><title>页面标题</title>......
  • partial
    partial部分类,当两个类的类名一致,且都加了关键字partial,在编译时是当作一个类partialclassForm1//partial部分类publicpartialclassForm1:Form右键快捷菜单把你当前想要的右键菜单选中,默认无事件:在.Net平台上,我们所用的控件都封装了很多的事件。事件就是对用......
  • HTML与CSS规范
    此规范主要实现目标:代码一致性和最佳实践.降低代码的成本同时改善多人协作的效率。同时遵守最佳实践,确保页面得到最佳优化和高效代码。命名规则项目命名全部采用小写方式,以下划线分隔。例如:my_project_name目录命名参照项目命名规则常用css/images/js/stylesJS文件命......
  • html5 svg 圆形进度条
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>HTML5中的SVG属性实现圆形进度条效果</title><style>#a{color:red;}</style></head><body><......
  • HTML5-创建HTML文档
    下述内容主要讲述了《HTML5权威指南》第7章关于“创建HTML文档”。HTML5中的一个主要变化是:将元素的语义与元素对其内容呈现结果的影响分开。从原理上讲这合乎情理。HTML元素负责文档内容的结构和含义,内容的呈现则由应用于元素上的CSS样式控制。下面介绍最基础的HTML元素:文档元素和......
  • 小米商城主页展示HTML+CSS
    大佬们呀,花了好几天的时间总算是看着页面展示可以了,求赐教!小米商城主页,对大佬来说肯定简单爆了,我抄写了好久呀,总是有一点点的小问题,还搞不明白主要是一个静态的小米商城页面,HTML前端代码不复杂,一堆的div和ul、li就没问题了,主要是css代码搞不懂导致显示不正常话不多说,直接上代码......
  • kaggle:Tweet Sentiment Extraction,提升分数的几种主要方法
    1.背景这次分享的是kaggle比赛TweetSentimentExtraction的第13名次的方法,作者主要用针对数据集进行前置处理或者是后置处理,具体原文:https://www.kaggle.com/c/tweet-sentiment-extraction/discussion/1595052.主要方法2.1RobertaModel这次比赛,大部分都是用了Roberta模型,其基础......
  • 使用BERT的两层encoder实现tweet sentiment extraction
    文章目录使用BERT的两层encoder实现tweetsentimentextraction1.前言1.1Required1.2分析给定的数据2.模型构造2.1数据清洗2.2模型结构使用BERT的两层encoder实现tweetsentimentextractionTweetsentimentextraction是kaggle的一个比赛,这个代码主要是想尝试利用BERT模型实......
  • Struts2----中使用ValueStack、ActionContext、ServletContext、request、session等
     声明:本文参考Struts2版本为2.3.1.2,内容仅供参考,限于笔者水平有限,难免有所疏漏,望您能友善指出。本文发表于ITEYE,谢绝转载。1.ValueStack  ValueStack在中文版的《Struts2深入浅出》一书中译作“值栈”。其本身数据结构是一个栈,使用者可以把一些对象(又称作bean)存入值栈中,然后......
  • Could not create ActionMapper: WebWork will *not* work!
    CouldnotcreateActionMapper:WebWorkwill*not*work!解决方法:将webwork.properties的webwork.objectFactory=springwebwork.objectFactory.spring.autoWire=name 两行去掉就可以了......