javascript - 根据单击的按钮将相同的表单发布到不同的操作

标签 javascript jquery asp.net-mvc posting

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

@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();
]

最佳答案

您可以根据单击的按钮更改表单的操作属性。

例如单击“开始”按钮时发布到“搜索”操作:

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

关于javascript - 根据单击的按钮将相同的表单发布到不同的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7508233/

相关文章:

jquery - CSS3 过渡计时功能

c# - LocalDb 和 IdentityDbContext Entity Framework 错误 26

c# - 获取没有 2 个 foreach 函数的相关实体 ID

javascript - Node.js:自定义命令行界面

javascript - 使用 Angular.js 和 PHP 时过滤器功能无法按预期工作

javascript - JQuery 追加无法正常工作

c# - 注入(inject)对象的两个实例

javascript - 替换文本区域中多次出现的字符串 (url)

javascript - D3.JS SVG 圆圈中的数据驱动颜色

javascript - DOM 更改后,单击时 jQuery 不起作用