asp.net-mvc asp.net mvc Ajax.BeginForm
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2600036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
asp.net mvc Ajax.BeginForm
提问by Bala R
I'm having some difficulties with Ajax.BeginForm
我在使用 Ajax.BeginForm 时遇到了一些困难
I have something like this in a view
我有这样的看法
<% using (Ajax.BeginForm("ActionName", null , null, new { id = "FormName" }))
{%>
<input type="hidden" value = '<%= Html.Encode( Model.id) %>' name="id"/>
<textarea id="message" name=message rows="4" style="width: 90%">
</textarea>
<% }%}
And the action method is something like this
而动作方法是这样的
[AcceptVerbs(HttpVerbs.Post)]
[Authorize]
public ActionResult ActionName(int id, string message)
{
....
}
I'm trying to pass the 'id' and 'message' to the action method. I'm passing 'null' for routeValues but I dont know what to pass. Ideally I was trying to find an overload that did not require route values but took actionName and htmlattributes (for form name) but I could not find one.I don't want to add 'message' to the view-model and I do need the FormName in there for jquery operations. What is the best way to work around this problem ?
我正在尝试将 'id' 和 'message' 传递给 action 方法。我正在为 routeValues 传递 'null',但我不知道该传递什么。理想情况下,我试图找到一个不需要路由值但采取 actionName 和 htmlattributes(用于表单名称)的重载,但我找不到一个。我不想向视图模型添加“消息”,我确实需要用于 jquery 操作的 FormName 。解决此问题的最佳方法是什么?
Oh, I forgot to mention, This is how I post the form
哦,我忘了提,这就是我发布表格的方式
$.post($("#FormName").attr('action'), $("#FormName").serialize(),
function(result) {
$("#correspondingDiv").html(result);
}
);
回答by Dave Swersky
Use this overload: http://msdn.microsoft.com/en-us/library/dd470605.aspx
使用此重载:http: //msdn.microsoft.com/en-us/library/dd470605.aspx
Ajax.BeginForm(
string "ActionName",
string "ControllerName",
new routevalues {id="IDValue",message="MyMessage"},
new AjaxOptions {OnBegin=[someFunction], OnFailure=[failureFunction] },
new { id = "FormName" }
)
回答by Lazarus
Try:
尝试:
<% using (Ajax.BeginForm("ActionName", null))
{%>
<input type="hidden" value = '<%= Html.Encode( Model.id) %>' name="id"/>
<textarea id="message" name=message rows="4" style="width: 90%">
</textarea>
<% }%}
I think you are over-complicating the the issue, ID and Message will get populated on the postback from the fields in the form so you don't need to specify them in the form declaration. You might also want to try Html.BeginForm()
instead unless you really want an Ajax response.
我认为您使问题过于复杂,ID 和 Message 将在表单中的字段的回发中填充,因此您无需在表单声明中指定它们。Html.BeginForm()
除非您真的想要 Ajax 响应,否则您可能还想尝试一下。