I have what would seem a common problem but I cant seem to find an answer.
I am trying to populate a drop down list from my controller for a create action. I can display the dropdown as I want but when I cannot get the value to be stored in the database.
My models:
public class Collection
{
public Guid CollectionId { get; set; }
public CollectionType CollectionType { get; set; }
}
public class CollectionType
{
public Guid CollectionTypeId { get; set; } public String Title { get; set; }
}
Controller:
//
// GET: /Collection/Create
public ActionResult Create()
{
ViewBag.CollectionTypes = _db.CollectionTypes.OrderBy(ct=> ct.Title).ToList();
var collection = new Collection();
return View(collection);
}
View:
@Html.DropDownListFor(ct => ct.CollectionType, new SelectList(ViewBag.CollectionTypes as System.Collections.IEnumerable, "CollectionTypeId", "Title"))
@Html.ValidationMessageFor(model => model.CollectionType)
This above snippet will show me the message "The value '50117dae-a7d7-4371-926f-7b1f7aaec133' is invalid."
if I change the line to
@Html.DropDownListFor(ct => ct.CollectionType.CollectionTypeId, new SelectList(ViewBag.CollectionTypes as System.Collections.IEnumerable, "CollectionTypeId", "Title"))
@Html.ValidationMessageFor(model => model.CollectionType)
the page will not do anything - I believe this could be because the controller is trying to add a new CollectionType.
I will create a ViewModel called CollectionViewModel like this:
public class CollectionViewModel
{
public Collection Collection {get;set};
public int CollectionTypeId {get;set}
public SelectList CollectionTypes {get;set;}
}
or modify you Collection class to include a CollectionTypeId as well besides the full CollectionType property in which case you remove the property from above.
Before shwoing the view create a new instance of the CollectionViewModel an set the SelectList as you already did (instead of placing it in the ViewBag).
davidtait
Member
5 Points
4 Posts
MVC 3 Razor DropDownListFor and Model property from EFCodeFirst
Feb 20, 2011 09:24 PM|LINK
I have what would seem a common problem but I cant seem to find an answer.
I am trying to populate a drop down list from my controller for a create action. I can display the dropdown as I want but when I cannot get the value to be stored in the database.
My models:
public class Collection { public Guid CollectionId { get; set; } public CollectionType CollectionType { get; set; } }public class CollectionType { public Guid CollectionTypeId { get; set; }public String Title { get; set; } }
Controller:
// // GET: /Collection/Create public ActionResult Create() { ViewBag.CollectionTypes = _db.CollectionTypes.OrderBy(ct=> ct.Title).ToList(); var collection = new Collection(); return View(collection); }View:
@Html.DropDownListFor(ct => ct.CollectionType, new SelectList(ViewBag.CollectionTypes as System.Collections.IEnumerable, "CollectionTypeId", "Title")) @Html.ValidationMessageFor(model => model.CollectionType)
This above snippet will show me the message "The value '50117dae-a7d7-4371-926f-7b1f7aaec133' is invalid."
if I change the line to
@Html.DropDownListFor(ct => ct.CollectionType.CollectionTypeId, new SelectList(ViewBag.CollectionTypes as System.Collections.IEnumerable, "CollectionTypeId", "Title")) @Html.ValidationMessageFor(model => model.CollectionType)the page will not do anything - I believe this could be because the controller is trying to add a new CollectionType.
I would appreciate any advice on this issue.
Thanks
raduenuca
All-Star
24675 Points
4250 Posts
Re: MVC 3 Razor DropDownListFor and Model property from EFCodeFirst
Feb 21, 2011 02:57 AM|LINK
I will create a ViewModel called CollectionViewModel like this:
public class CollectionViewModel { public Collection Collection {get;set}; public int CollectionTypeId {get;set} public SelectList CollectionTypes {get;set;} }or modify you Collection class to include a CollectionTypeId as well besides the full CollectionType property in which case you remove the property from above.
Before shwoing the view create a new instance of the CollectionViewModel an set the SelectList as you already did (instead of placing it in the ViewBag).
In the view use:
or
if you choosed to add the CollectionTypeId inside the Collection class.
Radu Enuca | Blog
davidtait
Member
5 Points
4 Posts
Re: MVC 3 Razor DropDownListFor and Model property from EFCodeFirst
Feb 21, 2011 09:17 AM|LINK
Thank you Radu,
I added CollectionTypeId to my Collection model and it now works perfectly.