Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 错误:值不能为null。参数名称:Value_C#_Asp.net Mvc_Entity Framework - Fatal编程技术网

C# 错误:值不能为null。参数名称:Value

C# 错误:值不能为null。参数名称:Value,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我创建了一个搜索页面。有一个专门化字段,其中数据通过EF传递 这是控制器: public ActionResult Search(string search) { DataTable dt; dt = this.GetSPDataTable("getAssociateData"); List<Associate> list = new List<Associate>(); list = (from DataRow dr in dt.Row

我创建了一个搜索页面。有一个专门化字段,其中数据通过EF传递

这是控制器:

public ActionResult Search(string search)
{
    DataTable dt;
    dt = this.GetSPDataTable("getAssociateData");

    List<Associate> list = new List<Associate>();
    list = (from DataRow dr in dt.Rows
            select new Associate
            {
                AssociateId = Convert.ToInt32(dr["AssociateId"]),
                Name = dr["Name"].ToString(),
                Phone = Convert.ToInt64(dr["Phone"]),
                Address = dr["Address"].ToString(),
                SpecializationId = Convert.ToInt32(dr["SpecializationId"]),
                SpcName = dr["SpcName"].ToString()
            }).ToList();
    return View(list.Where(x => x.Name.Contains(search) || search == null).ToList());
    //return View(db.Associates.Where(x => x.Name.Contains(search) || search == null).ToList());
公共操作结果搜索(字符串搜索)
{
数据表dt;
dt=这个.GetSPDataTable(“getAssociateData”);
列表=新列表();
列表=(来自dt.Rows中的数据行dr
选择新员工
{
AssociateId=Convert.ToInt32(dr[“AssociateId”]),
Name=dr[“Name”].ToString(),
Phone=Convert.ToInt64(dr[“Phone”]),
Address=dr[“Address”].ToString(),
SpecializationId=Convert.ToInt32(dr[“SpecializationId”]),
SpcName=dr[“SpcName”].ToString()
}).ToList();
返回视图(list.Where(x=>x.Name.Contains(search)| | search==null.ToList());
//返回视图(db.Associates.Where(x=>x.Name.Contains(search)| | search==null.ToList());
以下是视图:

@foreach (var item in Model)
{

    <tr>
        <td>
            @item.Name
        </td>
        <td>
            @item.Phone
        </td>
        <td>
            @item.Address
        </td>
        <td>
            @item.SpcName
        </td>
@foreach(模型中的变量项)
{
@项目名称
@项目.电话
@项目.地址
@item.SpcName
最后,模型:

[Key]
public int AssociateId { get; set; }

[Required(ErrorMessage = "You must provide a name")]
[MaxLength(10)]
public string Name { get; set; }

[Required(ErrorMessage = "Please Write Your Number ")]
[RegularExpression(@"^([0-9]{10})$", ErrorMessage = "Invalid Mobile Number.")]
public Nullable<long> Phone { get; set; }

[Required(ErrorMessage = "Where do you dwell")]
[MaxLength(50)]
public string Address { get; set; }

[Display(Name = "Specialization")]
[Required(ErrorMessage = "Select any of them")]
public Nullable<int> SpecializationId { get; set; }

[Display(Name = "Specialization")]
public string SpcName { get; set; }
[Key]
public int AssociateId{get;set;}
[必需(ErrorMessage=“您必须提供名称”)]
[MaxLength(10)]
公共字符串名称{get;set;}
[必需(ErrorMessage=“请写下您的号码”)]
[RegularExpression(@“^([0-9]{10})$”,ErrorMessage=“无效手机号码”)]
公共可空电话{get;set;}
[必需(ErrorMessage=“您住在哪里”)]
[MaxLength(50)]
公共字符串地址{get;set;}
[显示(Name=“专门化”)]
[必需(ErrorMessage=“选择其中任何一个”)]
公共可空的SpecializationId{get;set;}
[显示(Name=“专门化”)]
公共字符串SpcName{get;set;}
我得到一个空值错误。

正如@CamiloTerevinto所建议的:

检查
search
是否为
null
,然后尝试按其进行筛选

使用三元运算符(
?:
):

或在三行上:

if(search == null)
    return View(list);
return View(list.Where(x => x.Name.Contains(search)).ToList();

我之前曾提出以下建议,但下面的建议效率较低:


.Where(x=>x.Name.Contains(search)| | search==null)
为什么在尝试使用该值后要检查
null
?另外,您用实体框架对此进行了标记,但正在加载到数据表?这也没有任何意义。按照Camilo Terevinto的建议,在循环之前检查null,尝试使用
。Where(x=>search==null | | x.Name.Contains(search))
代替。这样,如果
search
null
,它将不会尝试调用
Contains(null)
@Rafalon与其遍历整个集合只是为了检查
search==null
,更好的方法是检查
search!=null
,然后执行
Where
if(search == null)
    return View(list);
return View(list.Where(x => x.Name.Contains(search)).ToList();
.Where(x => search == null || x.Name.Contains(search))