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

[Solved] RedirectToAction from Grid Insert

7 Answers 204 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Eva
Top achievements
Rank 1
Eva asked on 04 Jan 2012, 08:37 PM
I have a grid with a nested Menu.  The grid is set to editable with popup mode for insert purposes.  When an insert is execute, I want the page to forward from the popup to the larger Edit page.  Editing is actually facilitated by the nested Menu and that works perfectly.  It is the call to the RedirectToAction back to Edit from the popup insert that is failing.

My code in the contoller looks like this:
[GridAction]
public ActionResult Edit(Guid id)
{
  var review = ViewModel.FirstOrDefault(m => m.ID == id);
  return View(review);
}

[GridAction]
[HttpPost]
public ActionResult Insert(StandardSetViewModel model)
{
    StandardSetViewModel review = StandardSetViewModel.CreateStandardSet(model);
           
    if (TryUpdateModel(review))
        ViewModel.Add(review);

    return RedirectToAction("Edit", "StandardSet", new { id = review.ID });
}


Everything appears to work fine through the actual method, but then the generated HTML is slightly different.  From the Edit command view the menu called directly the generated HTML in the space that appears to have the issue looks like this:
//<![CDATA[
jQuery(document).ready(function(){
jQuery('#RibbonBar').tTabStrip();});
//]]>

Coming from the RedirectToAction call it looks like this:
//<![CDATA[
jQuery(document).ready(function(){
});
//]]>

Any explanation for why the #RibbonBar would not be called from the Redirect?  If you need any further explanation, please do let me know!
Thanks!

7 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 06 Jan 2012, 02:23 PM
Hi Eva,

I can't understand what your exact scenario is. Could you send the View in which you are using the Grid? From the code you sent, I can tell you that if you are using Ajax binding, as the GridAction attribute suggests, the Grid will expect a GridModel of the data collection as a result. The RedirectToAction doesn't actually return a result but an instruction(HTTP 302) to the Browser to redirect to another address.

Regards,
Daniel
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
0
Eva
Top achievements
Rank 1
answered on 06 Jan 2012, 02:43 PM
Daniel,

Thank you so much for replying.  Yes, I am using AJAX binding.  What you say makes sense.  I tried removing the GridAction decoration and am still getting the same thing.  I have attached the view in question so you can see what I am attempting to do.

Eva
0
Daniel
Telerik team
answered on 09 Jan 2012, 03:58 PM
Hello Eva,

The GridAction attribute is necessary for the proper work of the Grid when using Ajax Binding and the Grid will still expect a JSON data collection as result.
To redirect to the Edit View after the item is inserted you could handle the client-side OnComplete event and redirect manually. The inserted item ID can be send through the Insert action instead of the collection and taken from the OnComplete event argument's response property. For example:
[GridAction]
[HttpPost]
public ActionResult Insert(StandardSetViewModel model)
{
    StandardSetViewModel review = StandardSetViewModel.CreateStandardSet(model);
            
    if (TryUpdateModel(review))
        ViewModel.Add(review);
 
    return Json({ id = review.ID });
}
//Script
function onComplete(e) {
    if (e.name == 'insert') {
        window.location.href = 'Home/Edit?id=' + e.response.id;
    }
}

Alternative to this approach is to use Server Binding or a Custom Toolbar command instead of the insert command.


All the best,
Daniel
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
0
Eva
Top achievements
Rank 1
answered on 09 Jan 2012, 04:48 PM
I really want to use the Insert command as it does 100% what I am trying to do.

Using the suggestion above I get an error of "Microsoft JScript runtime error: Unable to get value of the property 'length': object is null or undefined" on "if(s.length&&typeof s[0].HasSubgroups!="undefined"&&!remoteOperations)" in telerik.grid.min.js.  If I click Continue when the exception is hit, it all seems to work fine.  So I just need a way to make this error stop happening now and I will be totally golden.

Thanks!
0
Accepted
Daniel
Telerik team
answered on 11 Jan 2012, 01:16 PM
Hi Eva,

The Grid tries to get the length of the returned collection and since no collection is being send back an error is raised.
To avoid it, you should return a GridModel which contains a collection with one element holding the ID. For example:
[GridAction]
[HttpPost]
public ActionResult Insert(StandardSetViewModel model)
{
    StandardSetViewModel review = StandardSetViewModel.CreateStandardSet(model);
             
    if (TryUpdateModel(review))
        ViewModel.Add(review);
    var data = new List<StandardSetViewModel>();
    data.Add(review);
    return View(new GridModel(data));
}
function onComplete(e) {
    if (e.name == 'insert') {
        window.location.href = 'Home/Edit?id=' + e.response.data[0].ID;
    }
}


Kind regards,
Daniel
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
0
Alvic
Top achievements
Rank 1
answered on 06 Jul 2012, 01:42 AM
Old topic, but a variant on the question.  In the posted solution, after insert it is redirecting using the 0 index item in the list.  This works when that is what you want, like if you are ordering by latest created.  But what if you are ordering on something like a Customer Order date descending and the user enters an order with a back-dated order date.  In this case, when the ajax insert is completed, the 0 index item is not the newest one the user created. 

I can't figure out how to redirect to the latest order created without ordering by the created date descending.  Is there a way in the OnComplete clientside method to find the latest added item and not just go by the 0 index?
0
Daniel
Telerik team
answered on 09 Jul 2012, 01:54 PM
Hello Alvic,

I am not sure if I understand correctly the question. The suggested approach is used to return some custom data and use it to redirect to a different page on the client. You could find the latest order in the action method and return it in a collection to avoid an error.

Regards,
Daniel
the Telerik team
Check out the successor of Telerik MVC Extensions - Kendo UI for ASP.NET MVC - and deem it for new ASP.NET MVC development.
Tags
General Discussions
Asked by
Eva
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Eva
Top achievements
Rank 1
Alvic
Top achievements
Rank 1
Share this question
or