J'essaie de remplir la vue d'édition avec des données qui ont des listes déroulantes utilisant ViewModel. Les données sont remplies mais les listes déroulantes ne sont pas sélectionnées selon les données.DropDownList à sélectionner sur Modifier à l'aide de ViewModel
Avoir vérifié des problèmes similaires dans SO comme [SO1] [1], SO2, SO3 mais pas en mesure de résoudre. Sachez que c'est peut-être quelque chose d'idiot qui me manque mais que je ne trouve pas.
code: viewmodel:
public class ProductVM
{
public int ID { get; set; }
[Required]
[DisplayName("Product Name")]
public string ProductName { get; set; }
public int SupplierID { get; set; }
public IEnumerable<SelectListItem> Suppliers { get; set; }
public Supplier Supplier { get; set; }
public int UnitID { get; set; }
public IEnumerable<SelectListItem> Units { get; set; }
public Unit Unit { get; set; }
public int ItemCategoryID { get; set; }
public IEnumerable<SelectListItem> Categories { get; set; }
[DisplayName("Categories")]
public ItemCategory ItemCategory { get; set; }
}
Controller Edit :
public ActionResult Edit(int id)
{
var productVM = GetProductById(id);
return View(productVM);
}
private ProductVM GetProductById(int id)
{
Product product = db.Products.Find(id);
var productVM = new ProductVM();
var suppliers = new SelectList(db.Suppliers, "ID", "SupplierName", product.SupplierID);
productVM.Suppliers = suppliers.ToList();
var categories = new SelectList(db.ItemCategories, "ID", "Name", product.ItemCategoryID);
productVM.Categories = categories.ToList();
var units = new SelectList(db.Units, "ID", "Name", product.UnitID);
productVM.Units = units.ToList();
}
Vue:
<div class="form-group">
@Html.LabelFor(model => model.ProductName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ProductName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ProductName, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.SupplierID, "SupplierID", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(u => u.SupplierID, Model.Suppliers, "--Select--")
@Html.ValidationMessageFor(model => model.SupplierID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UnitID, "UnitID", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(u => u.UnitID, (IEnumerable<SelectListItem>)Model.Units, "--Select--")
@Html.ValidationMessageFor(model => model.UnitID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ItemCategoryID, "ItemCategoryID", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(u => u.ItemCategoryID, (IEnumerable<SelectListItem>)Model.Categories, "--Select--")
@Html.ValidationMessageFor(model => model.ItemCategoryID, "", new { @class = "text-danger" })
</div>
</div>
apprécierais toute aide. Merci d'avance. [1]: Binding Viewmodel Property to dropdown selected item in razor
à menu déroulant sélectionné items en rasoir
Voulez-vous que les listes déroulantes soient chargées avec les valeurs déjà sélectionnées? Comme avec cette question par exemple: https://stackoverflow.com/questions/27901175/how-to-get-dropdownlist-selected-in-controller-in-mvc – MUlferts