简体   繁体   English

MVC/JavaScript/Razor - 如何从按钮打开新视图

[英]MVC/JavaScript/Razor - How to open a new View from a button

I'm working in a .Net framework using the MVC pattern.我正在使用MVC模式的.Net 框架中工作。 I have a link (that works) that looks like this:我有一个看起来像这样的链接(有效)

<li><a href="@Url.Action("HomePage", "Home")">Main Page</a></li>

In the above link, the "HomePage" is the name of the .cshtml page and there is a method in the controller with the same name that returns the view.在上面的链接中,“HomePage”是 .cshtml 页面的名称,并且控制器中有一个同名的方法返回视图。 The second parameter, "Home", is the subfolder under Views where the .cshtml page is located.第二个参数“Home”是 .cshtml 页面所在的 Views 下的子文件夹。

Now, in a different section of the same view, I have a button that should navigate to the same location when clicked.现在,在同一视图的不同部分,我有一个按钮,单击时应导航到同一位置。 It looks like this:它看起来像这样:

<input type="button" class="navigationButtons" id="btnHome" value="Main Page" />

Because I am using a .css style sheet for the navigationButtons, I would really LOVE to put the code to go the the Main Page from the button in the JavaScript.因为我为导航按钮使用了 .css 样式表,所以我真的很想将代码从 JavaScript 中的按钮转到主页。 So, I've created a jQuery variable and binded it to the .on("click", onHome) event.因此,我创建了一个 jQuery 变量并将其绑定到 .on("click", onHome) 事件。 I have the following method:我有以下方法:

onHome = function () {
    var url = '@Url.Action("HomePage", "Home")';
    window.location.href = url;
}

I've also tried window.open("HomePage", "Home");我也试过 window.open("HomePage", "Home"); Along with anything else I could find, to no avail.连同我能找到的任何其他东西,都无济于事。 Important: I do not want the view to open in a new window;重要提示:我不想在新窗口中打开视图; it is supposed to open exactly as it does in the link (which displays the view inside the framework view, like an embedded view under the Main Menu which belongs to the Main application page).它应该完全按照链接中的方式打开(它显示框架视图内的视图,就像属于主应用程序页面的主菜单下的嵌入视图)。

I've learned that if I use a button type instead of an input type in the html that I can include a link for onClick but I'm having trouble with that as well.我了解到,如果我在 html 中使用按钮类型而不是输入类型,我可以包含 onClick 的链接,但我也遇到了问题。 It appears all examples contain a complete url.似乎所有示例都包含一个完整的 url。 But I'm trying to open a view in the framework.但我试图在框架中打开一个视图。 I do not have a complete url.我没有完整的网址。

Does anybody have any ideas on how to make this type of navigation from JavaScript?有人对如何从 JavaScript 进行这种类型的导航有任何想法吗? If not, or there is a better way in the html, then I am eager to get a response.如果没有,或者html中有更好的方法,那么我很想得到回应。

你能在你的'navigationButtons'类中使用你的actionlink Html helper吗?

@Html.ActionLink("Home", "Index", "Home", new { @class = "navigationButtons" })

只需在标签中添加类。

<a href="@Url.Action("HomePage", "Home")" class="navigationButtons" autocomplete="off" data-loading-text="Loading Main Page...">Main Page</a>

我使用这个锚 <a href="@Url.Action("View File cshtml file", "ViewFolder")">

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

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