Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.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# 按名称和id搜索记录_C#_Asp.net Mvc_Entity Framework - Fatal编程技术网

C# 按名称和id搜索记录

C# 按名称和id搜索记录,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我正在使用ASP.NETMVC和SQLServerManagementStudio。 现在,我如何通过给定的名称和/或id获取记录 目前,我只找到一个使用id的记录,这很好,但是当按名称搜索记录时,它找不到正确的记录 我当前的代码如下所示: //Student.cs public partial class Student { public int No { get; set; } public string Name { get; set; } public stri

我正在使用ASP.NETMVC和SQLServerManagementStudio。 现在,我如何通过给定的名称和/或id获取记录

目前,我只找到一个使用id的记录,这很好,但是当按名称搜索记录时,它找不到正确的记录

我当前的代码如下所示:

//Student.cs

public partial class Student
{
    public int No { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public Nullable<decimal> mobileno { get; set; }
    public string Image { get; set; }
}
现在,我如何同时查询id和名称呢?

您可以获得双向查询

list.Where(i => i.Property == value).FirstOrDefault();       // C# 3.0+

list.Find(i => i.Property == value);                         // C# 3.0+
如果你找到任何东西,你可以得到它。如果找不到项,则返回null

请告诉我是否解决:)

更新控制器

public class CrudManuallyController : Controller
{
    public ActionResult Index()
    {

        return View(db.manages.ToList());
        //return View();
    }

    [HttpPost]
    public ActionResult search(int? id, string searchString,Student students)
    {
        //Lambda Linq to entity does not support Int32
        //var search = (from d in db.students where d.No == Convert.ToInt32(id) && d.Name == id select d).ToList();
        //var search = db.students.Where(d => d.No == Convert.ToInt32(id) && d.Name == id).ToList();
        query = db.students.AsQueryable();
        if (id.HasValue)
        {
            var studentId = id.Value;
            query = query.Where(d => d.No == studentId);
        }
        if (!string.IsNullOrEmpty(searchString))
            query = query.Where(d => d.Name.Contains(searchString));

        var search = query.ToList();
        return View("index",search);
    }
}
更新你的.cshtml

@using (Html.BeginForm("search", "CrudManually", FormMethod.Post))
{
    @Html.TextBox("id")
    @Html.TextBox("searchString")
    <br/>
    <input type="submit" value="Search"/>
}
@使用(Html.BeginForm(“搜索”,“CrudManually”,FormMethod.Post))
{
@Html.TextBox(“id”)
@文本框(“搜索字符串”)

}
list如何实现list我不知道如何使用list来查找记录名和id?您的list就是您的Queryable()!:)查询(var查询)包含您的所有学生。您可以在内部检查L2SQL是否支持int,l2e也支持int。那么问题在哪里呢?期望的结果是什么?添加了一个answer@jarakbans我希望我的答案是正确的helped@GabrielLlorico没有运行它将被授予error@jarakbans它给出了什么错误?这一行生成了一个错误
query=query.Where(d=>d.Name.Contains(searchs))请参见我更新我的code@jarakbans对不起,应该是
.Name.Contains(searchString)
。还更新了my codeLloricor:parameters dictionary包含方法System.Web.Mvc.ActionResult se的不可空类型'System.Int32'的参数'id'的空条目archhttps://stackoverflow.com/users/2122217/gabriel-llorico 为什么你要用两个文本框你能建议只使用一个文本框来显示id和姓名吗???@jarakbans哦,好吧,所以它可以是和/或,得到了it@jarakbans您想要一个文本框来同时满足
Name
搜索和
ID
搜索的需要吗?这就是你真正想要的吗?为了满足对int和string的搜索?如果搜索是
1 Jarak
,该怎么办?搜索应该如何工作?
public class CrudManuallyController : Controller
{
    public ActionResult Index()
    {

        return View(db.manages.ToList());
        //return View();
    }

    [HttpPost]
    public ActionResult search(int? id, string searchString,Student students)
    {
        //Lambda Linq to entity does not support Int32
        //var search = (from d in db.students where d.No == Convert.ToInt32(id) && d.Name == id select d).ToList();
        //var search = db.students.Where(d => d.No == Convert.ToInt32(id) && d.Name == id).ToList();
        query = db.students.AsQueryable();
        if (id.HasValue)
        {
            var studentId = id.Value;
            query = query.Where(d => d.No == studentId);
        }
        if (!string.IsNullOrEmpty(searchString))
            query = query.Where(d => d.Name.Contains(searchString));

        var search = query.ToList();
        return View("index",search);
    }
}
@using (Html.BeginForm("search", "CrudManually", FormMethod.Post))
{
    @Html.TextBox("id")
    @Html.TextBox("searchString")
    <br/>
    <input type="submit" value="Search"/>
}