Shinji
Shinji

Reputation: 85

Is my approach correct in using the asp mvc controller and javascript like this?

At the moment I have a MasterPage in an ASP.NET MVC3 project with a animesearch function

function AnimeSearch() {
                alert(document.getElementById('anime').value);
                window.location = "Paging/AnimeBySearch?searchstring=" + (document.getElementById('anime').value);
            }

What I do , I type in an anime movie in an html input tag and it returns the correct values accordingly.

As one can see , is that my JavaScript function is calling my controller and then the function with the correct parameters(from the input).

However a couple of questions.

First since this function is on my masterpage and the controller call is pretty static the following of course happens. When I get my result after for instance searching for "naruto" I am in the paging controller. If I want another anime movie then of course because of my static location the controller does not work anymore. This is my first question , what is the cleanest and proper way of handling this(no hacks please , did that , works but is not good coding )?

Second Is my approach correct ( calling the controller and action like this from javascript )?

Upvotes: 2

Views: 87

Answers (2)

jim tollan
jim tollan

Reputation: 22485

i would suggest that you think about a rewrite of the implementation and setup a controller and view that queries the anime model. this controller would implement an action method that allowed for paging and would return a partialview that was emitted into a predefined div on your anime search view. i'm in transit on iphone at present, otherwise would leave code example.

will update when on terra firma...

Upvotes: 0

rick schott
rick schott

Reputation: 21117

function getBaseUrl()
{
   return "@Url.Content("~/")";
}

function AnimeSearch(baseUrl) {
   alert(document.getElementById('anime').value);
   window.location = getGetBaseUrl() + "/Paging/AnimeBySearch?searchstring=" +     (document.getElementById('anime').value);
}

Upvotes: 2

Related Questions