0

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?

  • Elaboration-

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

tony
  • 2,178
  • 2
  • 23
  • 40
  • Incidentally, why bind to json? I could just have a for loop in the view and render the table, then bind Kendo to the table. This way just struck me as easier – tony Jul 04 '14 at 10:49
  • If you can bind the kendo grid like this (i don't know this grid) this seems a lot easier. – NicoD Jul 04 '14 at 10:52
  • I have to guess what's going to happen long term, I want to have the flexibility to be able to render the grid in both ways to prepare for future requirements, in particular hyperlinks will probably have loads of 'if' statements around them to say if enabled or not – tony Jul 04 '14 at 11:04
  • The grid has to be bind to json right ? so why do you use a partial view to render the json ? this part @{ var jsLines = @Html.Raw(Json.Encode(Model.Lines)); } should be done in you controller with return Json( ... myObjectToSerialize...); – NicoD Jul 04 '14 at 13:25
  • It's not just the json that's getting returned. Taking 'Add' as an example, it will return a partial view with the now cleared input elements and the json for the grid – tony Jul 04 '14 at 13:33
  • you could render [the partial view as a string](http://stackoverflow.com/questions/2537741/how-to-render-partial-view-into-a-string) and include it in the json message – NicoD Jul 04 '14 at 13:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/56779/discussion-between-tony-and-nicod). – tony Jul 04 '14 at 13:37

2 Answers2

1

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

$.ajax({
    url: '@Url.Action("Action", "Controller")',
    ...
NicoD
  • 1,179
  • 8
  • 19
  • As I say in the original question, "whats the equivalent for the controller?" And calling Ajax.ActionLink – tony Jul 04 '14 at 10:25
  • Maybe I don't really get your point. Ajax.ActionLink will create an anchor to a specific url. You can't do that in a controller. You want to redirect to another action from the controller ? – NicoD Jul 04 '14 at 10:29
  • I want to make a hyperlink in the cs code, this will be inserted into json which will be passed to a view, I will later bind with a Kendo grid. The hyperlink will be for select/delete – tony Jul 04 '14 at 10:31
  • It might be a case of using Url.Action and inserting the various tags myself, e.g. data-ajax='true' data-ajax-mode='replace' data-ajax-update='{2}' This is new to me so not sure yet – tony Jul 04 '14 at 10:33
  • You should update your question to your real problem. Maybe it's not the right way to do it. As I understand it now you want an action returning a json message containing the url as an html link. – NicoD Jul 04 '14 at 10:38
0

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

tony
  • 2,178
  • 2
  • 23
  • 40