0

is it possible to call another mvc razor view from the main view via javascript function or we should always use the action controller ? in case, there is some parameters to send to the new view, how to perform that with javasript function?

0

2 Answers 2

3

You never call a View directly from Javascript.

You call a controller (with parameters if needed) and the controller then processes the data and returns a View.

The View is always the result of a Controller and never called straight from any external front end code. The View of a Controller Action can however use multiple Partial Views to accomplish the end result.

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

4 Comments

in my case, i have a main view contains a gridpanel. when i click one element of the grid panel, i want to create a popup window to show some details about the selected element. why should i get back to the server and again the client side ??
You can either open a separate page which would be a separate Action that you request from the server or you could have the extra data hidden in a div that you display when the user clicks the link
Or use a lightbox that shows your extra data: lokeshdhakar.com/projects/lightbox2/?u=9
elements showed in the grid panel with few properties when clicked they will be showed in new window with some other properties so i cant use lightbox
2

The best way is to use Partial Views. For example as JensB said, you never call a view, you call a controller.

Javascript

function GetPartialView(parameter){    
    var url = "@Url.Action("PartialView", "Controller", new { parameter= "-parameter" })";
    url = url.replace("-parameter", parameter);
    //HTML element to load the partial view 
    $("#DivElement").load(url);
}

Controller

    public ActionResult PartialView()
    {
        //Code you need to return to the partial view...
        return PartialView("partialview");
    }

So after the javascript is called, you are sending a call to the controller and the controller make it's work to send the specific view you specified. Hope this helps.

2 Comments

thank you Jorge, can i use partial view to create popup windows ?
Well If you can use a Jquery plugin that handle popup and html content, I could imagine it's posible. Maybe when a plugin handle callbacks, after showing the callback you can load your partialview into the modal (depends on the modal of course)

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.