0

Coming from a webform background using ajax I am trying to create similar with mvc.

I have worked out what the controller, view and models do.

What i would like to do now is that when my user clicks a button an jquery call is made to update the view - hence not refreshing the entire page.

Much like a master/child design in webforms.

I have googled and I have tried a couple of things but I get a 404. I have checked and i am sure I am calling things correctly.

This is my jquery function:

$("#divProductsBanner").click(function () {
    var currentObject = $(this).text();
    $.get('@Url.Action("Lite", "Service")', { theName: currentObject });       
});

Where 'Lite' is my view and 'Service' is my controller.

my Service.cs controller looks like this:

public ActionResult Lite()
{
    return View();
}

and I have a view called 'Lite' under my 'Service' folder under the 'Views' folder.

when I click that button i get this error:

enter image description here

enter image description here

Andrew Simpson
  • 6,883
  • 11
  • 79
  • 179

1 Answers1

2

You can not use C# code in JS file. You can render the URL in the hidden field and use it

Something like

@Html.Hidden("MyURL", @Url.Action("Lite", "Service"))

In js

$("#divProductsBanner").click(function () {
    var currentObject = $(this).text();
    $.get($("#MyURL").val(), { theName: currentObject });       
});

Refer the links and change your code accordingly.

Asp.Net Mvc Url.Action in external js file?

Use Seprate js File And use Url Helpers in it with ASP.NEt MVC 3 and Razor View Engine

Community
  • 1
  • 1
Thangaraja
  • 926
  • 6
  • 20