简体   繁体   中英

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

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 class="form-group">
                <label   class="control-label">Department Name</label>
                <select asp-items="@ViewBag.Departments" class="form-control">

                </select>
            </div>
<div class="btn-group pt-4">
    @*also have other fields*@
<a asp-action="Index" class="btn btn-primary">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();
}

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

In your create view, try below:

 <form asp-action="Create">

            
            <div class="form-group">
                <label name="DeptId" class="control-label">Department Name</label>
                <select name="DeptId" class ="form-control" asp-items="@ViewBag.Departments" ></select>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </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"];

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" class="form-control">

Also made DeptName nullable in StudentViewModel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM