asp.net-mvc – 具有键’XXX’的ViewData项的类型为’System.Str
发布时间:2020-12-16 07:29:51 所属栏目:asp.Net 来源:网络整理
导读:参见英文答案 The ViewData item that has the key ‘XXX’ is of type ‘System.Int32’ but must be of type ‘IEnumerableSelectListItem’????????????????????????????????????3个 对MVC来说很新.尝试提交包含静态DropDownListFor的表单时,我收到以下异
参见英文答案 >
The ViewData item that has the key ‘XXX’ is of type ‘System.Int32’ but must be of type ‘IEnumerable<SelectListItem>’????????????????????????????????????3个
对MVC来说很新.尝试提交包含静态DropDownListFor的表单时,我收到以下异常. The ViewData item that has the key 'CurrentPosition' is of type 'System.String' but must be of type 'IEnumerable<SelectListItem>'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidOperationException: The ViewData item that has the key 'CurrentPosition' is of type 'System.String' but must be of type 'IEnumerable<SelectListItem>'. Source Error: Line 36: @Html.LabelFor(m => m.CurrentPosition,new { @class = "col-md-2 control-label" }) Line 37: <div class="col-md-10"> Line 38: @Html.DropDownListFor(m => m.CurrentPosition,ViewData["Positions"] as SelectList) Line 39: @Html.ValidationMessageFor(m => m.CurrentPosition,"",new { @class = "text-danger" }) Line 40: </div> 模型: [Display(Name = "Current Position")] [Required(ErrorMessage = "Please select their current position")] public string CurrentPosition { get; set; } 视图: <div class="form-group"> @Html.LabelFor(m => m.CurrentPosition,new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @Html.DropDownListFor(m => m.CurrentPosition,ViewData["Positions"] as SelectList) @Html.ValidationMessageFor(m => m.CurrentPosition,new { @class = "text-danger" }) </div> </div> 控制器: [HttpGet] public ActionResult Sales() { var positions = new SelectList(new [] { new { value = 0,text = "Select a Position..." },new { value = 1,text = "Merchandiser" },new { value = 2,text = "ISR" },new { value = 3,text = "TM" },new { value = 4,text = "AISR" },new { value = 5,text = "TAM" },new { value = 6,text = "BAM" },new { value = 7,text = "CSR" },new { value = 8,text = "Director" },new { value = 9,text = "TSM" },new { value = 10,text = "Tel-Sell" },new { value = 11,text = "Graphics" },new { value = 12,text = "Shelf Set" },new { value = 13,text = "Secretary" },new { value = 14,text = "POS" },new { value = 15,text = "Other" } },"value","text",1); ViewData["Positions"] = positions; return View(); } 我已经尝试了几种不同的方法,如果我直接在视图中直接填充列表项,我似乎只能使这个工作,我找不到正确的,因为我将在此视图中再次使用此列表.你怎么看? 编辑:发布操作.它现在是空的. [HttpPost] public ActionResult Sales(SalesViewModel model) { return View(model); } 解决方法
当您将表单提交到http post action方法时,通常会出现此错误,并且您将相同(视图)模型返回到视图,而不重新加载呈现SELECT元素所需的数据. 我想你现在的代码是这样的 [HttpPost] public ActionResult Sales(SomeViewModel model) { if(ModelState.IsValid) { // to do : Save and Redirect (PRG pattern) } return View(model); } 由于我们的视图代码使用的数据集是ViewData字典,因此您需要确保在调用返回View()之前重新加载ViewData [“Positions”]. [HttpPost] public ActionResult Sales(SomeViewModel model) { var positions = new SelectList(new [] { new { value = 0,text = "Director" } },1); ViewData["Positions"] = positions; // Reloading the data here return View(model); } 如果您不使用ViewData / ViewBag,但使用视图模型,则需要执行相同操作.将视图模型属性重新加载到填充SELECT元素所需的集合. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 将HttpRequestMessage转换为HttpRequest
- RegisterStartupScript无法在更新面板中工作,无法在asp.net
- asp.net – 直接将.aspx转换为.pdf [已关闭]
- asp.net-mvc – 2010初学者指南流利的nHibernate
- asp.net-mvc-5 – WebApi 2和MVC 5用户使用不同的路由属性吗
- asp.net – Gridview在列标题中排序/向下箭头
- asp.net – ASPX需要一个免费的datepicker
- asp.net – 我仍然犹豫使用ViewModels而不是View for Model
- asp.net – 是存储库单例或静态还是没有这些?
- asp.net-mvc – ASP.NET MVC中的全局错误处理(控制器之外)
推荐文章
站长推荐
- asp.net-mvc – 在IISExpress上通过计算机名访问
- [ASP.NET MVC]通过对HtmlHelper扩展简化“列表控
- asp.net-mvc – ASP.NET MVC:返回FileResult时如
- asp.net mvc errorhandler没有显示自定义错误页面
- asp.net-mvc-5 – 将属性路由限制为特定的HTTP谓
- asp.net-mvc – 在asp.net MVC中授权属性和jquer
- asp.net-mvc – 如何在ASP.NET MVC API控制器中获
- asp.net – Global.asax的方法来自哪里?
- asp.net – ASP .Net文件上载超出最大请求长度错
- asp.net-mvc – 可以使用存储库将外键映射到对象
热点阅读