Z
Z
z_a_p_a_r_a2018-11-14 16:13:01
ASP.NET
z_a_p_a_r_a, 2018-11-14 16:13:01

How to set not the ID of the connected entity but the Name field in the Create/Update view?

Hello connoisseurs.
The bottom line is that there are models:

public class Department
    {
        public int DepartmentID { get; set; }
        public string Name { get; set; }

        public List<Person> Persons { get; set; }
    }
 public class Person
    {
        public int PersonID { get; set; }
        public string Name { get; set; }

        public int DepartmentID { get; set; }
        public Department Department { get; set; }
    }

Well, the context is:
public class DepartmentContext : DbContext
    {
        public DepartmentContext (DbContextOptions<DepartmentContext> options)
            : base(options)
        {
        }

        public DbSet<Department> Department { get; set; }

        public DbSet<Person> Person { get; set; }
    }

VS generated the controller, everything works... When displaying Person, it showed the name and ID of the Department, well, here I corrected everything and shows the name of the Department. But here is the problem with editing and creating. Initially generated controller code:
public IActionResult Create()
        {
            ViewData["DepartmentID"] = new SelectList(_context.Department, "DepartmentID", "DepartmentID");
            return View();
        }
[HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("PersonID,Name,DepartmentID")] Person person)
        {
            if (ModelState.IsValid)
            {
                _context.Add(person);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            ViewData["DepartmentID"] = new SelectList(_context.Department, "Name", "Name", person.DepartmentID);
            return View(person);
        }

Well, View:
<div class="form-group">
                <label asp-for="Name" class="control-label"></label>
                <input asp-for="Name" class="form-control" />
                <span asp-validation-for="Name" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="DepartmentID" class="control-label"></label>
                <select asp-for="DepartmentID" class ="form-control" asp-items="ViewBag.DepartmentID"></select>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>

Accordingly, when creating a new person, there will be a page with the name field and DepartmentID drop-down list. I tried and made the dropdown list be named Department, BUT how to save now? gives an error message....

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Fat Lorrie, 2018-11-14
@z_a_p_a_r_a

The SelectList constructor has the signature :

public SelectList (System.Collections.IEnumerable items, 
                   string dataValueField, 
                   string dataTextField,
                   object selectedValue);

Therefore, the second parameter must be the name of the property for the values ​​of the option elements:
new SelectList(_context.Department, 
               nameof(Department.DepartmentID), 
               nameof(Department.Name), 
               person.DepartmentID);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question