I have a multiple select type control named fruit. When I want to update the fruitcollection form, the value that I entered earlier cannot appear on the fruitcollection multiple select form. What's the solution? Thank you
Update.cshtml (View)
...
<select class="form-control select2" multiple="multiple" name="FruitsCollection" value="@Model.Fruit">
<option value="Apple">Apple</option>
<option value="Banana">Banana</option>
<option value="Watermelon">Watermelon</option>
</select>
...
StoreController.cs (Controller)
MyDbContext db = new MyDbContext();
...
public ActionResult Update(int id)
{
MasterStore store = new MasterStore();
if (id!=0)
{
store = db.MasterStore.Where(s => s.Id == id).FirstOrDefault();
promo.FruitsCollection = promo.Fruit.Split(',').ToArray();
}
return View(store);
}
[HttpPost]
public ActionResult UpdateStore(MasterStore store)
{
MasterStore p = db.MasterStore.Where(s => s.Id == store.Id).First();
p.Id = store.Id;
p.Fruit = store.Fruit;
}
...
MasterStore.cs (Model)
...
public partial class MasterStore
{
public int Id { get; set; }
public string Fruit { get; set; }
[NotMapped]
public string[] FruitsCollection { get; set; }
}
...
<select>elements don't have avalueattribute. developer.mozilla.org/en-US/docs/Web/HTML/Element/select