根据单击的按钮将相同的表单发布到不同的操作 [英] Posting same form to different actions depending on button clicked

查看:84
本文介绍了根据单击的按钮将相同的表单发布到不同的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在StackOverflow上有类似的帖子,但也许我的误解更为重要。
我有一个动作索引()和一个索引视图它的渲染。
从Index()视图,取决于单击的按钮[HttpPost]索引()或[HttpPost] Search()必须被调用因为我发布了一些数据。发布到不同操作的唯一方法是使用jQuery吗?如果jQuery是唯一的方法,如果我的操作返回视图(完整的Html页面),我必须从$ .post清理整个文档元素并用我的视图html填充它?我对这一切都很陌生,非常感谢!

I have a similar post on this on StackOverflow but perhaps my misunderstanding is more substantial. I have an action Index() and an Index view its rendering. From Index() view depending on the button clicked [HttpPost]Index() or [HttpPost]Search() must be called cause I'm posting some data. Is the only way to post to different actions is by using jQuery ? If jQuery is the only way, if my actions return views(complete Html pages), to I have to clean the whole document element from the $.post and fill it up with my views html ? I'm pretty new to all this, thanks a ton!

@using (Html.BeginForm())
{
    <input name="startDate" type="text" size="20" class="inputfield" id="datepicker" />
    <a href="#" id="apply_button">...</a>
    <a href="#" id="go_button">...</a>
}


public ActionResult Index(string lang)
{
    return View();
}

//Perhaps this action is needed
[HttpPost]
public ActionResult Index(string lang, string startDate)
{
    return View();
}

[HttpPost]
public ActionResult Search(string lang, string startDate)
{
    return View();
]


推荐答案

您可以更改表单的操作属性取决于点击了哪个按钮。

You can change the form's action attribute depending on which button was clicked.

例如要在点击转到按钮时发布到搜索操作:

e.g. To post to the 'search' action when the 'go' button is clicked:

$('#go_button').click(function() {
    $('form').attr("action", "Search");  //change the form action
    $('form').submit();  // submit the form
});

这篇关于根据单击的按钮将相同的表单发布到不同的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆