umair mohsin

umair mohsin

  • 1.6k
  • 190
  • 26.3k

ViewModel is not persisting database value

Dec 17 2018 10:26 AM
i have populated one of my view with this viewmodel
 
public ActionResult AddStudent(int id)
{
var record = (from x in db.Registers
where (x.RegisterId == id)
select new StudentModel()
{
Batch = x.Batch,
ClassRoll = x.RoleNo,
Department = x.Department,
Name = x.StudentFullName
}).ToList();
return View(record);
}
these values are coming from database and persist on viewmodel to display but the problem is
 
 
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddStudent(StudentModel s)
{
Student stu = new Student();
s.LibraryCardNo = stu.LibraryCardNo;
s.Name = stu.Name;
s.Department = stu.Department;
s.Batch = stu.Batch;
s.ClassRoll = stu.RollNo;
db.Students.Add(stu);
ViewBag.Msg = "Record Inserted";
return View();
}
 
when this viewmodel is sent back to processing t 
 hese values are empty i don't know how to fix this thing.please help me
 
 

Answers (2)