Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.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# Validate在控制器操作的StudentTable中存在StudentID_C#_Sql Server_Asp.net Mvc_Controller - Fatal编程技术网

C# Validate在控制器操作的StudentTable中存在StudentID

C# Validate在控制器操作的StudentTable中存在StudentID,c#,sql-server,asp.net-mvc,controller,C#,Sql Server,Asp.net Mvc,Controller,当StudentId作为控制器操作中的参数传递时,如何验证传递的id是否存在于控制器操作中的StudentTable中 public ActionResult LookUpStudentId(string id) { if(id != //not present in StudentTable) return new RedirectResult("~/Error/NotFound"); return View()

当StudentId作为控制器操作中的参数传递时,如何验证传递的id是否存在于控制器操作中的StudentTable中

        public ActionResult LookUpStudentId(string id)
    {

        if(id != //not present in StudentTable)
          return new RedirectResult("~/Error/NotFound");


        return View();

    }

您的问题的答案在很大程度上取决于您用来访问SQL server的数据访问技术。因为您还没有告诉我们如何使用普通ADO.NET实现这一点:

public bool IsStudentExists(string id)
{
    using (var conn = new SqlConnection("some connection string"))
    using (var cmd = conn.CreateCommand())
    {
        conn.Open();
        cmd.CommandText = "SELECT id FROM StudentTable WHERE id = @id";
        cmd.Parameters.AddWithValue("@id", id);
        using (var reader = cmd.ExecuteReader())
        {
            return reader.Read();
        }
    }
}
然后:

public ActionResult LookUpStudentId(string id)
{
    if(!IsStudentExists(id))
    {
        return new RedirectResult("~/Error/NotFound");
    }
    return View();
}
显然,最好在存储库中重构此数据访问代码,这样控制器就不会与正在使用的数据访问技术紧密耦合。例如,您可以定义一个
IStudentsResposition

public interface IStudentsRepository
{
    Student GetStudent(string id);
}
然后您将实现它,现在您的控制器操作可以使用此抽象:

public class StudentsController: Controller
{
    private readonly IStudentsRepository _repository;
    public StudentsController(IStudentsRepository repository)
    {
        _repository = repository;
    }

    public ActionResult LookUpStudentId(string id)
    {
        var student = _repository.GetStudent(id);
        if(student == null)
        {
            return new RedirectResult("~/Error/NotFound");
        }
        return View(student);
    }
}

剩下的就是将此存储库的正确实现注入控制器。

您的问题的答案在很大程度上取决于您用于访问SQL server的数据访问技术。因为您还没有告诉我们如何使用普通ADO.NET实现这一点:

public bool IsStudentExists(string id)
{
    using (var conn = new SqlConnection("some connection string"))
    using (var cmd = conn.CreateCommand())
    {
        conn.Open();
        cmd.CommandText = "SELECT id FROM StudentTable WHERE id = @id";
        cmd.Parameters.AddWithValue("@id", id);
        using (var reader = cmd.ExecuteReader())
        {
            return reader.Read();
        }
    }
}
然后:

public ActionResult LookUpStudentId(string id)
{
    if(!IsStudentExists(id))
    {
        return new RedirectResult("~/Error/NotFound");
    }
    return View();
}
显然,最好在存储库中重构此数据访问代码,这样控制器就不会与正在使用的数据访问技术紧密耦合。例如,您可以定义一个
IStudentsResposition

public interface IStudentsRepository
{
    Student GetStudent(string id);
}
然后您将实现它,现在您的控制器操作可以使用此抽象:

public class StudentsController: Controller
{
    private readonly IStudentsRepository _repository;
    public StudentsController(IStudentsRepository repository)
    {
        _repository = repository;
    }

    public ActionResult LookUpStudentId(string id)
    {
        var student = _repository.GetStudent(id);
        if(student == null)
        {
            return new RedirectResult("~/Error/NotFound");
        }
        return View(student);
    }
}
剩下的就是将此存储库的正确实现注入控制器。

请参阅我的教程的第页

公共操作结果删除(int id=0) { Movie Movie=db.Movies.Find(id); 如果(电影==null) { 返回HttpNotFound(); } 返回视图(电影); }

请参见我的教程的

公共操作结果删除(int id=0) { Movie Movie=db.Movies.Find(id); 如果(电影==null) { 返回HttpNotFound(); } 返回视图(电影);
}

@user793468,太好了,所以只需使用EntityFramework实现我展示的
IStudentsRepository
。例如,您可以有一个
StudentsRepositoryEF
,在这里您可以直接返回给定id的EF模型。@user793468,太好了,所以只需实现我使用EntityFramework显示的
IStudentsRepository
。例如,您可以有一个
StudentsRepositoryEF
,其中您可以直接返回给定id的EF模型。