0

I'm trying to let users export data from the database to an excel file. I have a ProductController with a List action that receives information from search inputs. Like this:

[HttpGet]
public ActionResult List()
{
  //Displays the view with a SearchForm element
  return View();
}

[HttpPost]
public ActionResult List(FilterViewModel filter)
{
  //Queries the database applying the filter
  //and returns to the same view (it will output an html table now)
  return View(databaseQueryResults);
}

My problem is that in order to download a file I want to apply the same filters selected on the List action. My View goes like this:

<form id="SearchProductForm" action="/Product/List" method="POST">
  <!-- A few form controls go here in order to specify a criteria for searching -->
  <input type="submit" value="Search" />
  <button type="button">Export</button>
</form>

At this point my form has a submit button that will POST the form to the List method. However, when I press "Export" I need the form to be posted to a different action.

Sure, I can dinamically change the action method of my form but that doesn't seem the right way (or is it?).

The other option I can think of is to place my export button outside the form. But then I would need to have another form (a copy of my search form) so that it could be posted.

For all I know I can't start a download through Ajax. Or can I? If I could I would serialize the form and send to the second action.

So, how can I use one form for those two different situations in the best way possible?

Thanks for helping.

ataravati
  • 8,891
  • 9
  • 57
  • 89
Rodrigo Lira
  • 1,489
  • 3
  • 14
  • 28

0 Answers0