ASP.NET MVC 中可重用内容框数据?

发布于 2024-08-12 03:34:51 字数 308 浏览 5 评论 0原文

如果我为包含标题、图像和内容的框创建 PartialView,那么在不使用数据库的情况下存储内容的最佳方法是什么?

示例: TurboTax

我怀疑侧边栏中的框内容是否存储在数据库中,而是为了制作可重用的代码在 PartialView 中创建结构并填充内容区域将是有益的。我可以创建一个 PartialView 并将模型从父控制器传递到 PartialView,但是如果我想在另一个页面上使用相同的框,我将不得不复制和粘贴相同的内容。

If I create a PartialView for a box that holds a header, image and content what is the best way to store the content without using a database?

Example: TurboTax

I doubt the content for the boxes in the sidebar are stored in a database but to make reusable code it would be beneficial to create the structure in a PartialView and populate the content areas. I can create a PartialView and pass a Model from the parent Controller to the PartialView but then I would be stuck copying and pasting that same content if I wanted to use the same box on another page.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

指尖凝香 2024-08-19 03:34:51

对于固定内容,您可能需要考虑在文件系统中使用 XML+XSLT 甚至 HTML 片段并简单地呈现它们。 HtmlHelper 方法可能比部分视图 - Html.RenderXml() 或 Html.Include() 更有意义。这些视图和部分视图之间的唯一真正区别是,由于没有任何替换,因此不会调用视图引擎。我用我的隐私政策以及条款和条件来做这种事情。我当然会考虑将这些缓存起来。

如果这些确实是模板,而您只是替换内容,那么我认为部分视图效果很好,我会再次考虑将数据放入数据库中,如果我发现性能受到影响,可能会使用缓存。您可以将其与前者结合使用 - 比如说将您的图像/xml 保留在文件系统中,并将指向它们的指针保留在数据库中,以便您知道在部分中选择哪些。

For fixed content you might want to think about using XML+XSLT or even HTML snippets in the file system and simply rendering them. An HtmlHelper method might make more sense for this than a partial view - Html.RenderXml() or Html.Include(). The only real difference between these and partial views is that the view engine isn't invoked since there aren't any substitutions. I do this sort of thing with my privacy policy and terms and conditions. I'd certainly consider keeping these cached.

If these really are templates and you are just substituting content, then I think the partial view works well and I would consider putting the data in a database, again, maybe using caching if I found that performance suffered. You could use this in combination with the former -- say keep your images/xml in the file system and a pointer to them in the database so you know which ones to pick in the partial.

青春如此纠结 2024-08-19 03:34:51

将数据传递到在许多地方使用的分部视图可以通过多种方式完成:

为所有模型创建基本模型类。在基类中定义 PartialModel 属性,该属性将保存部分视图的模型(如果使用有许多部分视图,则可能有很多模型)。现在您可以在控制器操作中填充 PartialModel 属性,但为了使代码更可重用,您可以创建自己的操作过滤器,它将在执行操作方法之后(但在模型传递到视图之前)插入部分视图数据

public class PartialViewModelAttribute : ActionFilterAttribute
{
  public override void OnActionExecuted(ActionExecutedContext filterContext)
  {
     BaseViewModel model;

     if (filterContext.Controller.ViewData.Model == null)
     {
          model = new BaseViewModel();
          filterContext.Controller.ViewData.Model = model;
     }
     else
     {
        model = filterContext.Controller.ViewData.Model as BaseViewModel;
     }

     model.PartialModel = new PartialModel(...)   // Partial model initialization
     base.OnActionExecuted(filterContext);
  }
}

然后您可以像这样使用它:

[PartialViewModel]
public ActionResult Index()
{
  //...
}

另一种选择:您可以为所有控制器创建 BaseController 类,并在基本控制器初始化时创建 PartialModel。然后 PartialModel 可以存储在 ViewData[] 字典中。因为在视图中使用 ViewData 字典是不好的,所以在 HtmlHelper 上创建扩展方法,例如:

public static PartialModel GetPartialModel(this HtmlHelper helper)
{
   return helper.viewContext.ViewData["PartialModel"] as PartialModel
}

所以你可以通过这种方式获取模型:

<% Html.RenderPartial("MyPartial", Html.GetPartialModel()); %>

Passing data to partial view that is used in many places can be done in many ways:

Create base model class for all your models. In base class define PartialModel property which will be holding model for partial view (there may be many of them if use have many partial views). Now you can populate the PartialModel property in controller action, but to make code more reusable you can create your own Action Filter which will insert the partial view data just after the action method is executed (but before the model is passed to the view)

public class PartialViewModelAttribute : ActionFilterAttribute
{
  public override void OnActionExecuted(ActionExecutedContext filterContext)
  {
     BaseViewModel model;

     if (filterContext.Controller.ViewData.Model == null)
     {
          model = new BaseViewModel();
          filterContext.Controller.ViewData.Model = model;
     }
     else
     {
        model = filterContext.Controller.ViewData.Model as BaseViewModel;
     }

     model.PartialModel = new PartialModel(...)   // Partial model initialization
     base.OnActionExecuted(filterContext);
  }
}

Then you can use it like:

[PartialViewModel]
public ActionResult Index()
{
  //...
}

Another option: you can create BaseController class for all your controllers and create PartialModel on base controller initialization. Then PartialModel can be stored in ViewData[] dictionary. Because using ViewData dictionary in views is bad, create extension method on HtmlHelper like:

public static PartialModel GetPartialModel(this HtmlHelper helper)
{
   return helper.viewContext.ViewData["PartialModel"] as PartialModel
}

So you could obtaint the model this way:

<% Html.RenderPartial("MyPartial", Html.GetPartialModel()); %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文