0

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. enter image description here

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 ).

enter image description here

The reason why i use ModelState.Clear() is because, Example below: I would like to delete the second Item which is "asd3" enter image description here

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. enter image description here

Is it i make any mistake here ? Because if I remove the ModelState.Clear() code, then it will work fine.

Rehan Shah
  • 1,505
  • 12
  • 28
Ng Zheng
  • 59
  • 1
  • 12
  • 1
    Why do you clear modelstate when are deleting it from thelist? – Just code Jan 03 '19 at 04:21
  • Take a look for `ModelState.Clear()` effect here: https://stackoverflow.com/questions/1775170/asp-net-mvc-modelstate-clear & see [the docs](https://learn.microsoft.com/en-us/dotnet/api/system.web.mvc.modelstatedictionary.clear?view=aspnet-mvc-5.2). The method will remove all items from the model state dictionary. – Tetsuya Yamamoto Jan 03 '19 at 04:26
  • @Justcode i have updated my questions. Please read the last two part. That is why I tried.. I am not 100% sure if my code is correct or not. – Ng Zheng Jan 03 '19 at 06:07
  • @TetsuyaYamamoto thanks.. i will take a look on that.. – Ng Zheng Jan 03 '19 at 06:16

1 Answers1

0

If you are getting your Model from a form and you want to manipulate the data that came from the client form and write it back to a view, you need to call ModelState.Clear() to clean the ModelState values. But i don't think you need to show any new values.So using that means nothing.ACtually it has just rebuilded your model and passed to view and since your dropdown is binded from model so it lost its selected state and got back to default value.

Shubham
  • 443
  • 2
  • 10
  • thanks for your answer.. Yes.. i am getting my model from a form and manipulate the data. Suppose i do not need use ModelState.Clear() method. But if i didnt use it, the above problem occur(i have updated my questions). So i am confuse on that. – Ng Zheng Jan 03 '19 at 06:13