Jonathan Greene Jonathan Greene - 11 months ago 31
C# Question

Need help getting Submit button to run code in function in C# using MVC 5

First off let me start by saying I have been looking for this answer for a few hours. I have started by looking through the books I own and trying to decode what is happening. Next I check a few blogs and posts online that had a similar issue. I don't fully understand the Razor Syntax so that is part of my problem but I also feel there is a logic issue with how I am trying to do this. Any help is greatly appreciated.

When I click download I just get "the resource cannot be found"

Below is my code from the view. Feel free to be as harsh as possible I am learning and don't fully understand all this yet.

<td>
@Html.ActionLink("Edit", "Edit", new { id=item.cusName }) |
@Html.ActionLink("Details", "Details", new { id=item.cusName }) |
@Html.ActionLink("Delete", "Delete", new { id=item.cusName })


<br />

@using (Html.BeginForm("Download", "RMAFormModelsController", FormMethod.Post, new { id = "Download" }))
{
<div id="convertAboutPageButtonDiv">
<input type="submit" value="Download to Excel File" />
</div>
}
<br />

</td>


below is the code from the C# function.

//Export to excel
[HttpPost]
public ActionResult Download()
{

List<Lookup> lookupList = new List<Lookup>();
var grid = new System.Web.UI.WebControls.GridView();

grid.DataSource = lookupList;
grid.DataBind();

Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=YourFileName.xlsx");
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
grid.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();

return View();
}


This all seems like it should work. What am I miss-understanding?

Thank you all for your help!

Answer Source

Update RMAFormModels instead of RMAFormModelsController in form's html helper.

  @using (Html.BeginForm("Download", "RMAFormModels", FormMethod.Post))
   {
       <div id="convertAboutPageButtonDiv">
            <input type="submit" value="Download to Excel File" />
       </div>
   }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download