I have below code. The radio button list populate without selected value. I expect to see one of the options selected when the list is initially loaded Please see the photo. Anybody can help me out will be great appreciate.

enter image description here

namespace MvcApplication1.Models
{
    public class ProductEditViewModel : Product
    {
        public IEnumerable<SelectListItem> SupplierItems { get; set; }
        public IEnumerable<SelectListItem> CategorieItems { get; set; }
    }
}


[HttpGet]
public ActionResult ProductEdit(Int32 ProductId)
{
    var northwind = new NorthwindEntities();

    var q = from p in northwind.Products
            where p.ProductID == ProductId
            select new ProductEditViewModel
            {
                ProductID = p.ProductID,
                ProductName = p.ProductName,
                UnitPrice = p.UnitPrice,
                SupplierItems = from sup in northwind.Suppliers select new SelectListItem { Text = sup.CompanyName, Value = SqlFunctions.StringConvert((double)sup.SupplierID), Selected = sup.SupplierID == p.SupplierID },
                CategorieItems = from cat in northwind.Categories select new SelectListItem { Text = cat.CategoryName, Value = SqlFunctions.StringConvert((double)cat.CategoryID), Selected = cat.CategoryID == p.CategoryID },
                Discontinued = p.Discontinued
            };

    return View(q.SingleOrDefault());
}


@foreach (var Categorie in Model.CategorieItems)
{
    @Html.RadioButtonFor(model => model.CategoryID, Categorie.Value) @Categorie.Text; @:&nbsp;&nbsp;&nbsp;
}

Related posts

Recent Viewed