Say I have the following form that's in classic asp:
假设我有一个经典的asp:
<form name="impdata" id="impdata" method="POST" action="http://www.bob.com/dologin.asp">
<input type="hidden" value="" id="txtName" name="txtName" />
</form>
I need to simulate the action of submitting the form in asp.net mvc3, but I need to modify the hidden value before submitting. Is it possible to do this from the action or in another way?
我需要在asp.net mvc3中模拟提交表单的动作,但是我需要在提交之前修改隐藏值。是否有可能从行动或其他方式来做?
What I have so far...
到目前为止我拥有的……
View:
观点:
@using (Html.BeginForm("Impersonate", "Index", FormMethod.Post, new { id = "impersonateForm" }))
{
<input id="Impersonate" class="button" type="submit" value="Impersonate" action />
<input type="hidden" value="" id="txtName" name="txtName" />
}
Controller:
控制器:
[HttpPost]
public ActionResult Impersonate(string txtName)
{
txtName = txtName + "this string needs to be modified and then submitted as a hidden field";
//Redirect won't work needs the hidden field
//return Redirect("http://www.bob.com/dologin.asp");
}
Solution:
解决方案:
Seems that it isn't easy to do this from the controller so I ended up using jQuery. The action returns a JsonResult. Something like:
似乎从控制器上做这件事并不容易,所以我最终使用了jQuery。该操作返回一个JsonResult。喜欢的东西:
<button id="Impersonate" class="button" onclick="Impersonate()">Impersonate!</button>
<form name="impdata" id="impersonateForm" action="http://www.bob.com/dologin.asp">
<input type="hidden" value="" id="txtName" name="txtName" />
</form>
function Impersonate() {
$.ajax({
type: 'POST',
asynch: false,
url: '@Url.Action("Impersonate", "Index")',
data:
{
name: $('#txtName').val()
},
success: function (data) {
$('#txtName').val(data.Name);
$('#impersonateForm').submit();
}
});
Seems to work well...
似乎工作得很好…
2 个解决方案
#1
1
It is rather hard to redirect to a POST from a POST (relies on HTTP status codes without universal support), and it is impossible from a GET.
从POST重定向到POST是相当困难的(依赖HTTP状态码而没有通用的支持),而且从GET转到POST是不可能的。
The simplest solution is probably a little JavaScript on the result that posts the (new) form.
最简单的解决方案可能是在发布(新)表单的结果上添加一点JavaScript。
Thus you action method returns a view with the necessary data in it (passed via the model from the controller) which will include the JavaScript.
因此,action方法返回一个视图,其中包含必要的数据(通过控制器的模型传递),其中包含JavaScript。
#2
1
You can try something like this(using jquery):
您可以尝试以下方法(使用jquery):
<input id="Impersonate" class="button" type="submit" value="Impersonate" onclick="$('#txtName').val('new value')" />
#1
1
It is rather hard to redirect to a POST from a POST (relies on HTTP status codes without universal support), and it is impossible from a GET.
从POST重定向到POST是相当困难的(依赖HTTP状态码而没有通用的支持),而且从GET转到POST是不可能的。
The simplest solution is probably a little JavaScript on the result that posts the (new) form.
最简单的解决方案可能是在发布(新)表单的结果上添加一点JavaScript。
Thus you action method returns a view with the necessary data in it (passed via the model from the controller) which will include the JavaScript.
因此,action方法返回一个视图,其中包含必要的数据(通过控制器的模型传递),其中包含JavaScript。
#2
1
You can try something like this(using jquery):
您可以尝试以下方法(使用jquery):
<input id="Impersonate" class="button" type="submit" value="Impersonate" onclick="$('#txtName').val('new value')" />