简体   繁体   English

如何在 ASP.NET MVC 的录取表格成功页面上显示来自 model 的 id?

[英]How to display id from model on a admission form success page in ASP.NET MVC?

I have made a admission form so after on submitting the form it redirects to another page called "Success" there I want to display the student id or the primary id for each admission form is submitted which is generated in the model my codes are below:我已经制作了录取表格,因此在提交表格后,它会重定向到另一个名为“成功”的页面,我想显示学生 ID 或提交的每个录取表格的主 ID,这是在 model 中生成的,我的代码如下:

Model : Model

 public class AdmissionModel
   {
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Std_id { get; set; }  <-- **THIS I want to show in success page** -->

    [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; }
    [Required]
    [DataType(DataType.Date)]
    public string DOB { get; set; }

    public string std_gender { get; set; }
    [Required]
    [StringLength(200)]
    public string R_address { get; set; }
    [Required]
    [StringLength(200)]
    public string P_address { get; set; }
  
    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; }
 
    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; }
    [Required]
    public string Sports_details { get; set; }

    public string adm_status { get; set; }

}

Controller: Controller:

 public class AdmissionController : Controller
   {
    contextclass db = new contextclass();        
    public ActionResult Admission()
    {
        return View();
    }
    
    [HttpPost]
    public ActionResult Admission(AdmissionModel adm)
    {
        AdmissionModel a = new AdmissionModel();
        a.std_name = adm.std_name;
        a.std_father = adm.std_father;
        a.std_mother = adm.std_mother;
        a.DOB = adm.DOB;
        a.std_gender = adm.std_gender;
        a.R_address = adm.R_address;
        a.P_address = adm.P_address;
        a.adm_for = adm.adm_for;
        a.university = adm.university;
        a.E_no = adm.E_no;
        a.Center = adm.Center;
        a.City = adm.City;
        a.Field = adm.Field;
        a.Marks_secured = adm.Marks_secured;
        a.out_of = adm.out_of;
        a.Class_obtained = adm.Class_obtained;
        a.Sports_details = adm.Sports_details;
        db.a_model.Add(a);
        db.SaveChanges();
        
        return RedirectToAction("Success", "Admission");
    }

    public ActionResult Success()
    {
    
        return View();
    }

View:看法:

   @model IEnumerable<ITM_ColLege.Models.AdmissionModel>

    @{
       ViewBag.Title = "Success";
       Layout = "~/Views/Shared/_Layout.cshtml";
      }

     <h2>Admission form successfully submitted</h2>
     <h1>Please wait for your admission approval check your status from the menu your admission code 
       is  -show id here-
        

so I want to display specific id only for one who submits the admission form only.所以我只想为只提交录取表格的人显示特定的 ID。

So, I have updated the code of what you want to achieve.所以,我已经更新了你想要实现的代码。 I added comments below to your existing code.我在您现有的代码中添加了以下注释。 This might work for you.这可能对你有用。

CONTROLLER: CONTROLLER:

 public class AdmissionController : Controller
       {
        contextclass db = new contextclass();        
        public ActionResult Admission()
        {
            return View();
        }
        
        [HttpPost]
        public ActionResult Admission(AdmissionModel adm)
        {
            AdmissionModel a = new AdmissionModel();
            a.std_name = adm.std_name;
            a.std_father = adm.std_father;
            a.std_mother = adm.std_mother;
            a.DOB = adm.DOB;
            a.std_gender = adm.std_gender;
            a.R_address = adm.R_address;
            a.P_address = adm.P_address;
            a.adm_for = adm.adm_for;
            a.university = adm.university;
            a.E_no = adm.E_no;
            a.Center = adm.Center;
            a.City = adm.City;
            a.Field = adm.Field;
            a.Marks_secured = adm.Marks_secured;
            a.out_of = adm.out_of;
            a.Class_obtained = adm.Class_obtained;
            a.Sports_details = adm.Sports_details;
            db.a_model.Add(a);
            db.SaveChanges();
            
            // Pass in the std_id to the action
            return RedirectToAction("Success", "Admission", new {id = a.Std_id});
        }
    
        // Pass in the id as a parameter and store it in ViewBag
        public ActionResult Success(int id)
        {
            ViewBag.Id = id;
            return View();
        }

VIEW:看法:

@model IEnumerable<ITM_ColLege.Models.AdmissionModel>

    @{
       ViewBag.Title = "Success";
       Layout = "~/Views/Shared/_Layout.cshtml";
      }

     <h2>Admission form successfully submitted</h2>
     <h1>Please wait for your admission approval check your status from the menu your admission code 
       is  @ViewBag.Id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM