簡體   English   中英

如何在 ASP.NET MVC 的錄取表格成功頁面上顯示來自 model 的 id?

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

我已經制作了錄取表格,因此在提交表格后,它會重定向到另一個名為“成功”的頁面,我想顯示學生 ID 或提交的每個錄取表格的主 ID,這是在 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:

 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();
    }

看法:

   @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-
        

所以我只想為只提交錄取表格的人顯示特定的 ID。

所以,我已經更新了你想要實現的代碼。 我在您現有的代碼中添加了以下注釋。 這可能對你有用。

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();
        }

看法:

@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