mhd
mhd

Reputation: 444

call mvc razor view from javascript function

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?

Upvotes: 0

Views: 2996

Answers (2)

Jorge F
Jorge F

Reputation: 693

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.

Upvotes: 2

JensB
JensB

Reputation: 6850

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.

Upvotes: 3

Related Questions