Quote:
An exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll but was not handled in user code Additional information: There is no ViewData item of type 'IEnumerable<selectlistitem>' that has the key 'State'.

here is my controller :-
using ebpassprojects.Models; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using ebpassprojects; using System.Data.Entity; namespace ebpassprojects.Controllers public class ListController : Controller // GET: List private ApplicationDbContext db = new ApplicationDbContext(); private void PopulateLookups(ApplicationDbContext db) IEnumerable<SelectListItem> item = db.allofcity.Select(C => new SelectListItem Value = C.cities, Text = C.cities }).ToList(); ViewBag.city= item; IEnumerable<SelectListItem> item = db.allofstate.Select(C => new SelectListItem Value = C.States, Text = C.States }).ToList(); ViewBag.state = item; and my view :-
<div class = " form-group" > @Html.LabelFor(m => m.State, new { @class = " col-md-2 control-label" }) <div class = " col-md-10" > @Html.DropDownListFor(m => m.State, (IEnumerable<SelectListItem>)ViewBag.state, " Select College" , new { @class = " form-control" }) </ div > </ div > <div class = " form-group" > @Html.LabelFor(m => m.City, new { @class = " col-md-2 control-label" }) <div class = " col-md-10" > @Html.DropDownListFor(m => m.City, (IEnumerable<SelectListItem>)ViewBag.city, " Select College" , new { @class = " form-control" }) </ div > </ div > What I have tried:
i have tried viewdata instead of viewbag but its still not showing Your code seem ok, very likely, like @Graeme_Grant had pointed out, the code is missing "State" attribute/property from the Model. Here a quick test/example. I'm assuming PopulateLookups method is functioning, which I think it is.
1. Create a very simple class, let call it TestState
public class TestState public string State { get ; set ; } 2. Pick a controller/action, in my case, I picked home/Index
public ActionResult Index() PopulateLookups(db); return View(); 3. In the Index View, add the Model on the top and the dropdown below it
@model WebApplication4.Models.TestState <div class = " form-group" > @Html.LabelFor(m => m.State, new { @class = " col-md-2 control-label" }) <div class = " col-md-10" > @Html.DropDownListFor(m => m.State, (IEnumerable<SelectListItem>)ViewBag.state, new { @class = " form-control" }) </ div > </ div >
4. Run the application, navigate to home/index, you should see the dropdownlist with State in it.
5. If #4 work that proof your current Model is missing a property, add a State property in your Model that being referenced in the view. If that still doesn't work, post more code, share the model/view//controller with us again/
i want to bind this dropdownlist in my register page

here is my controller

i dont know where i put this

i tried one way but when i run it it shows me login page not regitration

here is my controller of registration page how to bind drpodownlist here

// GET: /Account/Register
[AllowAnonymous]
public ActionResult Register()
{


return View();
}

//
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<actionresult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
using (ApplicationDbContext db = new ApplicationDbContext())

if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
// Send an email with this link
// string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
// var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
// await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here");

return RedirectToAction("Index", "Home");
}
AddErrors(result);
}

// If we got this far, something failed, redisplay form
return View(model);
}
IEnumerable<SelectListItem> itenn = ... ViewBag.state = itenn;Spelling error.. itenn should be item
Also, you have changed the property from State to Value & Text (?) here:
C#
Enumerable<SelectListItem> itenn = db.allofstate.Select(C => new SelectListItem Value = C.States, Text = C.States }).ToList();
Did you try googling the error message like I just did? return keyword must not be followed by an object expression - Google Search [ ^ ]

Yes, this is why: Hide Copy Code
private void PopulateLookups(ApplicationDbContext db)
I am answering you inbetween compiles and running tests against code... I have given you plenty of information to fix it, you just need to do the work now.
ya i have tried many things searched on google

i have tried this method

[HttpGet]
public ActionResult register(allstate t)
{
using (ApplicationDbContext db = new ApplicationDbContext())
{
ViewBag.state = db.allofstate.Select(C => new SelectListItem
{
Value = C.States,
Text = C.States
}).ToList();
}

return View();
}


but still getting same error
Posting this working example, from a previous Q&A support session. Principle is still the same:
1. Data Model:
C#
public class DataModel public Queue<List<string>> Data { get ; set ; } = new Queue<List<string>>(); }2. Controller
public class HomeController : Controller public ActionResult Index() var results = new DataModel(); for ( int i = 0 ; i < 10 ; i++) results.Data.Enqueue( new List<string>() { " aaa" , " bbb" , " ccc" }); return View(results.Data); }2. View
Razor
ViewBag.Title = " Queue Data Test" ; @Model = IEnumerable<List<string>>; @foreach ( var item in Model) < p > data: @string .Join( " , " , item);
This is basic MVC programming. There is nothing complicated with the example. In the controller, I fill a model's property with values and then pass it to the view. The view then declares the model (enabling Intellisense and compiler validation) and iterates over the collection to display the values.

Start a new project, add the code and run it. Use breakpoints with the debugger and step through the code to see how it works.
done i got the solution , of my above problem

look what i have done

instead of using IEnumerable<selectlistitem> i have used

simple select list procedure

and in view also i have changed it to

@Html.DropDownListFor(m => m.States, (List<selectlistitem>)ViewBag.state, "Select state", new { @class = "form-control" })


now i am getting all states...finally
i have one question

i am doing my project with indiual authentication of mvc

here it uses indentitiy and all of that stuff

i have added some extra fields in the default created database using code first migration.

now i want to display all details of current logged in user and i also want to add feature like user can update his detail after login

i dont know how to do it in this default individual authenticated application
  • Read the question carefully.
  • Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  • If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  • Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid.
  •