2014-08-29 45 views
0

目前我使用兩個控制器來獲取(RenderMvcController)&後(SurfaceController)。但是,當使用Umbraco.Core在數據庫中插入記錄時,出現錯誤。

錯誤:「沒有從對象類型Umbraco.Web.PublishedCache.XmlPublishedCache.XmlPublishedContent到已知託管提供程序本機類型的映射。」

型號 - BaseModel.cs
在Umbraco中發佈數據時出錯

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="[email protected]">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插入或更新記錄時收到錯誤。 如何解決上述問題。讓我知道代碼有什麼問題。

+0

您實際上沒有發佈任何顯示您如何編寫記錄的代碼。您的存儲庫實際上是唯一有興趣的代碼,因爲這是拋出錯誤。 – Digbyswift 2014-08-29 10:25:30

+0

我想,它因爲RenderModel而拋出一個錯誤。 我已經使用了繼承RenderModel的BaseModel,並且錯誤是傳遞給RenderModel的IPublishedContent。 我已經更新了我的問題,並且發佈了所有帶有BaseModel和CategoryRepository的代碼。 – Dimple 2014-08-29 10:29:00

+0

您需要使用調試器來查看引發異常的位置。你的問題描述確實聲明「我在數據庫中插入記錄時遇到了錯誤」,這表明如果你使用'repo.Insert(model);'和'repo.Update(model);'line out,那麼頁面不會拋出異常。否則,我們確實需要查看您的存儲庫代碼。 – Digbyswift 2014-08-29 13:37:51

回答

0

現在我已經使用SurfaceController發佈& ChildActionOnly用於加載渲染列表並從模型中刪除RenderModel。現在它在CURD操作中工作得很好。