Answered by:
Error: The resource cannot be found.

Question
-
User-14642827 posted
I'm try to export one record to excel from my webgrid, my error is The resource cannot be found. /RecorderLogs/Export Row/1
I added a row with a actionlink but is not working:
controller code:
public void PrintExcel(int? id) { List<RecorderLog> recorderLog = new List<RecorderLog>(); //var logs = recorderLog.Where(x => x.STRID == id).ToList(); using (BrentTestEntities dbc = new BrentTestEntities()) { recorderLog = dbc.RecorderLogs.ToList(); var logs = recorderLog.Where(x => x.STRID == id).ToList(); } // create object to webgrid WebGrid grid = new WebGrid(source: recorderLog, canPage: false, canSort: false); string gridData = grid.GetHtml( columns: grid.Columns( grid.Column("STRNUMBER", "STR Number"), grid.Column("EquipmentRentalDetailedInfoID", "Equipment Rental Detailed Info ID"), grid.Column("VehicleTag", "Vehicle Tag"), grid.Column("ReservationNumber", "Reservation Number"), grid.Column("FuelDate", "Fuel Date"), grid.Column("GallonsConsumed", "Gallons Consumed"), grid.Column("FuelCost", "Fuel Cost") )).ToString(); Response.AddHeader("content-dispostion", "attachment/vnd.ms-excel; filename=report.xls"); // specify content type Response.ContentType = "application/vnd.ms-excel"; // write excel data using this method Response.Write(gridData); Response.End(); }
and the View:
<body> @{ FuelRecorderLog.Domain.Entities.RecorderLog recorderLog = new FuelRecorderLog.Domain.Entities.RecorderLog(); } @{ var fuelLogGrid = new WebGrid(Model, canPage: true, rowsPerPage: 10, selectionFieldName: "selectedRow", ajaxUpdateContainerId: "gridContent"); fuelLogGrid.Pager(WebGridPagerModes.NextPrevious); } <div id="gridContent"> @fuelLogGrid.GetHtml(tableStyle: "webGrid", headerStyle: "webgrid-header", footerStyle: "webgrid-footer", alternatingRowStyle: "alt", selectedRowStyle: "select", columns: fuelLogGrid.Columns( fuelLogGrid.Column( columnName: "STRNumberID", header: "STR Number", canSort: true, format: item => Html.ActionLink( ((int)item.STRID).ToString(), "RecorderLog Details", "Details", new { id = item.STRID }, null)), fuelLogGrid.Column("STRNUMBER", "STR Number"), fuelLogGrid.Column(header: "Export", format: item => Html.ActionLink("Export", "Export Row", new { @id = item.STRID })), <--error fuelLogGrid.Column("EquipmentRentalDetailedInfoID", "Equipment Rental Detailed Info ID"), fuelLogGrid.Column("VehicleTag", "Vehicle Tag"), fuelLogGrid.Column("ReservationNumber", "Reservation Number"), fuelLogGrid.Column("FuelDate", "Fuel Date"), fuelLogGrid.Column("GallonsConsumed", "Gallons Consumed"), fuelLogGrid.Column("FuelCost", "Fuel Cost"), fuelLogGrid.Column(format: @<input type="button" value="View Record" onclick="window.location.href = 'GenerateExcel.html';" />) )) @if (fuelLogGrid.HasSelection) { recorderLog = (FuelRecorderLog.Domain.Entities.RecorderLog)fuelLogGrid.Rows[fuelLogGrid.SelectedIndex].Value; <b>STRNUMBER</b>@recorderLog.STRNUMBER<br /> <b>EquipmentRentalDetailedInfoID</b>@recorderLog.EquipmentRentalDetailedInfoID<br /> <b>VehicleTag</b>@recorderLog.VehicleTag<br /> <b>ReservationNumber</b>@recorderLog.ReservationNumber<br /> <b>FuelDate</b>@recorderLog.FuelDate<br /> <b>FuelCost</b>@recorderLog.FuelCost<br /> } </div> <div> @Html.ActionLink("Print Excel", "PrintExcel", new { @class = "btn btn-success" }) </div> </body>
What is my error???
Wednesday, March 22, 2017 4:29 PM
Answers
-
User-359936451 posted
Try something like this, add this to a button click event with a gridview on an aspx page.
{ Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls"); Response.Charset = ""; Response.ContentType = "text/csv; charset-UTF-8"; // Response.ContentType = "application/vnd.ms-excel" StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); //GridView1.AllowPaging = False //GridView1.DataBind() //Change the Header Row back to white color GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF"); //Apply style to Individual Cells //' ''GridView1.HeaderRow.Cells(0).Style.Add("background-color", "red") //' ''GridView1.HeaderRow.Cells(1).Style.Add("background-color", "red") //' ''GridView1.HeaderRow.Cells(2).Style.Add("background-color", "red") //' ''GridView1.HeaderRow.Cells(3).Style.Add("background-color", "red") for (int x = 0; x <= GridView1.Columns.Count - 1; x++) { //Apply style to Individual Cells GridView1.HeaderRow.Cells(x).Style.Add("color", "black"); } for (int i = 0; i <= GridView1.Rows.Count - 1; i++) { GridViewRow row = GridView1.Rows(i); //Change Color back to white row.BackColor = System.Drawing.Color.White; //Apply text style to each Row row.Attributes.Add("class", "textmode"); //Apply style to Individual Cells of Alternating Row if (i % 2 != 0) { for (int y = 0; y <= GridView1.Columns.Count - 1; y++) { row.Cells(y).Style.Add("background-color", "#C2D69B"); //row.Cells(1).Style.Add("background-color", "#C2D69B") //row.Cells(2).Style.Add("background-color", "#C2D69B") //row.Cells(3).Style.Add("background-color", "#C2D69B") } } } GridView1.RenderControl(hw); //style to format numbers to string string style = "<style>.textmode{mso-number-format:\\@;}</style>"; // HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default Response.Write(style); Response.Output.Write(sw.ToString()); Response.Flush(); Response.End(); }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, March 22, 2017 8:26 PM
All replies
-
User-359936451 posted
Try something like this, add this to a button click event with a gridview on an aspx page.
{ Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls"); Response.Charset = ""; Response.ContentType = "text/csv; charset-UTF-8"; // Response.ContentType = "application/vnd.ms-excel" StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); //GridView1.AllowPaging = False //GridView1.DataBind() //Change the Header Row back to white color GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF"); //Apply style to Individual Cells //' ''GridView1.HeaderRow.Cells(0).Style.Add("background-color", "red") //' ''GridView1.HeaderRow.Cells(1).Style.Add("background-color", "red") //' ''GridView1.HeaderRow.Cells(2).Style.Add("background-color", "red") //' ''GridView1.HeaderRow.Cells(3).Style.Add("background-color", "red") for (int x = 0; x <= GridView1.Columns.Count - 1; x++) { //Apply style to Individual Cells GridView1.HeaderRow.Cells(x).Style.Add("color", "black"); } for (int i = 0; i <= GridView1.Rows.Count - 1; i++) { GridViewRow row = GridView1.Rows(i); //Change Color back to white row.BackColor = System.Drawing.Color.White; //Apply text style to each Row row.Attributes.Add("class", "textmode"); //Apply style to Individual Cells of Alternating Row if (i % 2 != 0) { for (int y = 0; y <= GridView1.Columns.Count - 1; y++) { row.Cells(y).Style.Add("background-color", "#C2D69B"); //row.Cells(1).Style.Add("background-color", "#C2D69B") //row.Cells(2).Style.Add("background-color", "#C2D69B") //row.Cells(3).Style.Add("background-color", "#C2D69B") } } } GridView1.RenderControl(hw); //style to format numbers to string string style = "<style>.textmode{mso-number-format:\\@;}</style>"; // HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default Response.Write(style); Response.Output.Write(sw.ToString()); Response.Flush(); Response.End(); }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, March 22, 2017 8:26 PM -
User-2057865890 posted
Hi BthJ6,
Html.ActionLink("Export", "Export Row", new { @id = item.STRID })), <--errorpublic static MvcHtmlString ActionLink( this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues )
The second parameter is actionName.
public void PrintExcel(int? id)Since the actionName is PrintExcel, modify the code like this
Html.ActionLink("Export", "PrintExcel", new { @id = item.STRID })
Take a look at the previous thread https://forums.asp.net/t/2117960.aspx
Best Regards,
Chris
Thursday, March 23, 2017 5:32 AM