在 Umbraco 中发布数据时出错 [英] Getting error on posting data in Umbraco

查看:60
本文介绍了在 Umbraco 中发布数据时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我使用两个控制器来获取(RenderMvcController) &后(表面控制器).但是在使用 Umbraco.Core 在数据库中插入记录时出现错误.

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

模型 - BaseModel.cs

Currently I am using both controller for get(RenderMvcController) & post(SurfaceController). But I am getting an error when inserting record in database using Umbraco.Core.

Error: "No mapping exists from object type Umbraco.Web.PublishedCache.XmlPublishedCache.XmlPublishedContent to a known managed provider native type."

Model - 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



Model - 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

View: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

Repository: 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

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

    }
}



控制器:SampleSurfaceController.cs

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 出现错误.如何解决上述问题.让我知道代码有什么问题.


I am getting error on SurfaceController when inserting or updating record. How to resolve above issue. let me know what is the problem with code.

推荐答案

现在我已经使用 SurfaceController 进行后期 &ChildActionOnly 用于在加载时渲染列表并从模型中删除 RenderModel.现在它可以很好地用于 CURD 操作.

Now I have used SurfaceController for post & ChildActionOnly for render listing on load and removed RenderModel from model. Now its working fine for CURD operation.

这篇关于在 Umbraco 中发布数据时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆