Html helper dropdownlistfor doesn't work as expected after using ModelState.Clear();. If i didnt include ModelState.Clear() in my controller, then it work fine
This is my HTML code
@Html.DropDownListFor(model => model.Vendors, new
SelectList(Model.vendors_list, "Entry_Value", "Entry_Value",
Model.Vendors), "--Please select Vendor--", new { @class = "form-control",
required = "required" })
Delete code as below
<input type="submit" value="Delete" formaction=@Url.Action("DeleteOthersPartList/" + i) formmethod="post" class="btn btn-danger" onclick="return confirm('Confirm Delete ?')" />
And this is my Controller code when button delete clicked
public ActionResult DeleteOthersPartList(RegisterAL model, int id)
{
try
{
PartList part = new PartList();
part = model.part_information_List[id];
model.part_information_List.Remove(part);
ModelState.Clear();
return View("New_RegistrationForm", model);
}
}
Before i click on my delete button, i select the value as below image show.
After i click on my delete button, the dropdownlist jump back to default value, and then the value actually is not blank (Highlighted is the value for model.Vendors ).
The reason why i use ModelState.Clear() is because,
Example below:
I would like to delete the second Item which is "asd3"
If I didnt add in ModelState.Clear() code, then below result will show. It will always deleted the LAST item.But the one I delete is "asd3", but it deleted "asd4".
I know it may be is because of controller code problem, but I have tried to debug, in controller side, the "asd3" record deleted, but when return to View, it appear again. Then I add in ModelState.Clear() code, I go back to my view, it able to show "asd3" is deleted but it create another problem about dropdownlist i mention above.
Is it i make any mistake here ? Because if I remove the ModelState.Clear() code, then it will work fine.