使用 ASP.NET MVC 中的按钮处理页面操作的正确方法?

发布于 2024-07-30 19:59:56 字数 459 浏览 3 评论 0原文

我正在使用 ASP.NET MVC 编写一个应用程序,由于各种原因,导航是通过 HTML 输入按钮处理的。 如何处理这种情况的最佳实践是什么?

  1. 在需要发布信息的按钮上设置一个表单,并在不需要保留信息的按钮上使用 JavaScript 进行重定向

  2. 通过表单处理所有按钮,以达到迷你表单处理不需要保留信息的按钮上的导航的程度被保留

    <% 使用 (Html.BeginForm()) 
         {%> 
      <输入类型=“隐藏”名称=“控制器”值=“1”/> 
      <输入类型=“隐藏”名称=“操作”值=“位置”/> 
       
      <% } %> 
      
  3. 我在这里没有想到的事情< /p>

I'm writing an app with ASP.NET MVC where, for various reasons, the navigation is handled through HTML input buttons. What are the Best practices on how to handle this situation?

  1. Set up a Form on the buttons where information needs to be POSTed and just use JavaScript to redirect on the buttons where information doesn't need to be retained

  2. Have all buttons handled through forms, to the point where a mini-form handles navigation on the buttons where the information doesn't need to be retained

    <% using (Html.BeginForm())
       { %>
    <input type="hidden" name="controller" value="1" />
    <input type="hidden" name="action" value="Location" />
    <input id="BackButton" type="submit" value="Go Back" />
    <% } %>
    
  3. Something I haven't thought of here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烂人 2024-08-06 20:00:02

使用空的 标签和 Javascript/jQuery。 例如:

在视图中:

<script src="<%= Url.Content("~/Content/navigation.js") %>" type="text/javascript"></script>
...
<a id="back" href="#">Back</a>
<a id="next" href="#">Next</a>

<script type='text/javascript'>
    var back_href="<%= Url.Action("...") %>";
    var next_href="<%= Url.Action("...") %>";
</script>

navigation.js 中的某处:

$(document).ready(function() {
    $('a#back').attr('href', back_href);
    $('a#next').attr('href', next_href);
});

Use empty <a href="#"> tags and Javascript/jQuery. For example:

In View:

<script src="<%= Url.Content("~/Content/navigation.js") %>" type="text/javascript"></script>
...
<a id="back" href="#">Back</a>
<a id="next" href="#">Next</a>

<script type='text/javascript'>
    var back_href="<%= Url.Action("...") %>";
    var next_href="<%= Url.Action("...") %>";
</script>

Somewhere in navigation.js:

$(document).ready(function() {
    $('a#back').attr('href', back_href);
    $('a#next').attr('href', next_href);
});
温柔少女心 2024-08-06 20:00:01

3:将一些锚标记设置为看起来像按钮。

在最近的一个应用程序中,我们实现了选项 1。客户喜欢在所有表单上都有“取消”按钮(...),因此我们只是在重置输入上添加了一个单击事件,该事件执行以下操作:

history.go(-1); return false;

3: Style some anchor tags to look like buttons.

On a recent application we implemented option 1. The client liked having "Cancel" buttons on all the forms (...) so we just stuck a click event on the reset inputs which did:

history.go(-1); return false;
岛徒 2024-08-06 19:59:59
  1. JavaScript 可能是你最好的选择。

使用迷你表格看起来确实很重。 您基本上希望使用按钮作为链接(我会认真考虑为什么您打破了非常基本的网络约定:按钮发布/修改和链接导航),因此越基本,您就可以做得越好。

  1. Javascript is probably your best bet.

Using a mini form seems really heavy handed. You are basically looking to use a button as a link (I'd look seriously at why you are breaking that very basic web convention: buttons post/modify and links navigate) so the more basic you can make it the better.

分分钟 2024-08-06 19:59:58

在这里您可以找到一篇关于如何设置锚标记样式的非常好的文章像一个按钮

Here you can find a pretty good article on how to style an anchor tag like a button

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文