Home > Mobile >  How to pass a specific ViewBag SelectList data from view to controller and then in Repository
How to pass a specific ViewBag SelectList data from view to controller and then in Repository

Time:09-26

I need to pass DeptId in department table to student table when doing create and upadate operations. How can i achieve this.

My AddStudent method in repository

 public void AddStudents(Student student)
    {
        context.Students.Add(student);
        context.SaveChanges();
    }

Get and Post method in studentcontroller

public IActionResult Create()
    {
        var departments=departmentRepository.GetAllDepartmentList();
        ViewBag.Departments = new SelectList(departments, "DeptId", "DeptName");
        return View();
    }
    [HttpPost]
    public IActionResult Create(Student student)
    {

        if (ModelState.IsValid)
        {
            repository.AddStudents(student);

            return RedirectToAction("Index");
        }
        return View();
    }

Create.cshtml

 <form asp-action="Create">
 <div >
                <label   >Department Name</label>
                <select asp-items="@ViewBag.Departments" >

                </select>
            </div>
<div >
    @*also have other fields*@
<a asp-action="Index" >Back to List</a>            </div>
</form>

StudentViewModel.cs is used for viewing and fetching data from both tables

How to do pass the same the DeptId for Update operation also. Here is the code to fetch the record of a particular student and it works fine but dont know how to do this for update

public StudentViewModel GetStudentById(int id)
{
        var students = context.Students.Join(context.Departments, stud => stud.DeptId, dept => dept.DeptId, (stud, dept) => new StudentViewModel
        {
            StudId = stud.StudId,
            StudentName = stud.StudentName,
            Age = stud.Age,
            Gender = stud.Gender,
            DOB = stud.DOB,
            Email = stud.Email,
            Address = stud.Address,
            DeptId = stud.DeptId,
            DeptName = dept.DeptName


        }).Where(stud => stud.StudId.Equals(id)).FirstOrDefault();

        return students; 

    }

Update method not working:

public void UpdateStudents(Student student)
{
    context.Students.Update(student);
    context.SaveChanges();
}

CodePudding user response:

How to do pass the same the DeptId for Update operation also

In your create view , try below:

 <form asp-action="Create">

            
            <div >
                <label name="DeptId" >Department Name</label>
                <select name="DeptId" class ="form-control" asp-items="@ViewBag.Departments" ></select>
            </div>
            <div >
                <input type="submit" value="Create"  />
            </div>
        </form>

In your creat action add parameter DeptId

    [HttpPost]
    public IActionResult Create(Student student, string DeptId)
    {...
       TempData["AA"] =  DeptId;
      }

And add below in your Update operation to get DeptId from Create post action

int DEPID = (int)TempData["AA"];

CodePudding user response:

Create is now working this way :

edited my Create post method as below:

 public IActionResult Create(StudentViewModel model)
    {

        if (ModelState.IsValid)
        {
            Student student = new Student
            {
                StudentName = model.StudentName,
                Gender = model.Gender,
                DOB = model.DOB,
                Age = model.Age,
                Email = model.Email,
                Address = model.Address,
                DeptId = model.DeptId
            };
            repository.AddStudents(student);

            return RedirectToAction("Index");
        }
        return View();
    }

Then in my Create.cshtml added asp-for:

@model StudentViewModel
@*..Other fields...*@
<select asp-for="DeptId" asp-items="@ViewBag.Departments" >

Also made DeptName nullable in StudentViewModel

  • Related