CRUD webapp Form is accepting empty responses after applying validations

I am stuck in making CRUD app first i Downloaded packages of EF then i create model class and db context and create migrations then i start creating my first view for creating/inserting data but the problem is when i run the program and try to insert nothing happens it only redirects me to index and when I open database to check wheather it is enter or not nothing was shown and also validations are not working #web
16 Replies
Angius
Angius2d ago
Gonna need to see some code
Technical Developer
yeh just 1 min posting visual studio is crashed HomeController.cs

using System.Diagnostics;
using crudapp.Migrations;
using crudapp.Models;
using Microsoft.AspNetCore.Mvc;
using Students = crudapp.Models.Students;

namespace crudapp.Controllers
{
public class HomeController : Controller
{
private readonly StudentDbContext studentDb;
public HomeController(StudentDbContext studentDbContext)
{
this.studentDb = studentDbContext;
}


public IActionResult Index()
{
return View();
}
[Route("/AddStudent")]
[HttpGet]

public IActionResult AddStudent()
{
return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> AddStudent([Bind("Id,Name,Email,Phone,Age")] Students Student)
{
if (ModelState.IsValid)
{
studentDb.Students.Add(Student);
studentDb.SaveChanges();
return RedirectToAction("Read");
}
return View(Student);
}

public IActionResult Privacy()
{
return View();
}

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}

using System.Diagnostics;
using crudapp.Migrations;
using crudapp.Models;
using Microsoft.AspNetCore.Mvc;
using Students = crudapp.Models.Students;

namespace crudapp.Controllers
{
public class HomeController : Controller
{
private readonly StudentDbContext studentDb;
public HomeController(StudentDbContext studentDbContext)
{
this.studentDb = studentDbContext;
}


public IActionResult Index()
{
return View();
}
[Route("/AddStudent")]
[HttpGet]

public IActionResult AddStudent()
{
return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> AddStudent([Bind("Id,Name,Email,Phone,Age")] Students Student)
{
if (ModelState.IsValid)
{
studentDb.Students.Add(Student);
studentDb.SaveChanges();
return RedirectToAction("Read");
}
return View(Student);
}

public IActionResult Privacy()
{
return View();
}

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}
@Angius which part i first show here is the controller code now sharing view code
Angius
Angius2d ago
I think I can already see the potential error
Technical Developer
ok and what is that
Angius
Angius2d ago
Presumably, your form doesn't fill all the properties of Student, since it's a database entity and the form wouldn't have fields for things like ID or foreign keys You should be using a viewmodel here Make a class or a record that contains only what the form sends Then you can even skip the [Bind]
Technical Developer
wait AddStudent.cshtml
@{
ViewData["Title"] = "AddStudent";
@model crudapp.Models.Students;
}

<div class="container m-5" >
<form method="post" asp-asp-controller="Home" asp-action="Index">
<div class="mb-3">
<label>Name: </label>
<input type="text" asp-for="Name" />
<span asp-validation-for="Name" class="Text-danger"></span>
</div>

<div class="mb-3">
<label>Email: </label>
<input type="Email" asp-for="Email" />
<span asp-validation-for="Email" class="Text-danger"></span>
</div>

<div class="mb-3">
<label>Phone: </label>
<input type="text" asp-for="Phone" />
<span asp-validation-for="Phone" class="Text-danger"></span>
</div>

<div class="mb-3">
<label>Age: </label>
<input type="number" asp-for="Age" />
<span asp-validation-for="Age" class="Text-danger"></span>
</div>
<input type="submit" value="Submit" />
</form>
</div>
@{
ViewData["Title"] = "AddStudent";
@model crudapp.Models.Students;
}

<div class="container m-5" >
<form method="post" asp-asp-controller="Home" asp-action="Index">
<div class="mb-3">
<label>Name: </label>
<input type="text" asp-for="Name" />
<span asp-validation-for="Name" class="Text-danger"></span>
</div>

<div class="mb-3">
<label>Email: </label>
<input type="Email" asp-for="Email" />
<span asp-validation-for="Email" class="Text-danger"></span>
</div>

<div class="mb-3">
<label>Phone: </label>
<input type="text" asp-for="Phone" />
<span asp-validation-for="Phone" class="Text-danger"></span>
</div>

<div class="mb-3">
<label>Age: </label>
<input type="number" asp-for="Age" />
<span asp-validation-for="Age" class="Text-danger"></span>
</div>
<input type="submit" value="Submit" />
</form>
</div>
see here is my view
Angius
Angius2d ago
Yeah, and Student model?
Technical Developer
using System.ComponentModel.DataAnnotations;

namespace crudapp.Models
{
public class Students
{
[Key]
public int Id { get; set; }
[Required]
[StringLength(50)]
public string Name { get; set; }
[Required]
[StringLength(100)]
[EmailAddress]
public string Email { get; set; }
[Required]
public string Phone { get; set; }
[Required]
public int Age { get; set; }
}
}
using System.ComponentModel.DataAnnotations;

namespace crudapp.Models
{
public class Students
{
[Key]
public int Id { get; set; }
[Required]
[StringLength(50)]
public string Name { get; set; }
[Required]
[StringLength(100)]
[EmailAddress]
public string Email { get; set; }
[Required]
public string Phone { get; set; }
[Required]
public int Age { get; set; }
}
}
and here is the dbContext
using Microsoft.EntityFrameworkCore;

namespace crudapp.Models
{
public class StudentDbContext : DbContext
{
public StudentDbContext(DbContextOptions<StudentDbContext> options) :base (options)
{

}
public DbSet<Students> Students { get; set; }
}
}
using Microsoft.EntityFrameworkCore;

namespace crudapp.Models
{
public class StudentDbContext : DbContext
{
public StudentDbContext(DbContextOptions<StudentDbContext> options) :base (options)
{

}
public DbSet<Students> Students { get; set; }
}
}
Angius
Angius2d ago
Yeah, the form does not fill the Id Hence the validation error
Angius
Angius2d ago
No description
Technical Developer
ok i am getting confused can u describe a little bit where to create this class for all my records
Angius
Angius2d ago
Wherever
Technical Developer
and what to write in it can u share a tutorial for that please
Angius
Angius2d ago
Just the properties that the form sets
Technical Developer
ok @Angius now i have added my viewModel but i am thinking what to do with this becuase my Sir didn't tell me this thing i saw this thing for the first time so i check online tutorials now what to do
Angius
Angius2d ago
You use that to construct your database model

Did you find this page helpful?