6

我在 StackOverflow 上有一个类似的帖子,但也许我的误解更严重。我有一个动作 Index() 和一个 Index 查看它的渲染。从 Index() 视图取决于单击的按钮 [HttpPost]Index() 或 [HttpPost]Search() 必须被调用,因为我正在发布一些数据。发布到不同操作的唯一方法是使用 jQuery 吗?如果 jQuery 是唯一的方法,如果我的操作返回视图(完整的 Html 页面),我必须从 $.post 中清除整个文档元素并用我的视图 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();
]
4

2 回答 2

16

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

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

$('#go_button').click(function() {
    $('form').attr("action", "Search");  //change the form action
    $('form').submit();  // submit the form
});
于 2011-09-22T00:52:07.507 回答
1

当您需要更改操作以及控制器时,除了接受的答案之外:

$('#go_button').click(function() {
    $('form').attr("action", "@("Search","OtherControllerName")");  //change the form action
    $('form').submit();  // submit the form
});
于 2016-08-02T07:38:52.610 回答