Binding Dropdownlist with Model Values

Posted by Self-Innovator under ASP.NET MVC on 5/5/2016 | Points: 10 | Views : 401 | Status : [Member] | Replies : 3
Hi All,

I'm trying to bind my dropdownlist with the model class where it takes the values from my EF. I have the below code

public sealed class StoreRegistrationViewModel
{
public StoreRegistrationInfoModel StoreRegistrationAssociation { get; set; }
}



public class StoreRegistration
{
public IList StoreIdentity { get; set; }
public int StoreNumber { get; set; }
public string StoreName { get; set; }
public string City { get; set; }
public Address StoreAddress { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string StorePhone { get; set; }
public string EmailAddress { get; set; }
public string VerifyEmailAAddress { get; set; }
public string Comments { get; set; }
}


[ActionName("Index")]
public ActionResult StoreRegistration()
{
StoreRegistration s = new StoreRegistration();
s.StoreIdentity = retailIdentityRepository.GetActiveRetailIdentities();
//ViewBag.RetailIdentity = retailIdentityRepository.GetActiveRetailIdentities();
return View("Index");
}



@using Newtonsoft.Json;
@model Mol2.Website.Models.StoreRegistration.StoreRegistrationViewModel
@{
ViewBag.Title = "Index";
}

@Html.DropDownList("retailIdentityDDL",x => x.StoreRegistrationAssociation.StoreIdentity, "Select")
@Html.ValidationMessageFor(x => x.StoreRegistrationAssociation.StoreIdentity)


I'm getting an exception unable to cast IList to IEnumerable i'm new to MVC


Any Suggesstions or help would be appreciated

Join Hands Change lives
Thanks & Regards
Straight Edge Society



Responses

Posted by: Bhuvanesh6 on: 7/26/2016 [Member] Starter | Points: 25

Up
0
Down
You can't able to directly bind an List collection to the dropdown list, you have to assign the values using the "SelectList" class.

Please refer this link for step by step approach with screen shot.

http://www.c-sharpcorner.com/UploadFile/4d9083/binding-dropdownlist-in-mvc-in-various-ways-in-mvc-with-data/

Bhuvan

Self-Innovator, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vijaysaklani on: 8/12/2016 [Member] Starter | Points: 25
Posted by: Bhuvanesh6 on: 8/18/2016 [Member] Starter | Points: 25

Up
0
Down
public class StoreRegistration

{
// this will hold the selected value
public string Hour { get; set; }

public IEnumerable<SelectListItem> Hours
{
get
{
return Enumerable
.Range(0, 23)
.Select(x => new SelectListItem {
Value = x.ToString("00"),
Text = x.ToString("00")
});
}
}
}
you could populate this view model inside the controller:

public class HomeController: Controller
{
public ActionResult Index()
{
var model = new StoreRegistration
{
// Set the Hour property to the desired value
// you would like to bind to
Hour = DateTime.Now.Hour.ToString("00")
};
return View(model);
}
}
and in your view simply:

@Html.DropDownListFor(
x => x.Hour,
new SelectList(Model.Hours, "Value", "Text")
)


Bhuvan

Self-Innovator, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response