0

How can I convert $("#myChildTabName").load("./MyController/MyMethod/" + myParameter); to use @url.Action ? MyMethod returns a PartialView. Basically I am I want to load a partial view upon click on the Tab.

Update as below: I have figured out a solution as below, but this is giving an error saying that applicationID does not exist in the current context. An alert just above the jQuery call does display applicationID.

//The below code DOES NOT work.

function LoadTabContents(applicationID)
{
        $("#currentTab").load('@Url.Action("TabMethod", "MyController", new { @id = applicationID })')
}

//The below code works fine.

function LoadTabContents(applicationID)
{
        $("#currentTab").load('@Url.Action("TabMethod", "MyController", new { @id = 123})')
}

4 Answers 4

4

I am afraid what you are trying to do cannot be done all via Razor. The applicationID must be appended client side since only the client knows the real value. You can try something like this:

function LoadTabContents(applicationID) {
    $("#currentTab").load('@Url.Action("TabMethod", "MyController")' + '/'+ applicationID);
}

The URL.Action will generate the right path and the rest will append the parameter phased to the JavaScript function. Some fiddling must be done with the slashes since I cannot remember if the URL.action will append one or not...

Sign up to request clarification or add additional context in comments.

Comments

1

@Url.Action - is server code, you can use it only in Razor Views. Look at this question ASP.NET MVC Url.Action in JQuery is not recognized

4 Comments

function LoadTabContents(applicationID) { $("#currentTab").load('@Url.Action("TabMethod", "MyController", new { @id = 123})') } - does work fine. Now I need to pass on a correct parameter value instead of the hardcoded 123.
@daniele my answer is correct even after update, @Url.Action generates on server and your function works on client.
Thanks webdeveloper for your suggestion. There is no argument that your code would work just fine. But my question is how can I fix the code so that it passes applicationID back to the server.
@daniele You trying to mix server and client code, it's not possible. Create link outside function in razor view and then add parameter: generatedUrl + '/' + applicationID OR generatedUrl + '?id=' + applicationID
0

1 . You should not use partial method in this case, if you what ot load data via request, you should use return View(), not partial View. But you if want to add some html in view you should use partial.And in this case partial means part of main view.

If you want to avoid using main layout - set property layout=string.Empty

2 . You can load with POST method and use like, in this case you can not use load() because it is GET request. You $.POST or $.Ajax

 public ActionResult Index()
        {
            if (Request.IsAjaxRequest())

                return PartialView("partialView");
            else
                return View();
        }
  1. Simple option for Partial vs View based on method type is set layout like:

@{ Layout = Request.IsAjaxRequest() ? null : "~/Views/Shared/_Layout.cshtml"; }

1 Comment

Thanks Anton, but this is not the answer that I am looking for.
0

May be this solution would work for you

$("#currentTab").load('@Url.Action("TabMethod", "MyController", new { @id = "[APPLICATION_ID]"}).replace("[APPLICATION_ID]",applicationID)')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.