Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.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# 控制入院审批和未审批状态_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# 控制入院审批和未审批状态

C# 控制入院审批和未审批状态,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,大家好,我正在MVC中制作一个大学网站项目,它有一张录取表,所以每当用户填写并提交录取表时,它就会进行审批,用户会收到他的录取代码来检查他/她的帐户审批状态,所以我在这里遇到了一个问题 守则: 型号: public partial class admission_tbl { public int adm_id { get; set; } //- by this user checks his status// [Required] [StringLength(50)]

大家好,我正在MVC中制作一个大学网站项目,它有一张录取表,所以每当用户填写并提交录取表时,它就会进行审批,用户会收到他的录取代码来检查他/她的帐户审批状态,所以我在这里遇到了一个问题

守则:

型号:

  public partial class admission_tbl
{
    public int adm_id { get; set; } //- by this user checks his status//
    [Required]
    [StringLength(50)]
    public string std_name { get; set; }
    [Required]
    [StringLength(50)]
    public string std_father { get; set; }
    [Required]
    [StringLength(50)]
    public string std_mother { get; set; }
    [AgeRangeValidation(ErrorMessage = "Age must be between 18 - 28", MinAge = 18, MaxAge = 28)]
    [Required]
    [DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]

    public System.DateTime DOB { get; set; }
    [Required]
    public string std_gender { get; set; }
    [Required]
    public string R_address { get; set; }
    [Required]
    public string P_address { get; set; }
    [Required]
    public string adm_for { get; set; }
    [Required]
    public string university { get; set; }
    [Required]
    public string E_no { get; set; }
    [Required]
    public string Center { get; set; }
    [Required]
    public string City { get; set; }
    [Required]
    public string Field { get; set; }
    [Required]
    public string Marks_secured { get; set; }
    [Required]
    public string out_of { get; set; }
    [Required]
    public string Class_obtained { get; set; }

    public string Sports_details { get; set; }
    public string adm_status { get; set; } //--This is the method which i am using to control account 
                                                approval or non approval status.
           }}
 public ActionResult Status_check()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Status_check(admission_tbl svm)
    {
        admission_tbl u = db.admission_tbl.Where(a => a.adm_id == svm.adm_id).SingleOrDefault();


        if (u.adm_status == "Approved")
        {
            ViewBag.Message1 = "Your account is Approved";
        }
        else if (u.adm_status == "Unapproved")
        {
            ViewBag.Message = "Your account is not Approved ";

        }
        else
        {
            ViewBag.Message = "Your account is in pending please wait";
        }

        return View();
    }
控制器:

  public partial class admission_tbl
{
    public int adm_id { get; set; } //- by this user checks his status//
    [Required]
    [StringLength(50)]
    public string std_name { get; set; }
    [Required]
    [StringLength(50)]
    public string std_father { get; set; }
    [Required]
    [StringLength(50)]
    public string std_mother { get; set; }
    [AgeRangeValidation(ErrorMessage = "Age must be between 18 - 28", MinAge = 18, MaxAge = 28)]
    [Required]
    [DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]

    public System.DateTime DOB { get; set; }
    [Required]
    public string std_gender { get; set; }
    [Required]
    public string R_address { get; set; }
    [Required]
    public string P_address { get; set; }
    [Required]
    public string adm_for { get; set; }
    [Required]
    public string university { get; set; }
    [Required]
    public string E_no { get; set; }
    [Required]
    public string Center { get; set; }
    [Required]
    public string City { get; set; }
    [Required]
    public string Field { get; set; }
    [Required]
    public string Marks_secured { get; set; }
    [Required]
    public string out_of { get; set; }
    [Required]
    public string Class_obtained { get; set; }

    public string Sports_details { get; set; }
    public string adm_status { get; set; } //--This is the method which i am using to control account 
                                                approval or non approval status.
           }}
 public ActionResult Status_check()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Status_check(admission_tbl svm)
    {
        admission_tbl u = db.admission_tbl.Where(a => a.adm_id == svm.adm_id).SingleOrDefault();


        if (u.adm_status == "Approved")
        {
            ViewBag.Message1 = "Your account is Approved";
        }
        else if (u.adm_status == "Unapproved")
        {
            ViewBag.Message = "Your account is not Approved ";

        }
        else
        {
            ViewBag.Message = "Your account is in pending please wait";
        }

        return View();
    }

因此,在“状态检查”中,用户输入其准入代码以检查帐户是否已批准,所有操作是否正常,但问题是,当我尝试输入一个随机数输入时,它会抛出空异常错误,而我想抛出一个消息错误,例如“无效准入代码不存在”我抓不到它我缺少什么?

对于
svm.adm\u id
入院记录中不存在的情况

admission_tbl u = db.admission_tbl.SingleOrDefault(a => a.adm_id == svm.adm_id);
.SingleOrDefault()
将返回NULL。因此,当您尝试从
u
访问
adm\u status
时(该值为空),您将获得
NullReferenceException

要解决此问题,需要添加验证以检查
u
对象在继续访问其内部属性之前不能为null

[HttpPost]
public ActionResult Status_check(admission_tbl svm)
{
    admission_tbl u = db.admission_tbl.Where(a => a.adm_id == svm.adm_id).SingleOrDefault();

    // Validate u cannot be null, if null do error handling
    if (u == null)
    {
        ViewBag.Message = "Invalid Admission Code no such exists";
        return View();
    }

    if (u.adm_status == "Approved")
    {
        ViewBag.Message1 = "Your account is Approved";
    }
    else if (u.adm_status == "Unapproved")
    {
        ViewBag.Message = "Your account is not Approved ";

    }
    else
    {
        ViewBag.Message = "Your account is in pending please wait";
    }

    return View();
}
注: 此声明

db.admission_tbl.Where(a => a.adm_id == svm.adm_id).SingleOrDefault();
可替换为:

db.admission_tbl.SingleOrDefault(a => a.adm_id == svm.adm_id);
两者都将返回相同的结果,如果找不到记录,则返回null

第二编辑版本: 您可以在ViewBag中绑定错误消息,或添加场景的任何处理并立即退出

ViewBag.Message = "Invalid Admission Code no such exists";
return View();

这回答了你的问题吗?是的,我对此进行了检查,但仍然没有找到任何解决方案。感谢@yong的详细解释,但是有可能在同一页面上只抛出一条错误消息而不是抛出异常吗?嗨@Mac,是的,当然,只需将
抛出新异常()
替换为
ViewBag.message=“无效的准入代码不存在”
。我更新了代码。你可以参考一下。谢谢。是的,这是我的答案!再次感谢@yong