wayfarer
wayfarer

Reputation: 790

asp.net mvc open another view in javascript

Migrating my brain from WebForms to MVC, I want to:

1) click on a button, that will 2) execute some javascript, that will 3) open a new window (or tab, whichever the browser is set for), that will show an existing view from the same solution, and 4) have that view execute some data loads

I'm stuck on 3 and 4

3) in this:

function foo(){
var wrkURL = globalURLVarFromViewOneThatPointsToViewTwo
window.open(wrkURL,"_blank")
}

how do I specify the URL of another existing view (ViewTwo) in the same solution? Is there a helper that I can call within ViewOne that will create that URL within the calling view, and load into globalURLVarFromViewOneThatPointsToViewTwo? Or, if I have to spell out the URL for ViewTwo, what does that syntax look like?

4) How do I get ViewTwo to automatically do some data operations (like find data for a FlexGrid) before or immediately on being displayed? I know how to do an Ajax call to a controller/action on document.ready; is there some other way of calling a controller/action and loading the view.bag, as the view is displayed, instead?

Upvotes: 0

Views: 6456

Answers (2)

Mister Epic
Mister Epic

Reputation: 16733

For building the Url, look at the URL helper:

@Url.Action("ActionName", "Controller", new { 
    someVariable= someData
}) 

As for #4, notice that when I'm building the URL, I'm building it with route parameters. These parameters will be passed to your controller's action and you can do what you please with them (just make sure routing is configured so you'll get the appropriate match for your intended action).

Upvotes: 2

Ufuk Hacıoğulları
Ufuk Hacıoğulları

Reputation: 38478

Actually action methods respond to URLs. So you need to create a URL for the action method. Make sure you return that specific view from the action.

var wrkURL = '@Url.Action("SomeAction", "SomeController")';

Upvotes: 2

Related Questions