Click here to Skip to main content
15,851,242 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have views Individual.cshtml, Family.cshtml, Company.cshtml, a drop down list and a button. The drop down list has values such as:
* Individual
* Family
* Company

Once an item is selected from the drop down the user will click the button and will be navigated to the specific view based on what was selected. My code is as follows

SQL
[HttpGet]
 public ActionResult SelectCategory()
 {
     List<SelectListItem> items = new List<SelectListItem>();

     items.Add(new SelectListItem { Text = "Select A Role", Value = "0" });
     items.Add(new SelectListItem { Text = "Individual", Value = "1" });
     items.Add(new SelectListItem { Text = "Family", Value = "2" });
     items.Add(new SelectListItem { Text = "Company", Value = "3" });

     ViewData["Roles"] = items;
     return View();
 }
 [HttpPost]
 public ActionResult SelectCategory(FormCollection form)
 {

     if (form.Equals(1))
     {
         return RedirectToAction("About", "Home");
     }
     return View();
 }



I keep getting an error. How do I do solve this
Posted
Comments
[no name] 27-Apr-14 19:54pm    
Since you do not tell us the error that you are getting, we will have to guess. My guess is that your FormCollection form would never be equal to the integer 1 so your redirect will never happen.
ZainNabi 28-Apr-14 12:00pm    
In my view I have the following code for a drop downlist<br><br>
 <br><br>
@{<br><br>
ViewBag.Title = "SelectCategory";<br><br>
Layout = "~/Views/Shared/_Layout1.cshtml";<br><br>
}<br><br>
 <br><br>

Select Category


 <br><br>
@using(Html.BeginForm())<br><br>
{<br><br>
<p>@Html.DropDownList("Roles", (IEnumerable<SelectListItem>)ViewData["Roles"])</p><br><br>
<input type="submit" value="Step 2" /><br><br>
}<br><br>
 <br><br>
The error I get when I select the individual property is this > An exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll but was not handled in user code<br><br>
 <br><br>
 <br>
<br>
Additional information: There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Roles'.
[no name] 28-Apr-14 18:58pm    
Okay... and? "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Roles'" tells you exactly what the problem is.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900