6

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

2 に答える 2

16

クリックされたボタンに応じて、フォームのアクション属性を変更できます。

たとえば、「go」ボタンがクリックされたときに「search」アクションに投稿するには:

$('#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 に答える