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();
]
MJ Richardson
  • 1,441
  • 12
  • 30
mishap
  • 8,176
  • 14
  • 61
  • 92
  • @DavidLaberge: Just added the code. Please have a look. – mishap Sep 22 '11 at 01:13
  • possible duplicate of [How can I assign different actions to same html form?](http://stackoverflow.com/questions/3999627/how-can-i-assign-different-actions-to-same-html-form) – Meryovi Jul 31 '13 at 17:02

2 Answers2

16

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
});
MJ Richardson
  • 1,441
  • 12
  • 30
  • But im posting same textfield content to different action depending on the button clicked. How can I have two different forms without duplicating the html please ? Thank you! Btw I added the code in the original question. – mishap Sep 22 '11 at 01:15
  • Thanks! You made it! Nobody else could help me with this. – mishap Sep 22 '11 at 13:30
1

Additional to accepted answer when you are in need to change action as well as Controller too:

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