1

In my View I have a ribbon with a spinner where users select a int type that I use as parameter to get some data in the repository.

So I made a ajax request:

  var url = "/ControllerExample/GetJsonUrl?param=" +      s.GetItemValueByName("countParam") ;
        jQuery.ajax({
            type: "GET",
            url: url,
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                window.location.href = data;
            },
            failure: function (errMsg) {
                alert(errMsg);
            }
        });

And in my Controller I have a ActionResult that returns a JsonResult with the specific url to the 'success' function:

 public ActionResult GetJsonUrl(string param)
    {

            var urlHelper = new UrlHelper(ControllerContext.HttpContext.Request.RequestContext);

            string url = urlHelper.Action("OtherAction", "ControllerExample", new {id = param});

            return new JsonResult {Data = url, JsonRequestBehavior = JsonRequestBehavior.AllowGet};           
    }

So if the result is success, the request goes back to the ajax success function and then go to the ExampleController to execute the OtherAction.

I would like to do it with just one action, is it possible? Refreshing the page in the JsonResult or something.

Drunix
  • 3,313
  • 8
  • 28
  • 50
gog
  • 11,788
  • 23
  • 67
  • 129

1 Answers1

2

You can render that view with your model to string and pass with JSON result. And then just replace entire DOM elemet.

Render a view as a string

public string RenderRazorViewToString(string viewName, object model)
{
  ViewData.Model = model;
  using (var sw = new StringWriter())
  {
    var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
    var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
    viewResult.View.Render(viewContext, sw);
    viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
    return sw.GetStringBuilder().ToString();
  }
}

And here

   success: function (data) {
        $("#targetDOMElementId").val(data);
    },

Where targetDOMElementId could be a container DIV for you View.

Community
  • 1
  • 1
NoWar
  • 36,338
  • 80
  • 323
  • 498
  • 1
    Thanks, now i got the point. I think it will work. By the way, i was testing the ajax request, and for a mistake i put type as 'POST', it worked too. Do you know why? – gog Apr 01 '14 at 19:40
  • 1
    @ggui I am happy that it was useful for you! – NoWar Apr 01 '14 at 19:41
  • @ggui There are cases where getting data with a complex type made of json in pure HTTP it is not allowed to do a GET request with request data in the body (json) but it is allowed to do GET requests with request data in the URI like domain.com?number=2&factor=4 but then its not json data therefore you can also use a POST to get data with a complex request type with json :) – Elisabeth Apr 05 '14 at 14:32