Hi,
With this viewcode I am trying to create a grid hierachy to be editable. The first level grid works , but the 2.nd level does not Update when creating/editing, I get this ModelState error when runnig ReviewCategory_Update , " The parameter conversion from type 'System.String' to type 'Reviews.Models.ReviewCategory' failed because no type converter can convert between these types. " , 'Reviews.Models.ReviewCategory' is the class used to populate the 2.nd level grid.
<
ul
data-role
=
"listview"
data-inset
=
"true"
>
<
li
data-role
=
"list-divider"
>Navigation</
li
>
<
li
>@Html.ActionLink("About", "About", "Home")</
li
>
<
li
>@Html.ActionLink("Contact", "Contact", "Home")</
li
>
</
ul
>
@(Html.Kendo().Grid<
Reviews.Models.Review
>().Name("ReviewGrid").Columns(columns =>
{
columns.Bound(p => p.Customer);
columns.Bound(p => p.Location);
columns.Bound(p => p.Title);
columns.Command(command => { command.Edit(); command.Destroy(); }).Width(200);
})
.ToolBar(toolbar=>toolbar.Create())
.DetailTemplate(detail=>detail.ClientTemplate(
Html.Kendo().Grid<
Reviews.Models.ReviewCategory
>()
.Name("ReviewCategory_#=ReviewId#")
.Columns(columns=>
{
columns.Bound(o => o.TableNo);
columns.Bound(o => o.Category);
columns.Bound(o => o.Comment);
columns.Command(command => { command.Edit(); command.Destroy(); }).Width(200);
})
.ToolBar(toolbar => toolbar.Create())
.Editable(editable => editable.Mode(GridEditMode.InLine))
.Pageable()
.Sortable()
.Scrollable()
.DataSource(dataSource => dataSource
.Ajax()
.Events(events => { events.Error("error_handler"); })
.Model(model => model.Id(p => p.Id))
.Create(update => update.Action("ReviewCategory_Create", "Home", new { id = "#=ReviewId#" }))
.Read(read => read.Action("ReviewCategory_Read", "Home", new { id = "#=ReviewId#" }))
.Update(update => update.Action("ReviewCategory_Update", "Home"))
.Destroy(update => update.Action("ReviewCategory_Destroy", "Home"))
)
.ToHtmlString()
))
.Editable(editable=>editable.Mode(GridEditMode.InLine))
.Pageable()
.Sortable()
.Scrollable()
.DataSource(dataSource => dataSource
.Ajax()
.Events(events => { events.Error("error_handler"); })
.Model(model=>model.Id(p=>p.ReviewId))
.Create(update=>update.Action("Review_Create","Home"))
.Read(read => read.Action("Review_Read", "Home"))
.Update(update => update.Action("Review_Update", "Home"))
.Destroy(update => update.Action("Review_Destroy", "Home"))
)
)
<
script
type
=
"text/javascript"
>
function error_handler(e) {
if (e.errors) {
var message = "Errors:\n";
$.each(e.errors, function (key, value) {
if ('errors' in value) {
$.each(value.errors, function () {
message += this + "\n";
});
}
});
alert(message);
}
}
function dataBound() {
this.expandRow(this.tbody.find("tr.k-master-row").first());
}
</
script
>
And this controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kendo.Mvc.UI;
using Kendo.Mvc.Extensions;
using Reviews.Models;
using System.Data.Metadata.Edm;
using System.Data.Objects.DataClasses;
namespace Reviews.Controllers
{
public class HomeController : Controller
{
ReviewModelContainer reviewContext = new ReviewModelContainer();
public ActionResult Index()
{
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
return View();
}
public ActionResult Review_Read([DataSourceRequest] DataSourceRequest request)
{
//return Json(SessionProductRepository.All().ToDataSourceResult(request));
return Json(reviewContext.ReviewSet.ToDataSourceResult(request));
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Review_Create([DataSourceRequest] DataSourceRequest request, Review review)
{
if (review != null && ModelState.IsValid)
{
reviewContext.AddToReviewSet(review);
reviewContext.SaveChanges();
}
return Json(new[] { review }.ToDataSourceResult(request, ModelState));
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditingInline_Update([DataSourceRequest] DataSourceRequest request, Review review)
{
if (review != null && ModelState.IsValid)
{
int currentid = review.ReviewId;
Review target = reviewContext.ReviewSet.Where(p => p.ReviewId == currentid).First();
if (target != null)
{
target.Customer = review.Customer;
target.Location = review.Location;
target.Title = review.Title;
reviewContext.SaveChanges();
}
}
return Json(ModelState.ToDataSourceResult());
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditingInline_Destroy([DataSourceRequest] DataSourceRequest request, Review review)
{
if (review != null)
{
reviewContext.DeleteObject(review);
reviewContext.SaveChanges();
}
return Json(ModelState.ToDataSourceResult());
}
public ActionResult ReviewCategory_Read(int id, [DataSourceRequest] DataSourceRequest request)
{
EntityCollection<
Models.ReviewCategory
> categories = reviewContext.ReviewSet.Where(Review => Review.ReviewId == id).First().ReviewCategory;
return Json(categories.ToDataSourceResult(request));
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ReviewCategory_Create(int id, [DataSourceRequest] DataSourceRequest request, ReviewCategory category)
{
if (category != null && ModelState.IsValid)
{
category.Review = reviewContext.ReviewSet.Where(t => t.ReviewId == id).First();
reviewContext.AddToReviewCategorySet(category);
reviewContext.SaveChanges();
}
return Json(new[] { category }.ToDataSourceResult(request, ModelState));
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ReviewCategory_Update([DataSourceRequest] DataSourceRequest request, ReviewCategory category)
{
if (category != null && ModelState.IsValid)
{
int currentid = category.Id;
ReviewCategory target = reviewContext.ReviewCategorySet.Where(p => p.Id == currentid).First();
if (target != null)
{
target.TableNo = category.TableNo;
target.Category = category.Category;
target.Comment = category.Comment;
reviewContext.SaveChanges();
}
}
return Json(ModelState.ToDataSourceResult());
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ReviewCategory_Destroy([DataSourceRequest] DataSourceRequest request, ReviewCategory category)
{
if (category != null)
{
reviewContext.DeleteObject(category);
reviewContext.SaveChanges();
}
return Json(ModelState.ToDataSourceResult());
}
public ActionResult About()
{
ViewBag.Message = "Your quintessential app description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your quintessential contact page.";
return View();
}
}
}
Any idears of why this is comming...
Thanks for your help !.....
Best Ole