I have the following button in a View
<button id="Export" class="btn btn-default custom" type="button">
<i class="glyphicon glyphicon-file"></i>Export to Excel
</button>
And I want it to export a list of materials that I have in a ViewData
I already have the logic. The code could be in a ActionResult
or a normal method and it would be something like this:
[HttpPost]
public void Export(List<int> listPassed)
{
//stuff here
}
I tried adding href="@Url.Action("Export", "Materials", new { export_documentation = true })"
but it didn't work out.
I could do it with an override of the ActionLink
method but I would lose the glyphicon. Thing that I want to avoid.
Overrides of the ActionLink
that tricks the helper to have a glyphicon also didn't work out.
I tried Ajax but I suck at jscript and ajax and all that stuff at the moment.
Edit
@using (Html.BeginForm("Export", "Materials",
FormMethod.Post, new { listPassed = Request["SelectList"] }))
{
<button id="Export" class="btn btn-default custom" type="submit">
<i class="glyphicon glyphicon-file"></i>Export to Excel
</button>
}
This won't work out.
So, how can send the parameters to the method?
答案 0 :(得分:2)
If your list is in ViewData then the object that is passed in as the 3rd parameter can be your list with the exact name that the signature of your controller specifies.
@Url.Action("Export", "Materials", new { listPassed= ViewData.myList })
Of course this will require you to use an anchor <a href="@Url...">...</a>
instead of a button but that shouldn't be a big deal.
答案 1 :(得分:0)
If you add your list to the ViewBag
[HttpGet]
public ActionResult MyView()
{
ViewBag.SelectList = new List<int> { 1, 2, 3 };
return View();
}
Then from the view you submit it with a form. You'll need the for loop rather than a foreach because you need the index i
to identify list items.
@{
var list = ViewBag.SelectList as List<int>;
}
@using(Html.BeginForm("Export", "Materials", FormMethod.Post))
{
for (var i = 0; i < list.Count; i++)
{
@Html.HiddenFor(m => list[i], new { Name="listPassed[" + i + "]" })
}
<button type="submit">Export to Excel</button>
}
Will submit to your action
[HttpPost]
public ActionResult Export(List<int> listPassed)
{
return RedirectToAction("Success");
}