首页 > 解决方案 > 在一个 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 

标签: c#asp.net-mvcentity-frameworksearch

解决方案


你可以使用 Linq 来做到这一点:

var values = search.Split(",");  
query = _context.BDD.Where(b => values.Contains(b.NBR)).ToList();

并且不要忘记包括System.Linq

using System.Linq;

推荐阅读