views:

49

answers:

4

I'm handling the submit portion of a ASP.NET MVC 2 page via javascript/JQuery because i need to construct some arrays, in the controller i either return back a View (if there are errors) or a URL (redirect)

I can handle the URL fine, but i have no idea how to handle returning a View object

public ActionResult Update(List<string> items, List<string> items2)  

{

//if error return View
return Json(View("EditRoles", new AdminEditRolesViewModel(roles)), JsonRequestBehavior.AllowGet);

//if OK return new URL
return Json(RedirectToAction("EditRolesDetails"), JsonRequestBehavior.AllowGet);

}

The javascript looks like this:

$.ajax({  
                url: '/Admin/Update/',  
                data: { items: editedRoles, items2: $("#deleteList").sortable('toArray') },  
                success: function(data) {  
                    window.location.href = data;  
                }  
            });  

Which handles URLS fine if data looks like "/Controller/Action", just not sure how to handle something like a View that returns an object with

  1. RouteName
  2. RouteValues (Array of key/value pairs)

Thanks much

A: 

Typically if using ajax you'll want to return a json representation of your model rather than a view.

In your case I'm assuming you'll want to return a string such as /Controller/EditRole/123

elspiko
Well the problem is that i need to display errors that in the ModelState, as soon as i redirect - im pretty sure i'll lose itUnless im mistaken?
Jerrold
A: 

I don't know if I'm 100% sure what you're trying to do but if there are errors can't you go through the ModelState and map those errors to a new type that you return via a JsonResult().

So maybe something like:

var item = new AdminEditRolesViewModel(roles);

foreach (var modelState in ModelState) 
{
   item.Errors.Add(modelState.Value);
}

return new JsonResult
             {
                 Data = item
             };

Then in your JS you would check to see if there are errors and deal with it appropriately.

Is that what you're looking for, or am I totally off base on your need?

Jeff T
A: 

Just to ask, is what you really want to create a modal alert of some kind? Something where you might have a partial view that fills in with error data and displays to the end user, maybe using JQuery or something to make it look nice?

ThatSteveGuy
A: 

@Elspiko

Yup i ended up using that approach as a workaround,

1) I still create a Json object, but fill it with data that might be used in an error and kick it back out 2) As the callback, i take that object and pass it back to the controller and handle it appropriately

$.ajax({
                url: '/Controller/Action/',
                data: { items: editedRoles, items2: $("#deleteList").sortable('toArray') },
                type: 'POST',
                traditional: true,
                success: function(data) {
                    var url = data.url + "?key=" + data.key + "&errorMessage=" + data.errorMessage;

                    window.location.href = url;
                }
            });

And in the Controller

public ActionResult Submit(string key, string errorMessage)
    {
        if(string.IsNullOrEmpty(key) && string.IsNullOrEmpty(errorMessage))
        {
            return RedirectToAction("SuccessAction");
        }

        //error coming back from jQuery, rebuild ModelState errors
        _wrapper.AddError(key, errorMessage);            
        return View("MyView", new myView());
    }
Jerrold