说我有以下形式这在传统的ASP:如何使用隐藏字段提交表单提交表单?
<form name="impdata" id="impdata" method="POST" action="http://www.bob.com/dologin.asp">
<input type="hidden" value="" id="txtName" name="txtName" />
</form>
我需要模拟提交在asp.net MVC3形式的行动,但我需要在提交之前修改隐藏的价值。是否有可能通过行动或其他方式做到这一点?
我有什么那么远,
查看:
@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" />
}
控制器:
[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");
}
解决方案:
似乎是不容易从控制器做到这一点,所以我最终使用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();
}
});
似乎运作良好...
请问这种帮助ü? http://msdn.microsoft.com/en-us/library/debx8sh9.aspx – VJAI 2012-07-27 07:43:38
@Mark我想我会去上面的jQuery和ajax解决方案。似乎很难从控制器中完成。 – woggles 2012-07-27 10:54:41