Mark Tabor

Mark Tabor

  • 585
  • 2k
  • 460.7k

how to get disabled dropdownListFor Value in controller

Nov 29 2019 10:51 AM
I have a dropdownListFor controls which are cascade in nature on edit i do need to disabled the below cascaded dropdown lists until the values get changed from the first one , so below is my code of dropdown list
```
<div class="form-group">
@Html.LabelFor(model => model.Program_Id, "Program_Id", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Program_Id", null, htmlAttributes: new { @class = "form-control", @id = "ddlPrograms" })
@Html.ValidationMessageFor(model => model.Program_Id, "", new { @class = "text-danger" })
</div>
</div>
```
I am making them disable using javascript
```
document.getElementById("ddlYear").disabled = true;
document.getElementById("ddlBlocks").disabled = true;
document.getElementById("ddlModules").disabled = true;
```
But on Edit when I submit the forms instead of selected value they send 0 to the controller . how to fix that issue if i am using hidden field then how i would be able to access this hidden field value as i am using entity framework and below is my controller edit post method
```
public async Task<ActionResult> Edit([Bind(Include = "Course_Id,Course_Name,Course_Code,Course_Visibility,Course_Start_Date,Course_End_Date,Block_Id,Module_Id,Program_Id,Year,Course_Description,Course_Image,Course_Category_Id,Course_Content_File_Path,Course_announcement,Course_Tags,Status,Course_Short_Name,Date_Created,Date_Modified")] Courses courses)
{
if (ModelState.IsValid)
{
db.Entry(courses).State = EntityState.Modified;
if (db.SaveChanges() > 0)
{
TempData["UpdatedMessage"] = "Updated Successfully";
}
// await db.SaveChangesAsync();
return RedirectToAction("Index");
}
ViewBag.Block_Id = new SelectList(db.Blocks, "Id", "Name", courses.Block_Id);
ViewBag.Course_Category_Id = new SelectList(db.Courses_Category, "Id", "Category_Name", courses.Course_Category_Id);
ViewBag.Module_Id = new SelectList(db.Moduels, "Id", "Name", courses.Module_Id);
ViewBag.Program_Id = new SelectList(db.Programs, "Id", "Program_Title", courses.Program_Id);
ViewBag.Year = new SelectList(db.Years, "Id", "Name", courses.Year);
return View(courses);
}
```
Thanks in advance

Answers (1)