Advertisement
stronk_8s

asp.net_internal

Mar 1st, 2024 (edited)
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ASP 5.16 KB | Source Code | 0 0
  1. microsoft.visualstudio.web.codegeneration.design
  2. microsoft.aspnetcore.component.quickgrid.entityframeworkadapter
  3.  
  4. //program.cs
  5.  
  6. builder.Services.AddDbContext<DataContext>(opt => opt.UseSqlServer(builder.Configuration.GetConnectionString("ConnectionString")));
  7.  
  8. //optional session hoy to j
  9. builder.Services.AddSession();
  10.         OR
  11. builder.Services.AddSession(options => { options.IdleTimeout = TimeSpan.FromMinutes(1); });
  12. builder.Services.AddDistributedMemoryCache();
  13.  
  14. app.UseSession();
  15.  
  16.  
  17.  
  18.  
  19. [HttpPost]
  20. public async Task<IActionResult> Edit(Student student)
  21. {  
  22.     Student s = await _context.Students.FindAsync(student.RollNo);
  23.    
  24.     if (s != null)
  25.     {
  26.  
  27.         s.Name = student.Name;
  28.         s.BirthDate = student.BirthDate;
  29.         s.Password = student.Password;
  30.         s.ConfirmPassword = student.ConfirmPassword;
  31.         s.Username = student.Username;
  32.         await _context.SaveChangesAsync();
  33.         return RedirectToAction(nameof(Index));
  34.  
  35.     }
  36.  
  37.     return View(s);
  38. }
  39.  
  40.  
  41.  
  42. //login
  43.  
  44.  
  45. //with session
  46.     [HttpPost]
  47.     public IActionResult Login(Student student)
  48.     {
  49.  
  50.         Student cust = _context.Students.Where(c => c.Username == student.Username && c.Password == student.Password).FirstOrDefault();
  51.         Marksheet mark = _context.Marksheets.Find(cust.RollNo);
  52.         if (cust != null)
  53.         {
  54.             HttpContext.Session.SetString("customer", cust.Username);
  55.             ViewBag.Message = "success";
  56.             if (mark != null)
  57.                 return RedirectToAction(nameof(Details), nameof(Marksheet), new { id = cust.RollNo });
  58.             else
  59.                 return RedirectToAction(nameof(Create), nameof(Marksheet));
  60.         }
  61.         else
  62.             ViewBag.Message = "Login Failed...";
  63.  
  64.         return View();
  65.     }
  66.  
  67.  
  68.     public IActionResult LogOut()
  69.     {
  70.         if (HttpContext.Session.GetString("customer") != null)
  71.         {
  72.             HttpContext.Session.Remove("customer");
  73.             return RedirectToAction("Login");
  74.         }
  75.         return View();
  76.     }
  77.  
  78.  
  79. //without session
  80. [HttpPost]
  81.     public IActionResult Login(Student student)
  82.     {
  83.         Student cust = _context.Students.Where(c => c.Username == student.Username && c.Password == student.Password).FirstOrDefault();
  84.         if(s != null)
  85.             return RedirectToAction("Index");
  86.         else
  87.             Viewbag.Message="failed...";
  88.     }
  89.  
  90.  
  91.  
  92.  
  93. //cshtml
  94.     <a asp-action="LogOut"><input type="button" value="LogOut"/></a> //with session
  95.     <a href="Index"><input type="button" value="LogOut"/></a> //without session
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105. //search
  106.  
  107.  
  108.     [HttpPost]
  109.     public IActionResult Search(string name)
  110.     {
  111.         var studentAttribute = _context.Students.Where(s => s.Name == name).FirstOrDefault();
  112.         return RedirectToAction(nameof(Details), new { id = studentAttribute.RollNo });
  113.     }
  114.  
  115.  
  116.  
  117.     // GET: Employee
  118.     public async Task<IActionResult> Index(string s)
  119.     {
  120.         if(string.IsNullOrEmpty(s))
  121.         return View(await _context.Employees.ToListAsync());
  122.         else
  123.         return View(await _context.Employees.Where(e=>e.Name == s).ToListAsync());
  124.     }
  125.  
  126. <form asp-action="Search" >
  127.     <input type="text" name="name"/>
  128.     <input type="submit"/>
  129. </form>
  130.  
  131.  
  132. <img src="~/images/1.jpg" height="100px" width="150px" />
  133.  
  134. <td>
  135.     <img height="150" width="150" src="@Url.Content("~/images/"+item.Name+".jpg")" />
  136. </td>
  137.  
  138.  
  139. //model validation
  140.  
  141.  
  142. [EmailAddress(ErrorMessage = "invalid mail")]
  143. [RegularExpression(@"^[a-z0-9]+[@]+[a-z]+[.]+[a-z]{2,}$",ErrorMessage ="Invalid Mail"]
  144. [RegularExpression(@"^[a-zA-Z0-9!@#$%_]{8,}$",ErrorMessage ="Invalid Password")]
  145. public string Password { get; set; }
  146.  
  147. [DataType(DataType.Password)]
  148. [Compare(nameof(Password),ErrorMessage ="must match")]
  149. public string ConfirmPassword { get; set; }
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.         private int total(int m1,int m2,int m3,int m4,int m5)
  159.         {
  160.             return m1+m2+m3+m4+m5;
  161.         }
  162.         private int average(int total)
  163.         {
  164.             return total / 5;
  165.         }
  166.         private char grade(int avg)
  167.         {
  168.             if (avg > 90)
  169.                 return 'A';
  170.             else if (avg >=80 && avg<90)
  171.                 return 'B';
  172.             else if (avg >=70 && avg< 80)
  173.                 return 'C';
  174.             else if (avg >=60 && avg < 70)
  175.                 return 'D';
  176.             else if (avg >=50 && avg < 60)
  177.                 return 'E';
  178.             else
  179.                 return 'F';
  180.         }
  181.  
  182.  
  183.         [HttpPost]
  184.         public async Task<IActionResult> Create(Marksheet marksheet)
  185.         {
  186.             marksheet.Total = total(marksheet.Mark1, marksheet.Mark2, marksheet.Mark3, marksheet.Mark4, marksheet.Mark5);
  187.             marksheet.Average = average(marksheet.Total);
  188.             marksheet.Grade = grade(marksheet.Average);
  189.             if (ModelState.IsValid)
  190.             {
  191.                 _context.Add(marksheet);
  192.                 await _context.SaveChangesAsync();
  193.                 return RedirectToAction(nameof(Index));
  194.             }
  195.             ViewData["RollNo"] = new SelectList(_context.Students, "RollNo", "RollNo", marksheet.RollNo);
  196.             return View(marksheet);
  197.         }
Tags: ASP.NET
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement