在Umbraco中发布数据时出错

时间:2014-08-29 09:58:36

标签: c# .net asp.net-mvc razor umbraco7

目前我正在使用两个控制器for get(RenderMvcController)&后(SurfaceController)。但是在使用Umbraco.Core在数据库中插入记录时出现错误。

错误:"从对象类型Umbraco.Web.PublishedCache.XmlPublishedCache.XmlPublishedContent到已知的托管提供程序本机类型没有映射。"

型号 - 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)
        {}
    }
}



型号 - Category.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>
}


存储库:CategoryRepository.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);
        }
    }
}

Controller: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);
        }

    }
}



Controller: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上收到错误。 如何解决上述问题。让我知道代码有什么问题。

1 个答案:

答案 0 :(得分:0)

现在我已经将SurfaceController用于post&amp; ChildActionOnly用于加载渲染列表并从模型中删除RenderModel。现在它适用于CURD操作。