Actually i am beginner in mvc and web programming so i have an error please help me i used viewbag
it works fine and also when i submit the values it store in my database but i got this error and i can't understand it please help me : **The ViewData item that has the key 'CategoryId' is of type 'System.Byte' but must be of type 'IEnumerable<SelectListItem>'.**
There really isn't an easier way to do it. You'll have to use an ajax call or some custom JS to accomplish cascading select lists. Sometimes when I'm feeling lazy, Ill make an ajax call that returns a partial view containing an entire new list and replace
the old list.
my next problem is :how to relate this two dropdown box to eachother
Really, you should have marked Radu's post as the answer and created a new thread. Not only does it give Radu the points he deserves but helps other people, with the same problem, find the answer.
totti_acmila...
Member
37 Points
30 Posts
MVC3 Dropdownlist problem
Feb 02, 2012 09:48 AM|LINK
Actually i am beginner in mvc and web programming so i have an error please help me i used viewbag
it works fine and also when i submit the values it store in my database but i got this error and i can't understand it please help me :
**The ViewData item that has the key 'CategoryId' is of type 'System.Byte' but must be of type 'IEnumerable<SelectListItem>'.**
in this line of my view :
@Html.DropDownList("CategoryId" , (SelectList)ViewBag.Cat , "--Select One--")
totti_acmila...
Member
37 Points
30 Posts
Re: MVC3 Dropdownlist problem
Feb 02, 2012 09:56 AM|LINK
this is my model:
public class AddLink { [Required] [Display(Name = "Url")] [DataType(DataType.Url)] public string Url { get; set; } [Display(Name = "Title")] public String Title { get; set; } [Display(Name = "ExpireDate")] public DateTime ExpireDate { get; set; } [Display(Name = "Category")] public byte CategoryId { get; set; } [Display(Name = "SubCategory")] public byte SubCategoryId { get; set; } }and
this is my controller:
// GET: /Link/Create public ActionResult Create() { ViewBag.Cat = new SelectList(db.Categories, "CategoryId","Title") ; ViewBag.SubCat = new SelectList(db.SubCategories , "SubCategoryId", "Title"); return View(); } // // POST: /Link/Create [HttpPost] public ActionResult Create(AddLink link) { if (ModelState.IsValid) { Link l = new Link(); l.Url = link.Url ; l.Title = link.Title ; l.CreationDate = System.DateTime.Now; l.Hit = 0; l.Rate = 0; l.MemberId = int.Parse(@User.Identity.Name.Split('|')[0]); l.CategoryId = System.Convert.ToByte ( link.CategoryId); l.SubCategoryId=link.SubCategoryId ; l.LanguageId = 1; LinkDirectoryEntities db = new LinkDirectoryEntities(); int u = db.Links.Where(x => x.Title == link.Title).Count(); if (u == 0) { db.Links.Add(l); db.SaveChanges(); } else { ModelState.AddModelError("", "The current Url exist!"); } } return View(link); }and finally this is my view:
@model LinkDirectory.Models.Link.AddLink @{ ViewBag.Title = "Create"; } <h2>Create</h2> <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>Link</legend> <div class="editor-label"> @Html.LabelFor(model => model.Url) </div> <div class="editor-field"> @Html.EditorFor(model => model.Url) @Html.ValidationMessageFor(model => model.Url) </div> <div class="editor-label"> @Html.LabelFor(model => model.Title) </div> <div class="editor-field"> @Html.EditorFor(model => model.Title) @Html.ValidationMessageFor(model => model.Title) </div> <div class="editor-label"> @Html.LabelFor(model => model) </div> <div class="editor-label"> @Html.LabelFor(model => model.CategoryId) </div> <div class="editor-label"> @Html.DropDownList("CategoryId" , (SelectList)ViewBag.Cat , "--Select One--") </div> <div class="editor-label"> @Html.LabelFor(model => model.SubCategoryId) </div> <div class="editor-label"> @Html.DropDownList("SubCategoryId", (SelectList)ViewBag.SubCat , "--Select One--") </div> <p> <input type="submit" value="Create" /> </p> </fieldset> } <div> @Html.ActionLink("Back to List", "Index") </div>So,in bolded line in view i got follow error even the code works fine and i got new record in my database
The ViewData item that has the key 'CategoryId' is of type 'System.Byte' but must be of type 'IEnumerable<SelectListItem>'.
raduenuca
All-Star
24675 Points
4250 Posts
Re: MVC3 Dropdownlist problem
Feb 02, 2012 10:31 AM|LINK
this happens in the HttpPost Create action right? When you return the view again: return View(link);
You need to call
ViewBag.Cat = new SelectList(db.Categories, "CategoryId","Title") ;
ViewBag.SubCat = new SelectList(db.SubCategories , "SubCategoryId", "Title");
again before return View(link);
Radu Enuca | Blog
totti_acmila...
Member
37 Points
30 Posts
Re: MVC3 Dropdownlist problem
Feb 02, 2012 10:48 AM|LINK
thank you so much dear Mr.Raduenuca
it resolved.
my next problem is :how to relate this two dropdown box to eachother
i mean when i select value in first combo
next cobmo values automatically loaded ?
for example first combo is country and second one is cities
but i want do that with my codes not new code
thanks
raduenuca
All-Star
24675 Points
4250 Posts
Re: MVC3 Dropdownlist problem
Feb 02, 2012 10:54 AM|LINK
You can follow this tutorial. It covers more than one way to cascade dropdown lists.
Radu Enuca | Blog
totti_acmila...
Member
37 Points
30 Posts
Re: MVC3 Dropdownlist problem
Feb 02, 2012 11:00 AM|LINK
Actually i dont want to change my codes i am just looking for simple way.
because i am amature in web programming and mvc and i checked your page before but i couldn't get too much
totti_acmila...
Member
37 Points
30 Posts
Re: MVC3 Dropdownlist problem
Feb 02, 2012 12:40 PM|LINK
any suggestion please ?????
ryanw51
Contributor
2363 Points
511 Posts
Re: MVC3 Dropdownlist problem
Feb 02, 2012 12:44 PM|LINK
There really isn't an easier way to do it. You'll have to use an ajax call or some custom JS to accomplish cascading select lists. Sometimes when I'm feeling lazy, Ill make an ajax call that returns a partial view containing an entire new list and replace the old list.
Yorrick vd V...
Participant
1674 Points
301 Posts
Re: MVC3 Dropdownlist problem
Feb 02, 2012 12:46 PM|LINK
Hi,
Pls take a look at the following post, maybe it will help you on your way:
You can download a working example project from here: https://skydrive.live.com/?cid=2bfae4e91bf38268#cid=2BFAE4E91BF38268&id=2BFAE4E91BF38268%21128
Download the CascadedDdl.zip file.
Hope this helps.
Regards,
Yorrick
&
Don't forget to click "Mark As Answer" on the post that helped you.
stevenbey
All-Star
16526 Points
3378 Posts
Re: MVC3 Dropdownlist problem
Feb 02, 2012 12:50 PM|LINK
Really, you should have marked Radu's post as the answer and created a new thread. Not only does it give Radu the points he deserves but helps other people, with the same problem, find the answer.
http://stevenbey.com
Recursion: see Recursion