简体   繁体   English

从javascript函数调用mvc razor视图

[英]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 ? 是否有可能通过javascript函数从主视图调用另一个mvc剃刀视图,或者我们应该始终使用动作控制器? in case, there is some parameters to send to the new view, how to perform that with javasript function? 如果有一些参数要发送到新视图,如何使用javasript函数执行该操作?

You never call a View directly from Javascript. 您永远不会直接从Javascript调用视图。

You call a controller (with parameters if needed) and the controller then processes the data and returns a View. 您调用一个控制器(如果需要,可以使用参数),然后该控制器处理数据并返回一个View。

The View is always the result of a Controller and never called straight from any external front end code. View始终是Controller的结果,从不直接从任何外部前端代码中调用。 The View of a Controller Action can however use multiple Partial Views to accomplish the end result. 但是,控制器操作的视图可以使用多个局部视图来完成最终结果。

The best way is to use Partial Views. 最好的方法是使用局部视图。 For example as JensB said, you never call a view, you call a controller. 例如,正如JensB所说,您永远不会调用视图,而会调用控制器。

Javascript 使用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. 因此,在调用javascript之后,您将向控制器发送调用,并且控制器将发送指定的特定视图作为工作。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM