Saurabh Suthar

Saurabh Suthar

  • NA
  • 62
  • 2.9k

Remove Selected Item From DropDown

Oct 20 2018 1:04 AM
How to Remove the Selected Item from DropdownList. Once the Item is Selected. Dropdown came from another Model.
 
My Model Looks given below;-
 
public int TaskId { get; set; }
[ForeignKey("TaskId")]
public virtual TaskMst TaskMst { get; set; }
[NotMapped]
public string Task { get; set; }
My Controller Looksgiven below:-
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create(TaskEntry taskEntry)
{
if ((!string.IsNullOrWhiteSpace(taskEntry.strStartDate)) && (Utility.isDate(taskEntry.strStartDate, DateFormats.ddMMyyyy)))
taskEntry.StartDate = Utility.stringToDate(taskEntry.strStartDate, DateFormats.ddMMyyyy, DateFormats.MMddyyyy);
else
ModelState.AddModelError("strStartDate", "Invalid Start Date..");
if (ModelState.IsValid)
{
db.TaskEntries.Add(taskEntry);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
ViewBag.TaskId = new SelectList(db.TaskMsts, "TaskId", "Task");
return View(taskEntry);
}
 
My View Look Given below:-
 
<div class="form-group">
@Html.LabelFor(model => model.Task, "Task", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.DropDownList("TaskId", null, "--Select--", htmlAttributes: new { @id = "TaskId", @class = "form-control select2", @onchange = "getpriority();" })
@Html.ValidationMessageFor(model => model.Task, "", new { @class = "text-danger" })
</div>
</div>
 
Please Suggest how to Remove once they item selected from the Dropdown.

Answers (1)