在 Umbraco 中发布数据时出错

编程入门 行业动态 更新时间:2024-10-26 16:25:31
本文介绍了在 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 中发布数据时出错

本文发布于:2023-10-12 00:18:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483249.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   Umbraco

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!