Jonathan Greene Jonathan Greene - 2 years ago 66
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.

@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" />
<br />


below is the code from the C# function.

//Export to excel
public ActionResult Download()

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

grid.DataSource = lookupList;

Response.AddHeader("content-disposition", "attachment; filename=YourFileName.xlsx");
Response.ContentType = "application/";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);

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" />
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download