【问题标题】:Getting error on posting data in Umbraco在 Umbraco 中发布数据时出错
【发布时间】:2014-09-10 16:01:39
【问题描述】:

目前我同时使用 get(RenderMvcController) 和 post(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);
        }
    }
}

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

【问题讨论】:

  • 您实际上并没有发布任何代码来显示您是如何编写记录的。您的存储库确实是唯一值得关注的代码,因为正是它引发了错误。
  • 我认为,由于 RenderModel,它会引发错误。我使用了继承 RenderModel 的 BaseModel,错误是传递给 RenderModel 的 IPublishedContent。我已经更新了我的问题,并使用 BaseModel 和 CategoryRepository 发布了所有代码。
  • 您需要使用调试器单步执行并查看抛出异常的位置。您的问题描述确实指出“在数据库中插入记录时出现错误”,这表明如果您取出 repo.Insert(model);repo.Update(model); 行,则页面不会引发异常。否则我们真的需要查看您的存储库代码。

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


【解决方案1】:

现在我使用 SurfaceController 进行发布,使用 ChildActionOnly 进行加载时的渲染列表,并从模型中删除了 RenderModel。现在它可以正常运行 CURD 操作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-03
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多