在Umbraco中发布数据时出错



当前我正在为get使用两个控制器(RenderMvcController)&支柱(SurfaceController)。但是我在使用Umbrac.Core在数据库中插入记录时遇到错误。

错误:"不存在从对象类型Umbrac.Web.PublishedCache.XmlPublishedCache.XXmlPublishedContext到已知托管提供程序本机类型的映射。"

模型-BaseModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Umbraco.Web;
namespace SampleLogic.Models
{
    public class BaseModel : Umbraco.Web.Models.RenderModel
    {
        public BaseModel()
        : base(UmbracoContext.Current.PublishedContentRequest.PublishedContent)
        {}
    }
}



型号-分类.cs

[TableName("Categories")]
[PrimaryKey("Id", autoIncrement = true)]
public class Category : BaseModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Category> lstCategory;
    public Category()
    {
        lstCategory = new List<Category>();
    }
}

查看:Sample.cshtml

@using SampleLogic
@using SampleLogic.Models
@inherits UmbracoTemplatePage
@{
    Layout = "umbLayout.cshtml";
    var repo = new CategoryRepository();
}
@Html.Action("AddCategory", "SampleSurface")
@foreach (var category in repo.GetAll())
{
    <p>
        @category.Name 
        @Html.ActionLink("Edit", "Sample", "Sample", new { id = @category.Id }, null)
        <a href="?id=@category.Id">Edit</a>
    </p>
}


存储库:类别存储库.cs

using SampleLogic.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using umbraco.DataLayer;
using Umbraco.Core;
using Umbraco.Core.Persistence;
namespace SampleLogic
{
    public class CategoryRepository
    {
        private readonly UmbracoDatabase _database;
        public CategoryRepository()
        {
            _database = ApplicationContext.Current.DatabaseContext.Database;
        }
        public List<Category> GetAll()
        {
            return _database.Fetch<Category>("select * from categories");
        }
        public Category GetCategoryById(int id)
        {
            return _database.FirstOrDefault<Category>("select * from categories where Id = " + id);
        }
        public void Insert(Category category)
        {
            _database.Insert(category);
        }
        public void Update(Category category)
        {
            _database.Update(category);
        }
        public void Delete(Category category)
        {
            _database.Delete(category);
        }
    }
}

控制器:SampleController.cs

using SampleLogic.Models;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Umbraco.Core.Models;
using Umbraco.Web;
using Umbraco.Web.Models;
using Umbraco.Web.Mvc;
namespace SampleLogic.Controllers
{
    public class SampleController : RenderMvcController
    {
        public ActionResult Sample(int id = 0)
        {
            Category model = new Category();
            var repo = new CategoryRepository();
            if (Request.QueryString["id"] != null)
            {
                model.Name = repo.GetCategoryById(Convert.ToInt32(Request.QueryString["id"])).Name;
            }
            model.lstCategory = repo.GetAll();
            return CurrentTemplate(model);
        }
    }
}



控制器:SampleSurfaceController.cs

using SampleLogic.Models;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Umbraco.Core.Models;
using Umbraco.Web;
using Umbraco.Web.Mvc;
namespace SampleLogic.Controllers
{
    public class SampleSurfaceController : SurfaceController
    {
        [HttpPost]
        public ActionResult Sample(Category model)
        {
            var repo = new CategoryRepository();
            if (model.Id > 0)
            {
                repo.Update(model);
            }
            else
            {
                repo.Insert(model);
            }
            model.Name = string.Empty;
            return CurrentUmbracoPage();
        }
        [ChildActionOnly]
        public ActionResult AddCategory(Category model)
        {
            if (Request.QueryString["id"] != null)
            {
                var repo = new CategoryRepository();
                model.Name = repo.GetCategoryById(Convert.ToInt32(Request.QueryString["id"])).Name;
            }
            //TODO: do some searching (perhaps using Examine) 
            //using the information contained in the custom class QueryParameters
            //return the SearchResults to the view
            return PartialView("AddCategory", model);
        }
    }
}


插入或更新记录时,我在SurfaceController上遇到错误。如何解决上述问题。让我知道代码有什么问题。

现在我已经使用SurfaceController进行post&加载时渲染列表的ChildActionOnly,并从模型中删除了RenderModel。现在CURD操作正常。

相关内容

  • 没有找到相关文章

最新更新