简体   繁体   English

带有帖子功能的MVC Html.ActionLink?

[英]MVC Html.ActionLink with post functionality?

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编写了MVC扩展,您可以在其中传递Post参数,例如:

<% 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: 这将使链接呈现为正常状态,但具有一个onClick事件,该事件将提交一个呈现的表单,该表单具有用于Action,Controller和Route Values的Action url,以及Post参数的其他隐藏输入,例如:

<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. 我讨厌在不需要时重新创建轮子。

ActionLink is just for creating an <a> . ActionLink仅用于创建<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). 如果不是那么最好将链接设置为表单内的提交按钮,而不是使用javascript(javascript和电子邮件不相处很好)。

You could create the form and appende it to the end of the DOM. 您可以创建表单并将其附加到DOM的末尾。 This could be done through partial view or through javascript. 这可以通过部分视图或通过javascript完成。

Honestly I suggest you don't use a POST. 老实说,我建议你不要使用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. 如果您保留大部分数据并且只需要检索所述数据所需的ID,则不应该在actionlink中传递太多数据。

Ajax.ActionLink works perfectly fine for a post request. Ajax.ActionLink非常适合发布请求。 To refresh page, you can create a function that refreshes page (eg function reload(){ windows.location.reload();} ). 要刷新页面,您可以创建一个刷新页面的函数(例如, 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. 注意:您需要引用相应的脚本以使用ajax或jQuery代码。

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>   
                ) &nbsp;&nbsp;
    <%} %> 

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

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