我有 MVC4 应用程序,其中包含 View 和部分 View 。
1)查看页面(Home.cshtml、Index.cshtml)
2) PartialViews(View1.cshtml,View2.cshtml,View3.cshtml)
3) 我在 www.example.com/Home 页面中有 3 个按钮(Button1、Button2、Button3)
按钮是
<button id="btn1" type="button" onclick="btn1Click(this);"> View1 </button>
<button id="btn2" type="button" onclick="btn2Click(this);"> View1 </button>
<button id="btn3" type="button" onclick="btn3Click(this);"> View1 </button>
单击这些按钮时,我会在主页(内容部分)中显示相应的部分 View 。我在 .js 文件 中的 Javascript 函数(btn1Click(button))中编写了该代码。如果我们有更多的部分 View 要显示在主页的内容中,那么我们如何使用 jQuery/Javascript 跟踪这些部分 View 的路径(我们的项目文件路径)。 如果我们使用
Window.location.href
我们将获得主网址(www.example.com/Home page),但是当用户单击这些按钮(button1/button2/button3)时,我需要找到部分 View 路径,除了项目路径(使用 jQuery/Javascript 的 ProjectName/PartilaViews/View1.cshtml) 或 (ProjectName/PartilaViews/View2.cshtml) 或 (ProjectName/PartilaViews/View3.cshtml) 。
我不想更改功能/程序(上述流程),
最佳答案
试试这个:
window.location.href='@Url.Action("action name", "controller name")'
来自你的 Controller 的actionresult将返回部分 View 。
------------------------------------------------------------ - - - - - - - 或者 - - - - - - - - - - - - - - - - - - -------------------------------------------
var partial = '@Html.Partial("Partial view name")'
变量partial
将从部分 View 返回html。
关于javascript - 使用 jQuery/Javascript 在 MVC 4 中查找内容页面 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24907699/