Rovdjuret
Rovdjuret

Reputation: 1528

Dropdownlistfor always first value selected while batch editing

I'm trying to batch edit multiple rows where I use a list with status among others to edit.

However, my list won't set the correct value for each row, insted it always chooses the first item in the list. I'm clueless why and asking if anyone knows why and what the solution is.

This happens only while batch editing and not in any other single row editing view. Difference is I'm looping through a list to set and render the values. Just to be extra clear :)

Thanks in advance!

Controller

[HttpGet]
public ActionResult BatchEdit(string search, int organization)
{
    var products = _classService.Get(search, organization);
    var statusList = _productService.GetStatus();

    var batchEditViewModel = new BatchEditViewModel(products, statusList);

    return View("BatchEdit", batchEditViewModel);
}

ViewModel

public BatchEditViewModel(List<Product> products, List<ProductStatus> productStatus)
{
    Products = products;
    ProductStatus = SetStatus(productStatus);
}
public IEnumerable<SelectListItem> ProductStatus { get; set; }

private IEnumerable<SelectListItem> SetStatus(IEnumerable<ProductStatus> productStatus)
{
    return new SelectList(productStatus, "Id", "Title");
}

View

@foreach (var item in Model.Products)
{
   <div class="col-md-6 form-group">
       <label>Status</label>
       @Html.DropDownListFor(m => item.StatusId, Model.ProductStatus, new { @class = "form-control" })
   </div>
}

Output

enter image description here

Upvotes: 0

Views: 1016

Answers (2)

Rovdjuret
Rovdjuret

Reputation: 1528

Solved by doing like this insted:

@Html.DropDownListFor(m => item.StatusId, new SelectList(Model.ProductStatus, "Value", "Text", item.StatusId), new { @class = "form-control status-select" })

Upvotes: 1

Maximilian Ast
Maximilian Ast

Reputation: 3499

in item.Product.StatusId

item is already a Product so try:

item.StatusId

instead of

item.Product.StatusId

Upvotes: 1

Related Questions