c# - 在一个 TextBox Asp.Net Mvc 中搜索多个项目
问题描述
首先提前感谢这个社区提供的帮助我有一个问题,我的客户想一次性更新数据库中的记录。所以解决方案是他在Excel中用“,”分隔他的对象,然后复制/粘贴到一个文本框,问题是查询对单个记录工作正常,但对于许多它不起作用。
下面的模型代码
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
namespace ctivation.Models { public partial class BDD { [Required] [StringLength(50)] public string CODE { get; 放; }
[Required]
[StringLength(50)]
public string NOM { get; set; }
[Required]
[StringLength(50)]
public long TYPE{ get; set; }
[Key]
public Long NBR { get; set; }
public bool STATUS { get; set; }
[Required]
public string OBSERVATION { get; set; }
[Column(TypeName = "date")]
public DateTime? DATE_MAJ { get; set; }
}
}
控制器:
public ActionResult Index(string search)
{
var query = _context.BDD.ToList();
if (search == null)
query = _context.Database.SqlQuery<BDD>("select * from [BDD] where CONVERT (varchar,[NBR])IN(@p0)", search).ToList();
else
{
string x = addComa(search);
query = _context.BDD.SqlQuery("select * from [BDD] where CONVERT (varchar,[NBR])IN (@search)", new SqlParameter("@search", x)).ToList();
}
return View(query);
}
和视图
@model IEnumerable<SimActivation.Models.BDD_ARPT>
@{
ViewBag.Title = "Test";
}
<h2>Search</h2>
@using (Html.BeginForm("Index", "BDD"))
{
<b>@Html.TextBox("search")
<input type = "submit" name = "submit"value = "Search" /></b>
}
... table code
what i want is something like that were the number's are multiple records in the Database [enter image description here][1]
[1]: https://i.stack.imgur.com/zL4hA.png
thanks again for the help
解决方案
你可以使用 Linq 来做到这一点:
var values = search.Split(",");
query = _context.BDD.Where(b => values.Contains(b.NBR)).ToList();
并且不要忘记包括System.Linq
:
using System.Linq;
推荐阅读
- python - 堆积散点图
- javascript - Slickgrid:删除列文本
- python - 将 pcap 文件转换为重新组装的流(json)
- c++ - 我在使用 getline 时遇到问题
- python - 如何将视频从我的 Kivy android 应用程序流式传输到处理数据的服务器?
- ruby - 有没有办法可以在 ruby 中调用另一个类的方法?
- python - 如何使实体在pygame中因颜色碰撞而受到伤害?
- php - Microsoft 从 PHP 8 起终止对 PHP 的支持
- javascript - 如何将 32 位整数集合中的随机整数存储在紧凑的数据结构中以快速检查成员资格?
- react-native - 您的 Android App Bundle 使用错误的密钥签名