假设您在MVC中有一个名为Category的模型类(具有id和name字段)
有什么办法可以将IEnumberable<Catergory>
作为视图渲染模型返回到MVC视图文件中吗?
答案 0 :(得分:1)
您可以选择将数据传输到Sitecore中的MVC View。以下所有示例都来自社区MVC项目,可供您查看。可以在这里找到:
https://github.com/Sitecore-Community/sample-sitecore-mvc
要执行您要执行的操作,请在视图渲染中使用自定义模型。您需要创建一个继承自IRenderingModel的模型。使用IRenderingModel需要一个名为Initialize的函数。在Initialize中,您将创建构建IEnumberable所需的逻辑。如果您希望可以编辑类别的详细信息,则需要确保将模型字段作为HtmlString类型返回。
我想要注意的是,大多数开发人员不使用此方法,因为它不可测试。
public class Car : IRenderingModel
{
public HtmlString Make { get; set; }
public HtmlString Model { get; set; }
// Rendering represents the context rendering - this particular model is only ever used by View Renderings
public Sitecore.Mvc.Presentation.Rendering Rendering { get; set; }
// Item represents the rendering's datasource, and PageItem represents the context page
// These properties exist on Sitecore's own RenderingModel object
public Item Item { get; set; }
public Item PageItem { get; set; }
public void Initialize(Sitecore.Mvc.Presentation.Rendering rendering)
{
// Use the Rendering object passed in by Sitecore to set the datasource Item and context PageItem properties
Rendering = rendering;
Item = rendering.Item;
PageItem = PageContext.Current.Item;
// Set property values using FieldRenderer to ensure that the values are editable in the Page Editor
Make = new HtmlString(FieldRenderer.Render(Item, "Make"));
Model = new HtmlString(FieldRenderer.Render(Item, "Model"));
}
}
然后在/ sitecore / layout / Models中,您将为渲染创建一个新模型。在类型字段中,指定上面创建的模型。在示例中,它是MVC.Data.Models.Car。
最后,在视图渲染中,指定模型类并使用模型数据。
@model MVC.Data.Models.Car
<!-- To set the 'Background' parameter, go into the Page Editor > select 'Design' mode, click on the Featured Car component >
click More > click Edit component properties > Scroll down to the 'Parameters' section -->
<div class="highlight" style="background-color: #@Model.Rendering.Parameters["Background"]">
<h3>This week's featured car</h3>
<p><strong>Model:</strong> @Model.Model</p>
<p><strong>Make:</strong> @Model.Make</p>
</div>
在大多数情况下,当您拥有自定义数据时,我建议使用控制器渲染。它干净,可重复使用且可测试。
为您的渲染创建一个传统模型。
namespace MVC.Data.Models
{
/// <summary>
/// This light-weight model is used as an example of how you can isolate Sitecore-dependent code into a repository, which
/// means your models have no dependency on Sitecore and look much more like regular ASP.NET MVC models. See DriverController.cs
/// for usage.
/// </summary>
public class Driver
{
public HtmlString Name { get; set; }
public HtmlString Text { get; set; }
public HtmlString Image { get; set; }
}
}
在该示例中,作者创建了一个具有Driver模型的View Model,以及有关渲染的信息,如数据源项。
public class DriverViewModel
{
public Driver Driver { get; set; }
public string Background { get; set; }
public Item ContextItem { get; set; }
}
然后创建一个控制器/存储库来填充该模型。最后将它传递给渲染。
public ActionResult Featured()
{
var repository = _driverRepository;
var driver = repository.GetDriver();
var driverViewModel = repository.GetDriverViewModel(driver);
return View(driverViewModel);
}
@model MVC.Data.Models.DriverViewModel
<!-- To set the 'Background' parameter, go into the Page Editor > select 'Design' mode, click on the Featured Driver component >
click More > click Edit component properties > Scroll down to the 'Parameters' section -->
<div class="highlight" style="background: #@Model.Background">
<h3>This week's featured driver</h3>
<p><strong>Name:</strong> @Model.Driver.Name</p>
<p><strong>Description:</strong></p> @Model.Driver.Text
</div>