在system.web.mvc.dll中但未在用户代码中处理其他信息:没有类型为'ienumerable< selectlistitem>'的viewdata项目具有关键'状态'的。 [英] 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'.
问题描述
Quote:System.Web.Mvc.dll中出现'System.InvalidOperationException'类型的异常,但未在用户代码中处理附加信息:没有类型为'IEnumerable< selectlistitem>'的ViewData项目,其键有'State'。
这里是我的控制器: -
使用ebpassprojects.Models;
使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
使用ebpassprojects;
使用System.Data.Entity;
名称空间ebpassprojects.Controllers
{
公共类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;
}
}
}
和我的观点: -
< 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>
我的尝试:
i尝试过viewdata而不是viewbag,但它仍然没有显示
你的代码似乎没问题,很有可能像@Graeme_Grant所指出的那样,代码从模型中缺少State属性/属性。这是一个快速测试/示例。我假设PopulateLookups方法正在运行,我认为它是。
1.创建一个非常简单的类,我们称之为TestState
public class TestState
{
< span class =code-keyword> public string 状态{ get ; set ; }
}
2.选择一个控制器/动作,在我的情况下,我选择回家/索引
public ActionResult Index()
{
PopulateLookups(db);
return 查看();
}
3.在索引视图中,添加顶部的模型及其下方的下拉菜单
@ 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 = 表单-control})
< / div >
< / div >
4.运行应用程序,导航到home / index,您应该看到包含State的下拉列表。
5.如果#4工作证明您当前的模型缺少属性,请在模型中添加一个在视图中引用的State属性。如果仍然不起作用,发布更多代码,再次与我们共享模型/视图//控制器/
IEnumerable< SelectListItem> itenn = ...
ViewBag.state = itenn;拼写错误..
itenn
应为item
此外,您已将该属性从州
更改为价值与优惠文本
(?)here:Enumerable< SelectListItem> itenn = db.allofstate.Select(C = > new SelectListItem
{
Value = C.States,
Text = C.States
})。ToList();
发布此工作示例,来自之前的Q& A支持会议。原则仍然相同:
1.数据模型:public class DataModel
{
public Queue< List< string>>数据{获取; set ; }
= new 队列< List< string>>();
}2。控制器
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 查看(results.Data);
}
}2。查看
@ {
ViewBag.Title =队列数据测试;
}
@Model = IEnumerable < List< string > 跨度>取代;
< div >
@foreach(模型中的var项)
{
< p > 数据:
@ string.Join(,,item);
< / p >
}
< / div > 跨度>
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/
IEnumerable<SelectListItem> itenn = ... ViewBag.state = itenn;Spelling error..
itenn
should beitem
Also, you have changed the property fromState
toValue & 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>>; <div> @foreach (var item in Model) { <p> data: @string.Join(", ", item); </p> } </div>
这篇关于在system.web.mvc.dll中但未在用户代码中处理其他信息:没有类型为'ienumerable< selectlistitem>'的viewdata项目具有关键'状态'的。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!