asp.net mvc Ajax.BeginForm
我在使用 Ajax.BeginForm 时遇到了一些困难,
我在视图中有类似的东西
<% 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>
<% }%}
,而操作方法是这样的,
[AcceptVerbs(HttpVerbs.Post)]
[Authorize]
public ActionResult ActionName(int id, string message)
{
....
}
我试图将“id”和“消息”传递给操作方法。我正在为routeValues 传递“null”,但我不知道要传递什么。理想情况下,我试图找到一个不需要路由值但需要 actionName 和 htmlattributes (用于表单名称)的重载,但我找不到一个。我不想将“消息”添加到视图模型中,但我确实需要其中的 FormName 用于 jquery 操作。 解决这个问题的最佳方法是什么?
哦,我忘了提,这就是我发布表格的方式
$.post($("#FormName").attr('action'), $("#FormName").serialize(),
function(result) {
$("#correspondingDiv").html(result);
}
);
I'm having some difficulties with 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 ?
Oh, I forgot to mention, This is how I post the form
$.post($("#FormName").attr('action'), $("#FormName").serialize(),
function(result) {
$("#correspondingDiv").html(result);
}
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用此重载:
http://msdn.microsoft.com/en-us/library/dd470605.aspx
Use this overload:
http://msdn.microsoft.com/en-us/library/dd470605.aspx
尝试:
我认为您使问题过于复杂,ID 和消息将从表单中的字段回发时填充,因此您无需在表单声明中指定它们。您可能还想尝试
Html.BeginForm()
,除非您确实想要 Ajax 响应。Try:
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.