ASP.NET MVC 搜索+保存搜索结果.2个按钮共用一个表单
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ASP.NET MVC 搜索+保存搜索结果.2个按钮共用一个表单相关的知识,希望对你有一定的参考价值。
想要实现的效果,1.搜索功能 2.搜索结果保存到text功能
前台代码
一个表单2个按钮,通过JS代码来修改form的action,来实现调用不同的后台代码
<form name="form1" action="~/Card/List" method="get" > <span class="label label-success">卡号:</span> <input class="form-inline" type="text" name="CardNum" placeholder="请输入要查询的卡号" value="@Request.QueryString["CardNum"]" style="width:260px" /> <span class="label label-success">面值:</span> <input type="text" name="Price" placeholder="" value="@Request.QueryString["Price"]" style="width:100px" /> <span class="label label-success">状态:</span> <select name="Status"> <option value="2" @(Request.QueryString["Status"] == "2" ? "selected=‘selected‘" : "")> 全部 </option> <option value="-1" @(Request.QueryString["Status"] == "-1" ? "selected=‘selected‘" : "")> 冻结 </option> <option value="0" @(Request.QueryString["Status"] == "0" ? "selected=‘selected‘" : "")> 已用 </option> <option value="1" @(Request.QueryString["Status"] == "1" ? "selected=‘selected‘" : "")> 未用 </option> </select> <span class="label label-success">创建日期:</span> <input type="date" class="inline laydate-icon" name="CreateDateTime" value="@Request.QueryString["CreateDateTime"]" /> <button type="submit" class="btn btn-success" onclick="mysearch()">查询</button> <button id="SaveText" class="btn btn-success" onclick="myexport()">导出查询结果</button> </form>
JS代码
function mysearch() { document.form1.action = "/Card/List"; } function myexport() { document.form1.action = "/Card/Export"; }
后台代码
后台用到了mvcpager分页,用的htmlget方式
[CheckLogin] [HttpGet] public ActionResult List(string cardnum, decimal? Price, int? Status, DateTime? CreateDateTime, int id = 1) { return list(cardnum, Price, Status, CreateDateTime, id); } [CheckLogin] [HttpGet] public ActionResult Export(string cardnum, decimal? Price, int? Status, DateTime? CreateDateTime, int id = 1) { CardService cardS = new CardService(); var model = cardS.Search2(cardnum, Price, Status, CreateDateTime); var items= list(cardnum, Price, Status, CreateDateTime, id); string str = string.Empty; foreach (var item in model) { str = str + item.CardNum+"----"+item.Price+"----"+item.CreateDateTime+"----"+item.Status + "\r\n"; } byte[] data = Encoding.UTF8.GetBytes(str); return File(data, "text/plain", "卡密.txt"); } private ActionResult list(string cardnum, decimal? Price, int? Status, DateTime? CreateDateTime,int id = 1) { CardService cardS = new CardService(); var model = cardS.Search2(cardnum, Price, Status, CreateDateTime); PagedList<CardDTO> InfoPager = model.ToPagedList(id, 10); InfoPager.TotalItemCount = model.Count(); InfoPager.CurrentPageIndex = id; CardSearchDTO model2 = new CardSearchDTO(); model2.Cards = InfoPager; if (Request.IsAjaxRequest()) return PartialView("_AjaxSearchGet", model2); return View("List",model2); }
以上是关于ASP.NET MVC 搜索+保存搜索结果.2个按钮共用一个表单的主要内容,如果未能解决你的问题,请参考以下文章