Technical Developer
Technical Developer
CC#
Created by Technical Developer on 1/29/2025 in #help
CRUD webapp Form is accepting empty responses after applying validations
now what to do
32 replies
CC#
Created by Technical Developer on 1/29/2025 in #help
CRUD webapp Form is accepting empty responses after applying validations
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
32 replies
CC#
Created by Technical Developer on 1/29/2025 in #help
CRUD webapp Form is accepting empty responses after applying validations
now i have added my viewModel
32 replies
CC#
Created by Technical Developer on 1/29/2025 in #help
CRUD webapp Form is accepting empty responses after applying validations
@Angius
32 replies
CC#
Created by Technical Developer on 1/29/2025 in #help
CRUD webapp Form is accepting empty responses after applying validations
ok
32 replies
CC#
Created by Technical Developer on 1/29/2025 in #help
CRUD webapp Form is accepting empty responses after applying validations
can u share a tutorial for that please
32 replies
CC#
Created by Technical Developer on 1/29/2025 in #help
CRUD webapp Form is accepting empty responses after applying validations
and what to write in it
32 replies
CC#
Created by Technical Developer on 1/29/2025 in #help
CRUD webapp Form is accepting empty responses after applying validations
ok i am getting confused can u describe a little bit where to create this class for all my records
32 replies
CC#
Created by Technical Developer on 1/29/2025 in #help
CRUD webapp Form is accepting empty responses after applying validations
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; }
}
}
32 replies
CC#
Created by Technical Developer on 1/29/2025 in #help
CRUD webapp Form is accepting empty responses after applying validations
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; }
}
}
32 replies
CC#
Created by Technical Developer on 1/29/2025 in #help
CRUD webapp Form is accepting empty responses after applying validations
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
32 replies
CC#
Created by Technical Developer on 1/29/2025 in #help
CRUD webapp Form is accepting empty responses after applying validations
wait
32 replies
CC#
Created by Technical Developer on 1/29/2025 in #help
CRUD webapp Form is accepting empty responses after applying validations
ok and what is that
32 replies
CC#
Created by Technical Developer on 1/29/2025 in #help
CRUD webapp Form is accepting empty responses after applying validations
@Angius which part i first show here is the controller code now sharing view code
32 replies
CC#
Created by Technical Developer on 1/29/2025 in #help
CRUD webapp Form is accepting empty responses after applying validations
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 });
}
}
}
32 replies
CC#
Created by Technical Developer on 1/29/2025 in #help
CRUD webapp Form is accepting empty responses after applying validations
visual studio is crashed
32 replies
CC#
Created by Technical Developer on 1/29/2025 in #help
CRUD webapp Form is accepting empty responses after applying validations
posting
32 replies
CC#
Created by Technical Developer on 1/29/2025 in #help
CRUD webapp Form is accepting empty responses after applying validations
yeh just 1 min
32 replies