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
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/
IEnumerable<SelectListItem> itenn = ...
ViewBag.state = itenn;Spelling error..
itenn
should be
item
Also, you have changed the property from
State
to
Value & Text
(?) here:
Enumerable<SelectListItem> itenn = db.allofstate.Select(C =>
new
SelectListItem
Value = C.States,
Text = C.States
}).ToList();
Posting this working example, from a previous Q&A support session. Principle is still the same:
1. Data Model:
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
ViewBag.Title =
"
Queue Data Test"
;
@Model
= IEnumerable<List<string>>;
@foreach
(
var
item
in
Model)
<
p
>
data:
@string
.Join(
"
, "
, item);
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.
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);
}