MVC Html.ActionLink 具有发布功能吗?

发布于 2024-12-21 10:04:07 字数 697 浏览 3 评论 0原文

我正在检查是否有人为 Html.ActionLink 编写了 MVC 扩展,您可以在 Post 参数中传递这样的扩展

<% Html.ActionLink("Click me", "Index", "Home", new { MyRouteValue = "123" }, null, new { postParam1 = "a", postParam2 = "b" }); %>

:操作、控制器和路由值以及来自帖子参数的附加隐藏输入,如下所示:

<a href="#" onClick="$('#theform').submit(); return false;">Click me</a>
<form id="theform" action="/Home/Index/123" method="post">
   <input type="hidden" name="postParam1" value="a">
   <input type="hidden" name="postParam2" value="b">
</form>

我希望将用户重定向到可能包含大量数据的各个页面。不仅从一页到另一页,而且从电子邮件到另一页。这将是高度可重用的,我认为会清理很多代码,并且如果它已经在那里浮动的话,会节省大量编写它的时间。我讨厌在不必要的时候重新造轮子。

I'm checking to see if anyone has written an MVC extension for Html.ActionLink that you can pass in Post parameters like such:

<% Html.ActionLink("Click me", "Index", "Home", new { MyRouteValue = "123" }, null, new { postParam1 = "a", postParam2 = "b" }); %>

That would render the link like normal but having an onClick event that submits an also rendered form with an Action url for the Action, Controller, and Route Values with additional hidden inputs from the Post Parameters like such:

<a href="#" onClick="$('#theform').submit(); return false;">Click me</a>
<form id="theform" action="/Home/Index/123" method="post">
   <input type="hidden" name="postParam1" value="a">
   <input type="hidden" name="postParam2" value="b">
</form>

I'm looking to redirect users to various pages with potentially a lot of data. Not only from page to page, but from email to page also. This would be highly reusable and I think would clean up a lot of code, and would save a bunch of time writing this if its already floating around out there. I hate recreating the wheel when I don't have to.

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

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

发布评论

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

评论(3

我最亲爱的 2024-12-28 10:04:07

ActionLink 仅用于创建 。如果您所要求的内容已经在表单内,那么它就会爆炸。如果不是,那么最好将链接设置为表单内的提交按钮,而不是使用 javascript(javascript 和电子邮件不能很好地相处)。

您可以创建表单并将其附加到 DOM 的末尾。这可以通过部分视图或通过 javascript 来完成。

老实说,我建议你不要使用 POST。如果您保留了大部分数据并且只拥有检索所述数据所需的 id,那么您永远不必在操作链接中传递太多数据。

ActionLink is just for creating an <a>. What you are asking for would blow up if it is already inside of a form. If it isn't then it is preferable to make the link the submit button inside the form and NOT use javascript (javascript and emails don't get along great).

You could create the form and appende it to the end of the DOM. This could be done through partial view or through javascript.

Honestly I suggest you don't use a POST. If you persist most of the data and just have the ids needed to retrieve said data, you should never have to pass too much data in an actionlink.

佼人 2024-12-28 10:04:07

Ajax.ActionLink 对于发布请求来说效果非常好。要刷新页面,您可以创建一个刷新页面的函数(例如function reload(){ windows.location.reload();})。
它看起来像这样。

@Ajax.ActionLink("DiaplyName", "Action", new { parameters to post }, new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, OnComplete="reload();"})

注意:您需要引用适当的脚本才能使用 ajax 或 jQuery 代码。

Ajax.ActionLink works perfectly fine for a post request. To refresh page, you can create a function that refreshes page (e.g. function reload(){ windows.location.reload();}).
It would look something like this.

@Ajax.ActionLink("DiaplyName", "Action", new { parameters to post }, new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, OnComplete="reload();"})

Note: You'll need to reference the appropriate scripts to use ajax or jQuery code.

被翻牌 2024-12-28 10:04:07

这段代码对我很有帮助,拯救了我的一天。我改进了它,它对模拟用户有帮助。下面是我所做的。

  <% if (Session["SessionUserImpersonate"] != null && Session["SessionUserImpersonate"] != "" && Session["SessionUserImpersonate"] == "Yes")
    {
        BLL.Models.User userold = new BLL.Models.User();
        userold = (BLL.Models.User)Session["SessionUserOld"];      
        %>
    <span class="FL">(Impersonated as <%=Website.Backoffice.SessionHelper.Session_User.UserName != null ? Website.Backoffice.SessionHelper.Session_User.UserName:"" %>  , </span>

    <form class="FL" id='frmid' action="/Index/Login?username=<%=userold.UserName%>&password=<%=userold.Password%>&IsImpersonate=No"  method="post">
                <a class="TxtRed" style="cursor:pointer;" onclick="$('#frmid').submit(); return false;" > - finish impersonated session  </a>
                </form>   
                )   
    <%} %> 

This piece of code was helpful for me and saved my day.. I improved it and it helped me for Impersonated user.. here is bellow ,what i did..

  <% if (Session["SessionUserImpersonate"] != null && Session["SessionUserImpersonate"] != "" && Session["SessionUserImpersonate"] == "Yes")
    {
        BLL.Models.User userold = new BLL.Models.User();
        userold = (BLL.Models.User)Session["SessionUserOld"];      
        %>
    <span class="FL">(Impersonated as <%=Website.Backoffice.SessionHelper.Session_User.UserName != null ? Website.Backoffice.SessionHelper.Session_User.UserName:"" %>  , </span>

    <form class="FL" id='frmid' action="/Index/Login?username=<%=userold.UserName%>&password=<%=userold.Password%>&IsImpersonate=No"  method="post">
                <a class="TxtRed" style="cursor:pointer;" onclick="$('#frmid').submit(); return false;" > - finish impersonated session  </a>
                </form>   
                )   
    <%} %> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文