javascript - 使用 jQuery/Javascript 在 MVC 4 中查找内容页面 URL

标签 javascript jquery asp.net-mvc asp.net-mvc-4 tracker

我有 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/

相关文章:

javascript - 这是在 JavaScript 中引用对象的安全方法吗?

javascript - 在克隆表中查找可见行?

jquery-animate - 可以使 jquery.live() 与负载一起工作吗?

jquery - css 适用于除 IE 以外的所有其他浏览器

asp.net-mvc - IIS Express - 'mimeMap' 上的 HTTP 错误 500.19 0x800700b7( 'fileExtension' 设置为 '.svg')

asp.net-mvc - Web API 2 需要尾部斜杠才能使自定义属性路由工作

javascript - 存储本 map 片以便在另一个页面上传

javascript - 为什么 include() 在我的代码中不能正常工作?

javascript - 如果点击目标不是它本身或其使用 jQuery 的子元素,则隐藏元素

c# - 如何在 C# 中解析 JSON 对象