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; }
}
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
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
16 Points
100 Posts
set selected value in select list of cshtml using ViewBag
Apr 02, 2020 05:26 AM|pmdrait|LINK
@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" />
<a asp-action="Index">Back to List</a>
</div>
</div>
</form>
</div>
Contributor
2690 Points
874 Posts
Re: set selected value in select list of cshtml using ViewBag
Apr 02, 2020 06:25 AM|Rena Ni|LINK
Hi pmdrait,
Here is a working demo about how to set select list:
1.Model:
2.View:
3.Controller:
Result:
Best Regards,
Rena
Member
16 Points
100 Posts
Re: set selected value in select list of cshtml using ViewBag
Apr 02, 2020 07:43 AM|pmdrait|LINK
Thanks for the complete solution .
Member
16 Points
100 Posts
Re: set selected value in select list of cshtml using ViewBag
Apr 02, 2020 08:17 AM|pmdrait|LINK
Contributor
2690 Points
874 Posts
Re: set selected value in select list of cshtml using ViewBag
Apr 03, 2020 07:22 AM|Rena Ni|LINK
Hi pmdrait,
Here is the code:
Best Regards,
Rena