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)
{}
}
}
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>();
}
}
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>
}
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);
}
}
}
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);
}
}
}
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.
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.