I have a dropdownlist as follows
<div class="form-group">
@Html.LabelFor(m => m.Categories, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.DropDownListFor(m => m.Categories, new SelectList(Model.CategoriesDropDownList), "Choose a category", new { @class = "form-control" })
</div>
@Html.ValidationMessageFor(m => m.Categories)
</div>
However, I get an invalid error for validation when I check if the model state is valid or not.
My product form view model is as follows
using LaptopMart.Models;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace LaptopMart.ViewModels
{
public class ProductFormViewModel
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public decimal? Price { get; set; }
[Required]
[DisplayName("Stock")]
public int? StockQuantity { get; set; }
[Required]
public string Description { get; set; }
[Required]
[Display(Name="Category")]
public ICollection<Category> Categories { get; set; }
public string Image { get; set; }
public IEnumerable<Category> CategoriesDropDownList { get; set; }
}
}
I have a many to many relationship between category and product. When I want to fill up the form for product, I wish to choose a category from CategoriesDropDownList into Categories. How do I go about doing that?