You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于ASP.NET MVC创建Sitecore 9.3内容编辑器可编辑的CRUD网页

Sitecore 9.3 连接与可编辑CRUD功能ASP.NET MVC网页实现指南

1. 环境配置与Sitecore连接

  • 确认你的ASP.NET MVC项目基于**.NET Framework 4.7.2**,这是Sitecore 9.3的硬性要求
  • 在项目web.config中配置Sitecore数据库连接字符串,示例:
    <connectionStrings>
      <add name="Sitecore.Core" connectionString="Data Source=你的SQL服务器;Initial Catalog=Sitecore.Core;User ID=sa;Password=你的密码;" />
      <add name="Sitecore.Master" connectionString="Data Source=你的SQL服务器;Initial Catalog=Sitecore.Master;User ID=sa;Password=你的密码;" />
      <add name="Sitecore.Web" connectionString="Data Source=你的SQL服务器;Initial Catalog=Sitecore.Web;User ID=sa;Password=你的密码;" />
    </connectionStrings>
    
  • 通过NuGet安装匹配Sitecore 9.3版本的核心包:Sitecore.Kernel、Sitecore.Mvc,版本号需对应9.3.x系列

2. 创建Sitecore可编辑数据模板

  • 登录Sitecore内容编辑器,在模板库(Template Library)中新建模板,命名为Product,路径设为/Templates/Custom/Product
  • 为模板添加字段:
    • Name:字段类型选单行文本,勾选“共享字段”
    • Price:字段类型选数字,设置合适的小数位数
    • Description:字段类型选富文本
  • 为模板设置标准值,预留后续关联MVC视图的配置位
  • 在内容树的Content节点下新建Products文件夹,添加若干基于Product模板的内容项作为测试数据

3. 实现MVC控制器的CRUD逻辑

创建继承自Sitecore.Mvc.Controllers.SitecoreController的控制器,示例代码:

using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Security.Accounts;
using System.Linq;
using System.Web.Mvc;

public class ProductController : Sitecore.Mvc.Controllers.SitecoreController
{
    private readonly Database _masterDb = Sitecore.Data.Database.GetDatabase("master");
    private readonly ID _productTemplateId = new ID("{你的Product模板ID}");

    // 列表展示(Read)
    public ActionResult Index()
    {
        var productsFolder = _masterDb.GetItem("/sitecore/content/Products");
        var products = productsFolder.Children.Where(i => i.TemplateID == _productTemplateId).ToList();
        return View(products);
    }

    // 跳转创建页面
    public ActionResult Create()
    {
        return View();
    }

    // 执行创建(Create)
    [HttpPost]
    public ActionResult Create(string name, decimal price, string description)
    {
        using (new UserSwitcher(User.FromName(@"sitecore\admin", false)))
        {
            var productsFolder = _masterDb.GetItem("/sitecore/content/Products");
            var productItem = productsFolder.Add(name, new TemplateID(_productTemplateId));
            
            productItem.Fields["Name"].Value = name;
            productItem.Fields["Price"].Value = price.ToString();
            productItem.Fields["Description"].Value = description;
            
            productItem.Editing.AcceptChanges();
        }
        return RedirectToAction("Index");
    }

    // 跳转编辑页面
    public ActionResult Edit(string id)
    {
        var productItem = _masterDb.GetItem(new ID(id));
        return View(productItem);
    }

    // 执行更新(Update)
    [HttpPost]
    public ActionResult Edit(string id, string name, decimal price, string description)
    {
        using (new UserSwitcher(User.FromName(@"sitecore\admin", false)))
        {
            var productItem = _masterDb.GetItem(new ID(id));
            productItem.Editing.BeginEdit();
            
            productItem.Fields["Name"].Value = name;
            productItem.Fields["Price"].Value = price.ToString();
            productItem.Fields["Description"].Value = description;
            
            productItem.Editing.EndEdit();
        }
        return RedirectToAction("Index");
    }

    // 执行删除(Delete)
    public ActionResult Delete(string id)
    {
        using (new UserSwitcher(User.FromName(@"sitecore\admin", false)))
        {
            var productItem = _masterDb.GetItem(new ID(id));
            productItem.Delete();
        }
        return RedirectToAction("Index");
    }
}

4. 构建可在内容编辑器中编辑的MVC视图

列表视图(Index.cshtml)

@model List<Sitecore.Data.Items.Item>

<h1>产品列表</h1>
<a href="@Url.Action("Create")">添加新产品</a>
<ul>
    @foreach (var item in Model)
    {
        <li>
            @Html.Sitecore().Field("Name", item) - @Html.Sitecore().Field("Price", item)
            <a href="@Url.Action("Edit", new { id = item.ID })">编辑</a>
            <a href="@Url.Action("Delete", new { id = item.ID })">删除</a>
            <div>@Html.Sitecore().Field("Description", item)</div>
        </li>
    }
</ul>

使用@Html.Sitecore().Field()渲染字段后,在Sitecore内容编辑器中点击字段即可直接编辑内容。

编辑视图(Edit.cshtml)

@model Sitecore.Data.Items.Item

<h1>编辑产品</h1>
@using (Html.BeginForm())
{
    @Html.Hidden("id", Model.ID)
    <div>
        <label>名称:</label>
        @Html.TextBox("name", Model.Fields["Name"].Value)
    </div>
    <div>
        <label>价格:</label>
        @Html.TextBox("price", Model.Fields["Price"].Value)
    </div>
    <div>
        <label>描述:</label>
        @Html.TextArea("description", Model.Fields["Description"].Value)
    </div>
    <button type="submit">保存</button>
}

5. Sitecore渲染与路由配置

  • 登录Sitecore内容编辑器,进入布局→渲染,新建控制器渲染:
    • 控制器选择ProductController
    • Action选择Index
    • 视图路径填写~/Views/Product/Index.cshtml
  • 将该渲染添加到目标页面的展示细节中,或直接关联到内容项的布局配置
  • 无需修改默认MVC路由,Sitecore会自动兼容现有路由规则

内容的提问来源于stack exchange,提问作者Peter Anthony Mortiga

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 13:25:16