This is a migrated thread and some comments may be shown as answers.

Editing / updateting grid in a details template not working..

3 Answers 505 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ole
Top achievements
Rank 1
Ole asked on 04 Jun 2012, 05:23 PM

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

3 Answers, 1 is accepted

Sort by
0
Ole
Top achievements
Rank 1
answered on 06 Jun 2012, 03:53 PM
Hi,

solved this problem,  by renaming the category input parameter to categoryModel in the ReviewCategory_Update controller method.

The problem was that a property in the model also was called category and conflicting with input parameter. 

Now a new problem has come. My Model is entity framework based and when I update a review model.state.isvalid is false that there are no entity keys.

Here is the JSON the web client sends for an update

sort=&group=&filter=&ReviewID=4&Customer=new+&Location=new&Title=new23&EntityState=2&EntityKey.EntitySetName=ReviewSet&EntityKey.EntityContainerName=ReviewModelContainer&EntityKey.EntityKeyValues%5B0%5D%5BKey%5D=ReviewID&EntityKey.EntityKeyValues%5B0%5D%5BValue%5D=4&EntityKey.IsTemporary=false&EntityKey%5BEntitySetName%5D=ReviewSet&EntityKey%5BEntityContainerName%5D=ReviewModelContainer&EntityKey%5BEntityKeyValues%5D%5B0%5D%5BKey%5D=ReviewID&EntityKey%5BEntityKeyValues%5D%5B0%5D%5BValue%5D=4&EntityKey%5BIsTemporary%5D=false

As far as I can see the entitykey is there 3 times.
Is this a bug ?,
Is EF not supported in mvc/Kendo ?.

Best
Ole


0
Nikolay Rusev
Telerik team
answered on 07 Jun 2012, 01:32 PM
Hello Ole,

Unfortunately we are not sure how to reproduce this behavior. Can you provide us with a sample runnable app which demonstrate described behavior? Thus we'll be able to assist you further.

All the best,
Nikolay Rusev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Ole
Top achievements
Rank 1
answered on 08 Jun 2012, 03:13 PM
Hi,

I have placed a support ticket with files..

ticket ID 552602
Tags
Grid
Asked by
Ole
Top achievements
Rank 1
Answers by
Ole
Top achievements
Rank 1
Nikolay Rusev
Telerik team
Share this question
or