tony
tony

Reputation: 2392

Asp MVC AjaxExtensions, call from controller

I'm sure this is simple but I'm struggling to find it

Inside a controller you can do something like this:

public ActionResult MyAction()
{
  string url = Url.Action(action, controller),
  // do something with the url
}

What's the Ajax equivalent? i.e. where you would call Ajax.ActionLink in a View whats the equivalent for the controller?

I have a master/detail arrangement with a grid and some input elements. You can click on select/delete in the grid to amend or delete the line.

The grid is a Kendo UI grid, the view is rendered via:

  1. a partial view to render the input elements
  2. creating a json object, i.e.

    @{ var jsLines = @Html.Raw(Json.Encode(Model.Lines)); }

  3. Binding the Kendo grid to this json

From within the grid I want to hit on select and call an Ajax method to update the partial view with the form details

thanks

Upvotes: 0

Views: 51

Answers (2)

tony
tony

Reputation: 2392

I'm not at all convinced that this is the right way to go but it's always good to have options.

Ajax.ActionLink seems to be the same as Url.Action but with a few attributes thrown in. So you can use this:

        return string.Format("<a data-ajax='true' data-ajax-mode='replace' 
                               data-ajax-update='{2}' href=\"{0}\">{1}</a>",
            Url.Action(action, controller, routeValues),
            text, 
            "formContainerSelectSection");

to update this:

<div id="formContainerSelectSection">
    ... stuff to be replaced via ajax
</div>

I accept, especially after the discussion with NicoD, that there are other and probably easier ways to do this, in particular this is creating a link in the controller, that's the Views job, but the original question was about how to do this

Upvotes: 0

NicoD
NicoD

Reputation: 1189

You can use Url.Action from the razor view. Something like :

$.ajax({
    url: '@Url.Action("Action", "Controller")',
    ...

Upvotes: 1

Related Questions