Skip to content Skip to sidebar Skip to footer

How Do I Send A String Through Html Into A Controller. ASP.NET MVC

I have a method that exports data into Excel called ExportCSV(). I would like a method that has some added ability: ExportCSV(string searchString) where the string that's in the se

Solution 1:

<form method="GET" asp-action="ExportCSV">    
    <input type="text" placeholder="Chem Name" name="cheminventory2String" value="@ViewData["Search"]" id="SearchString" />
    <input type="submit" value="Search" class="btn btn-default" />

    <button type="submit" class="btn btn-default">Export Table</a>
</form>

The name attribute on your HTML tag above needs to match the parameter your MVC ActionResult is taking, and since you were using an Anchor tag you were simply navigating people to your URL, without any parameters.

It should be wrapped inside of a form tag and then the anchor link should be replaced with a submit button.

You can then use your searchstring like below:

public FileContentResult ExportCSV(string cheminventory2String)
    {
         var dataTable = from m in _context.ChemInventory2.Include(c => c.Chemical).Include(c => Location).Include(c => c.Order)
                         where cheminventory2String == m.Chemical.Name select m;

         return File(export.ExportToBytes(), "text/csv", "Chemical Inventory.csv");
    }

Post a Comment for "How Do I Send A String Through Html Into A Controller. ASP.NET MVC"