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

[Solved] Grid commands not binding to appropriate controller methods

3 Answers 79 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jark Monster
Top achievements
Rank 1
Jark Monster asked on 30 May 2012, 09:01 PM
I am writing a project that uses CSLA with CodeSmith generated entities, and I have been having a problem getting a basic grid up and running.  Eventually, I want to support Batch Editing, but given these issues, I've simplified my code for the time being.

Basically, when I run the application, everything renders correctly, and I can get to the point where I need to actually insert the item into the list, but nothing happens except that the new grid row is wiped back to default values.

When I use the debugger to see what is going on, and both the Insert/Edit and Cancel buttons are invoking the Index() method (the start up method for the view).  I'm baffled as to why this is happening as my databinding section is exactly the same as many examples that I have seen.  It could easily be something else, but can anybody confirm/deny that CSLA could cause something like this?  Any help would be greatly appreciated!

View
@using Telerik.Web.Mvc.UI
@model Test.App.MVC.Models.ShipmentModel
 
@{
    Layout = "~/Views/Shared/_StandardLayout.cshtml";
}
 
    @(Html.Telerik().Grid(Model.Shipment.ItemList).Name("TestGrid")
        .DataKeys(keys =>
        {
            keys.Add(p => p.ItemID);
        })
        .ToolBar(commands =>
        {
            commands.Insert().ButtonType(GridButtonType.ImageAndText).ImageHtmlAttributes(new { style = "margin-left:0" });
        })
        .DataBinding(dataBinding =>
        {
            dataBinding.Ajax()
                .Select("SelectDefaultReferences", "Home")
                .Insert("InsertDefaultReferences", "Home")
                .Update("SaveDefaultReferences", "Home");
        })
        .Resizable(resizing => resizing.Columns(true))
        .Reorderable(reorderable => reorderable.Columns(true))
        .Columns(columns =>
        {
            columns.Bound(p => p.FreightClass).Width(30);
            columns.Bound(p => p.Dimension.UnitOfMeasure).Width(30);
            columns.Bound(p => p.Dimension.Length).Width(30);
            columns.Bound(p => p.Dimension.Width).Width(30);
            columns.Command(commands => commands.Edit().ButtonType(GridButtonType.ImageAndText));
        })
        .Pageable()
        .Scrollable(c => c.Height("375px"))
        .Sortable()
    )

Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Telerik.Web.Mvc;
 
namespace Test.App.MVC.Controllers
{
    [Authorize]
    public class HomeController : Csla.Web.Mvc.Controller
    {
        //This method is called for every button press (Add New, Insert, and Cancel)
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to BlueShip";
             
            ControllerBase.State.ClearObjects();
            Test.App.MVC.Models.ShipmentModel shipmentModel = new Test.App.MVC.Models.ShipmentModel();
             
            shipmentModel.Shipment.ShipmentStatus = "New";
 
            ControllerBase.State.Object = shipmentModel;
            ViewData.Model = shipmentModel;
 
            return View();
        }
 
        [GridAction]
        public ActionResult SelectDefaultReferences()
        {
            try
            {
                Test.App.MVC.Models.ShipmentModel shipmentModel = (Test.App.MVC.Models.ShipmentModel)ControllerBase.State.Object;
 
                return View(new GridModel(shipmentModel.Shipment.ItemList));
            }
            catch (Exception e)
            {
                ModelState.AddModelError(string.Empty, "Error occurred: " + e.Message);
                return PartialView("Error");
            }
        }
                      [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult SaveDefaultReferences(int id)
        {
            try
            {
                Test.App.Business.Shipment.Item item = new Business.Shipment.Item();
                Test.App.MVC.Models.ShipmentModel shipmentModel = (Test.App.MVC.Models.ShipmentModel)ControllerBase.State.Object;
 
                shipmentModel.Shipment.ItemList.Remove(item);
 
                TryUpdateModel(item);
 
                shipmentModel.Shipment.ItemList.Add(item);
 
                return View(new GridModel(shipmentModel.Shipment.ItemList));
            }
            catch (Exception e)
            {
                ModelState.AddModelError(string.Empty, "Error occurred: " + e.Message);
                return PartialView("Error");
            }
        }
                      [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult InsertDefaultReferences()
        {
            try
            {
                Test.App.Business.Shipment.Item item = new Business.Shipment.Item();
                Test.App.MVC.Models.ShipmentModel shipmentModel = (Test.App.MVC.Models.ShipmentModel)ControllerBase.State.Object;
 
                TryUpdateModel(item);
                shipmentModel.Shipment.ItemList.Add(item);
 
                return View(new GridModel(shipmentModel.Shipment.ItemList));
            }
            catch (Exception e)
            {
                return PartialView("Error");
            }
        }
}

3 Answers, 1 is accepted

Sort by
0
Cliff
Top achievements
Rank 1
answered on 04 Jul 2012, 02:35 PM
Hi,

I am getting exactly the same problem, the Url that is built by the grid is 'http://localhost:33900/Home/Index/135?RequestList-mode=select' when the select method should invoke the 'EditSaved' action of another controller.

Any help would be gratefully appreciated.

Telerik make great tools, but using them can be a pain!

Regards.
0
Cliff
Top achievements
Rank 1
answered on 04 Jul 2012, 02:56 PM
Hi,

Not really a fix but I have now got round the problem by creating a custom command.
.Columns(Sub(columns)
       columns.Command(Sub(commands)
       commands.Custom("Change") _
             .HtmlAttributes(new with { .id = "change"}) _
             .Text("Edit") _
             .Action("RequestSelect", "RequestViewModel") _
             .ButtonType(GridButtonType.Text)
       commands.Delete().ButtonType(GridButtonType.Text)
End Sub).Width(200)

There is a gotcha with this, in that the .HtmlAttributes must be used to set the id of the command button, or the command will not work.

Sorry example is in VB.NET but I am having to use it for a while....

Regards.
0
dusan
Top achievements
Rank 1
answered on 19 Jul 2012, 09:16 AM
I just was able to get away from this with this nasty trick :

custom route like this :

routes.MapRoute("TakeOffGridUpdateOrInsertFix""TakeOffGridUpdateOrInsert"new
            {
                controller = "TakeOff",
                action = "TakeOffGridUpdateOrInsert"
            });

It's snippet from my Global.asax.cs. Grid is indeed posting to server, but it lacks controller part in URL , like this :
http://localhost:4479/TakeOffGridUpdateOrInsert

It's a hack and I'm going to definitely post a sample to them so they are aware of this.

Will keep you updated, but this really sucks... :( Or it's just me, but pls tell me what am I doing wrong to fix this. Grid is loading data correctly, but Insert/Update/Delete is posted without controller part in URL. :(
Tags
Grid
Asked by
Jark Monster
Top achievements
Rank 1
Answers by
Cliff
Top achievements
Rank 1
dusan
Top achievements
Rank 1
Share this question
or