locked
set selected value in select list of cshtml using ViewBag RRS feed

  • Question

  • User-1202579601 posted

    @model StudentAttendance.Models.Semester
    <h4>Edit Semester Details </h4>
    <div class="row">
    <form asp-action="Edit" class="form-horizontal">
    <div class="col-md-6">
    <div class="form-group">
    <label asp-for="SemId" class="control-label col-md-2">Semester Id</label>
    <div class="col-md-10">
    <input asp-for="SemId" class="form-control" />
    <span asp-validation-for="SemId" class="text-danger"></span>
    </div>
    </div>
    <div class="form-group">
    <label asp-for="Dos" class="control-label col-md-2">Start Date</label>
    <div class="col-md-10">
    <input asp-for="Dos" class="form-control" />
    <span asp-validation-for="Dos" class="text-danger"></span>
    </div>
    </div>
    <div class="form-group">
    <label asp-for="Doe" class="control-label col-md-2">End Date</label>
    <div class="col-md-10">
    <input asp-for="Doe" class="form-control" />
    <span asp-validation-for="Doe" class="text-danger"></span>
    </div>
    </div>
    <div class="form-group">
    <label asp-for="DepartmentId" class="control-label">Department</label>
    <div class="col-md-6">
    <select asp-for="DepartmentId" class="form-control" asp-items="@ViewBag.Dept" > //set option value selected to that of DepartmentId
    <option value="">-- Select Department --</option>
    </select>
    <span asp-validation-for="DepartmentId" class="text-danger" />
    </div>
    </div>

    <div class="form-group">
    <input type="submit" value="Save" class="btn btn-default" />
    &nbsp;&nbsp;&nbsp;&nbsp; <a asp-action="Index">Back to List</a>
    </div>
    </div>
    </form>
    </div>

    Thursday, April 2, 2020 5:26 AM

Answers

  • User711641945 posted

    Hi pmdrait,

    Here is a working demo about how to set select list:

    1.Model:

    public class Semester
    {
        public int SemId { get; set; }
        public string Dos { get; set; }
        public string Doe { get; set; }
        public int DepartmentId { get; set; }
        public Department Department { get; set; }
    }
    public class Department
    {
        public int DepartmentId { get; set; }
        public string DepartmentName { get; set; }
    }

    2.View:

    @model Semester
    <h4>Edit Semester Details </h4>
    <div class="row">
        <div class="col-md-6">
            <form asp-action="Edit" class="form-horizontal">
                <div class="form-group">
                    <label asp-for="SemId" class="control-label col-md-2">Semester Id</label>
                    <div class="col-md-10">
                        <input asp-for="SemId" class="form-control" />
                        <span asp-validation-for="SemId" class="text-danger"></span>
                    </div>
                </div>
                <div class="form-group">
                    <label asp-for="Dos" class="control-label col-md-2">Start Date</label>
                    <div class="col-md-10">
                        <input asp-for="Dos" class="form-control" />
                        <span asp-validation-for="Dos" class="text-danger"></span>
                    </div>
                </div>
                <div class="form-group">
                    <label asp-for="Doe" class="control-label col-md-2">End Date</label>
                    <div class="col-md-10">
                        <input asp-for="Doe" class="form-control" />
                        <span asp-validation-for="Doe" class="text-danger"></span>
                    </div>
                </div>
                <div class="form-group">
                    <label asp-for="DepartmentId" class="control-label">Department</label>
                    <div class="col-md-6">
                        <select asp-for="DepartmentId" class="form-control" asp-items="@ViewBag.Dept">
                            <option value="">-- Select Department --</option>
                        </select>
                        <span asp-validation-for="DepartmentId" class="text-danger" />
                    </div>
                </div>
    
                <div class="form-group">
                    <input type="submit" value="Save" class="btn btn-default" />
                    &nbsp;&nbsp;&nbsp;&nbsp; <a asp-action="Index">Back to List</a>
                </div>
            </form>
    </div>  
    </div>

    3.Controller:

    public class SemestersController : Controller
    {
        private readonly YourDbContext _context;
    
        public SemestersController(YourDbContext context)
        {
            _context = context;
        }
    
        // GET: Semesters
        public async Task<IActionResult> Index()
        {
            var model = _context.Semester.Include(s => s.Department);
            return View(await model.ToListAsync());
        }
        // GET: Semesters/Edit/5
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }
    
            var semester = await _context.Semester.FindAsync(id);
            if (semester == null)
            {
                return NotFound();
            }
            ViewBag.Dept = new SelectList(_context.Department, "DepartmentId", "DepartmentName", semester.DepartmentId);
            return View(semester);
        }
    
        // POST: Semesters/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, [Bind("SemId,Dos,Doe,DepartmentId")] Semester semester)
        {
            if (id != semester.SemId)
            {
                return NotFound();
            }
    
            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(semester);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!SemesterExists(semester.SemId))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            ViewBag.Dept = new SelectList(_context.Department, "DepartmentId", "DepartmentName", semester.DepartmentId);
            return View(semester);
        }
    }

    Result:

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 2, 2020 6:25 AM

All replies

  • User711641945 posted

    Hi pmdrait,

    Here is a working demo about how to set select list:

    1.Model:

    public class Semester
    {
        public int SemId { get; set; }
        public string Dos { get; set; }
        public string Doe { get; set; }
        public int DepartmentId { get; set; }
        public Department Department { get; set; }
    }
    public class Department
    {
        public int DepartmentId { get; set; }
        public string DepartmentName { get; set; }
    }

    2.View:

    @model Semester
    <h4>Edit Semester Details </h4>
    <div class="row">
        <div class="col-md-6">
            <form asp-action="Edit" class="form-horizontal">
                <div class="form-group">
                    <label asp-for="SemId" class="control-label col-md-2">Semester Id</label>
                    <div class="col-md-10">
                        <input asp-for="SemId" class="form-control" />
                        <span asp-validation-for="SemId" class="text-danger"></span>
                    </div>
                </div>
                <div class="form-group">
                    <label asp-for="Dos" class="control-label col-md-2">Start Date</label>
                    <div class="col-md-10">
                        <input asp-for="Dos" class="form-control" />
                        <span asp-validation-for="Dos" class="text-danger"></span>
                    </div>
                </div>
                <div class="form-group">
                    <label asp-for="Doe" class="control-label col-md-2">End Date</label>
                    <div class="col-md-10">
                        <input asp-for="Doe" class="form-control" />
                        <span asp-validation-for="Doe" class="text-danger"></span>
                    </div>
                </div>
                <div class="form-group">
                    <label asp-for="DepartmentId" class="control-label">Department</label>
                    <div class="col-md-6">
                        <select asp-for="DepartmentId" class="form-control" asp-items="@ViewBag.Dept">
                            <option value="">-- Select Department --</option>
                        </select>
                        <span asp-validation-for="DepartmentId" class="text-danger" />
                    </div>
                </div>
    
                <div class="form-group">
                    <input type="submit" value="Save" class="btn btn-default" />
                    &nbsp;&nbsp;&nbsp;&nbsp; <a asp-action="Index">Back to List</a>
                </div>
            </form>
    </div>  
    </div>

    3.Controller:

    public class SemestersController : Controller
    {
        private readonly YourDbContext _context;
    
        public SemestersController(YourDbContext context)
        {
            _context = context;
        }
    
        // GET: Semesters
        public async Task<IActionResult> Index()
        {
            var model = _context.Semester.Include(s => s.Department);
            return View(await model.ToListAsync());
        }
        // GET: Semesters/Edit/5
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }
    
            var semester = await _context.Semester.FindAsync(id);
            if (semester == null)
            {
                return NotFound();
            }
            ViewBag.Dept = new SelectList(_context.Department, "DepartmentId", "DepartmentName", semester.DepartmentId);
            return View(semester);
        }
    
        // POST: Semesters/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, [Bind("SemId,Dos,Doe,DepartmentId")] Semester semester)
        {
            if (id != semester.SemId)
            {
                return NotFound();
            }
    
            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(semester);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!SemesterExists(semester.SemId))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            ViewBag.Dept = new SelectList(_context.Department, "DepartmentId", "DepartmentName", semester.DepartmentId);
            return View(semester);
        }
    }

    Result:

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 2, 2020 6:25 AM
  • User-1202579601 posted

    Thanks for the complete solution .

    Thursday, April 2, 2020 7:43 AM
  • User-1202579601 posted

    if (!SemesterExists(semester.SemId))
    The underlined part showing error. It refers to entity or table
    Thursday, April 2, 2020 8:17 AM
  • User711641945 posted

    Hi pmdrait,

    Here is the code:

    private bool SemesterExists(int id)
    {
        return _context.Semester.Any(e => e.SemId == id);
    }

    Best Regards,

    Rena

    Friday, April 3, 2020 7:22 AM