如何通过html将字符串发送到控制器中. ASP.NET MVC [英] How do I send a string through html into a controller. ASP.NET MVC

查看:129
本文介绍了如何通过html将字符串发送到控制器中. ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种将数据导出到Excel的方法,称为ExportCSV().

I have a method that exports data into Excel called ExportCSV().

我想要一种具有一些附加功能的方法:ExportCSV(string searchString),其中将网页搜索栏中的字符串发送到此方法,然后可以在其中使用它.所以我的问题是,如何将搜索栏中的字符串发送到该方法?

I would like a method that has some added ability: ExportCSV(string searchString) where the string that's in the search bar on the web page is sent to this method where I can then use it. So my question is, how can I send the string in the search bar into this method?

相关代码如下.

html 的表示可以正常工作的搜索功能,但是我无法使用类似的功能来导出

the html The 's are for a search function that does work, but I can't get a similar functionality to work for exporting


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

<a class="btn btn-default" asp-action="ExportCSV">Export Table</a>


这是控制器


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

     *code for making and filling the csv to export*

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

ExportCSV()有效,但是它导出表中的所有内容. 我希望它仅导出已搜索的内容. 在我尝试过的事情中,什么都没有传递到方法中. searchString始终为空.

The ExportCSV() works, but it exports everything in the table. I want it to only export what has been searched for. In the things that I've tried, nothing is every passed into the method. searchString is always empty.

推荐答案

<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>

上面的HTML标记上的name属性需要与MVC ActionResult所采用的参数匹配,并且由于您使用的是Anchor标记,因此您只是在将人们导航到您的URL,而没有任何参数.

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");
    }

这篇关于如何通过html将字符串发送到控制器中. ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆