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# 为什么';在mvc中搜索操作工作?_C#_Asp.net Mvc - Fatal编程技术网

C# 为什么';在mvc中搜索操作工作?

C# 为什么';在mvc中搜索操作工作?,c#,asp.net-mvc,C#,Asp.net Mvc,我从几个小时以来就一直遇到这个问题。当我点击提交按钮时,它没有响应,只是刷新页面 索引控制器 public ActionResult Index() { 返回视图(db.Students.ToList()); } [HttpGet,ActionName(“索引”)] 公共操作结果搜索索引(字符串选项,字符串搜索) { 如果(选项==“名称”) { var a=db.Students.Where(x=>x.StudentName==search | | search==null); 返回视图(a.

我从几个小时以来就一直遇到这个问题。当我点击提交按钮时,它没有响应,只是刷新页面

索引控制器

public ActionResult Index()
{
返回视图(db.Students.ToList());
}
[HttpGet,ActionName(“索引”)]
公共操作结果搜索索引(字符串选项,字符串搜索)
{
如果(选项==“名称”)
{
var a=db.Students.Where(x=>x.StudentName==search | | search==null);
返回视图(a.ToList());
}
否则,如果(选项==“性别”)
{
返回视图(db.Students.Where(x=>x.Gender==search.ToList());
}
其他的
{
返回视图(db.Students.Where(x=>x.RegNo==search | | search==null.ToList())
}
}
索引视图

@使用(Html.BeginForm(“Index”、“Student”、FormMethod.Get)){
搜索者:@Html.RadioButton(“选项”、“名称”)名称
@单选按钮(“选项”、“性别”)性别
@单选按钮(“选项”、“部门”)部门
@RadioButton(“选项”、“RegNo”)RegNo
}
如何解决这个问题?

创建一个帖子。GET用于请求数据。您正试图发回需要回发的数据(搜索参数)

 [HttpGet]
 public ActionResult SearchIndex()
    {
        return View();
    }

[HttpPost,ActionName("Index")]
 public ActionResult SearchIndex(string option, string search)
    {
      if (option == "Name")
      {
        var a = db.Students.Where(x => x.StudentName == search || search == null);

        return View(a.ToList());
      }
      else if (option == "Gender")
      {
        return View(db.Students.Where(x => x.Gender == search).ToList());

      }
      else
      {
        return View(db.Students.Where(x => x.RegNo == search || search == null).ToList()) 
      }
    }
html还需要更新以使用FormMethod.Post

 @using (Html.BeginForm("Index", "Student", FormMethod.Post, new { encType = "multipart/form-data" }))

编辑 再想一想,我认为您只需要将多部分/表单数据添加到html中

@using (Html.BeginForm("Index", "Student", FormMethod.Get, new { encType = "multipart/form-data" }))

我认为这个代码可以提供更多的结果

public ActionResult SearchIndex(string search)
{
var students = from s in db.Students
                   select s;
    if (!String.IsNullOrEmpty(search))
    {
        students = students.Where(s => s.StudentName.Contains(search)
                               || s.Gender.Contains(search));
    }
return View(students.ToList());

}
我不知道您的要求,但当您将数据搜索到db时,您只需要该数据中的一个或两个属性

@using(Html.BeginForm("Index","Student",FormMethod.Get)){
    <div id="search">
        <input type="text" name="search" />
        <input type="submit"  name="submit" value="Search" class="btn btn-default"/>
    </div>
}
@使用(Html.BeginForm(“Index”、“Student”、FormMethod.Get)){
}

GET可以发回数据,也可以发布数据。
encType=“multipart/form data”
允许上传文件。在OP的情况下(他们没有文件输入),这是不必要的。您的输入未与控制器连接。输入中的名称必须与搜索字符串同名,如
name=“search”
。在该方法的第一行放置断点。当你提交时它会被击中吗?参数的值是什么,尤其是“search”?我刚刚用name=“search”更改了输入标记,它可以工作,但当我搜索姓名/性别的第一个字母时,它会显示出来blanks@roni这是因为您选中了
x.Gender==search
:换句话说,性别必须等于搜索文本。也许
x.Gender.StartsWith(search)
会有用,但我不确定