Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- microsoft.visualstudio.web.codegeneration.design
- microsoft.aspnetcore.component.quickgrid.entityframeworkadapter
- //program.cs
- builder.Services.AddDbContext<DataContext>(opt => opt.UseSqlServer(builder.Configuration.GetConnectionString("ConnectionString")));
- //optional session hoy to j
- builder.Services.AddSession();
- OR
- builder.Services.AddSession(options => { options.IdleTimeout = TimeSpan.FromMinutes(1); });
- builder.Services.AddDistributedMemoryCache();
- app.UseSession();
- [HttpPost]
- public async Task<IActionResult> Edit(Student student)
- {
- Student s = await _context.Students.FindAsync(student.RollNo);
- if (s != null)
- {
- s.Name = student.Name;
- s.BirthDate = student.BirthDate;
- s.Password = student.Password;
- s.ConfirmPassword = student.ConfirmPassword;
- s.Username = student.Username;
- await _context.SaveChangesAsync();
- return RedirectToAction(nameof(Index));
- }
- return View(s);
- }
- //login
- //with session
- [HttpPost]
- public IActionResult Login(Student student)
- {
- Student cust = _context.Students.Where(c => c.Username == student.Username && c.Password == student.Password).FirstOrDefault();
- Marksheet mark = _context.Marksheets.Find(cust.RollNo);
- if (cust != null)
- {
- HttpContext.Session.SetString("customer", cust.Username);
- ViewBag.Message = "success";
- if (mark != null)
- return RedirectToAction(nameof(Details), nameof(Marksheet), new { id = cust.RollNo });
- else
- return RedirectToAction(nameof(Create), nameof(Marksheet));
- }
- else
- ViewBag.Message = "Login Failed...";
- return View();
- }
- public IActionResult LogOut()
- {
- if (HttpContext.Session.GetString("customer") != null)
- {
- HttpContext.Session.Remove("customer");
- return RedirectToAction("Login");
- }
- return View();
- }
- //without session
- [HttpPost]
- public IActionResult Login(Student student)
- {
- Student cust = _context.Students.Where(c => c.Username == student.Username && c.Password == student.Password).FirstOrDefault();
- if(s != null)
- return RedirectToAction("Index");
- else
- Viewbag.Message="failed...";
- }
- //cshtml
- <a asp-action="LogOut"><input type="button" value="LogOut"/></a> //with session
- <a href="Index"><input type="button" value="LogOut"/></a> //without session
- //search
- [HttpPost]
- public IActionResult Search(string name)
- {
- var studentAttribute = _context.Students.Where(s => s.Name == name).FirstOrDefault();
- return RedirectToAction(nameof(Details), new { id = studentAttribute.RollNo });
- }
- // GET: Employee
- public async Task<IActionResult> Index(string s)
- {
- if(string.IsNullOrEmpty(s))
- return View(await _context.Employees.ToListAsync());
- else
- return View(await _context.Employees.Where(e=>e.Name == s).ToListAsync());
- }
- <form asp-action="Search" >
- <input type="text" name="name"/>
- <input type="submit"/>
- </form>
- <img src="~/images/1.jpg" height="100px" width="150px" />
- <td>
- <img height="150" width="150" src="@Url.Content("~/images/"+item.Name+".jpg")" />
- </td>
- //model validation
- [EmailAddress(ErrorMessage = "invalid mail")]
- [RegularExpression(@"^[a-z0-9]+[@]+[a-z]+[.]+[a-z]{2,}$",ErrorMessage ="Invalid Mail"]
- [RegularExpression(@"^[a-zA-Z0-9!@#$%_]{8,}$",ErrorMessage ="Invalid Password")]
- public string Password { get; set; }
- [DataType(DataType.Password)]
- [Compare(nameof(Password),ErrorMessage ="must match")]
- public string ConfirmPassword { get; set; }
- private int total(int m1,int m2,int m3,int m4,int m5)
- {
- return m1+m2+m3+m4+m5;
- }
- private int average(int total)
- {
- return total / 5;
- }
- private char grade(int avg)
- {
- if (avg > 90)
- return 'A';
- else if (avg >=80 && avg<90)
- return 'B';
- else if (avg >=70 && avg< 80)
- return 'C';
- else if (avg >=60 && avg < 70)
- return 'D';
- else if (avg >=50 && avg < 60)
- return 'E';
- else
- return 'F';
- }
- [HttpPost]
- public async Task<IActionResult> Create(Marksheet marksheet)
- {
- marksheet.Total = total(marksheet.Mark1, marksheet.Mark2, marksheet.Mark3, marksheet.Mark4, marksheet.Mark5);
- marksheet.Average = average(marksheet.Total);
- marksheet.Grade = grade(marksheet.Average);
- if (ModelState.IsValid)
- {
- _context.Add(marksheet);
- await _context.SaveChangesAsync();
- return RedirectToAction(nameof(Index));
- }
- ViewData["RollNo"] = new SelectList(_context.Students, "RollNo", "RollNo", marksheet.RollNo);
- return View(marksheet);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement