当我提交表单时没有选择任何值vs通过此例外:我想知道如何处理此下拉列表的验证 [英] When i submit the form without selecting any value vs through this exception: i want to know how to handle the validation for this dropdown list

查看:83
本文介绍了当我提交表单时没有选择任何值vs通过此例外:我想知道如何处理此下拉列表的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

没有类型为'IEnumerable< SelectListItem>'的ViewData项具有键'categoryId'。





这是我的代码

控制器:

[HttpGet]

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'categoryId'.


This is my code
Controller:
[HttpGet]

public ActionResult AddNewProduct()
       {




ViewBag.Category = new List<SelectListItem>
                               {

                                   new SelectListItem{Text="DELL",Value="1"},
                                   new SelectListItem{Text="HP",Value="2"},
                                   new SelectListItem{Text="APPLE",Value="3"},
                                   new SelectListItem{Text="ACER",Value="4"},
                                   new SelectListItem{Text="LENOVO",Value="5"},
                                   new SelectListItem{Text="SAMSUNG",Value="6"},
                                   new SelectListItem{Text="ASUS",Value="7"}
                               };


           return View("AddNewProduct");
       }





// Razor查看





//Razor View



@ Html.DropDownListFor(

m => m.categoryId,

@ ViewBag.Category as List< SelectListItem> ;,

new {@ class =dropdownList,style =width:25%;}}


@Html.DropDownListFor(
m => m.categoryId,
@ViewBag.Category as List<SelectListItem>,
new { @class="dropdownList", style = "width:25%;" })





我的尝试:



http://www.asp.net/mvc/overview/ old-versions / with-the-dropdownlist-box-and-jquery / using-the-dropdownlist-helper-with-aspnet-mvc



What I have tried:

http://www.asp.net/mvc/overview/older-versions/working-with-the-dropdownlist-box-and-jquery/using-the-dropdownlist-helper-with-aspnet-mvc

推荐答案

模型您从控制器传入Razor视图时没有名为categoryId的属性。错误消息必须更清楚多少?



由于您没有显示相应的代码,我们无法说出任何其他信息。您不会从控制器返回此视图,也不会显示应该从控制器获取视图模型对象的Razor视图。
The model you passed into the Razor view from your controller doesn't have a property called "categoryId". How much more clearer does the error message have to be?

We can't say anything else about it because you haven't shown the appropriate code. You show nothing from the controller returning this view and nothing of the Razor view that should be getting a view model object from the controller.


这篇关于当我提交表单时没有选择任何值vs通过此例外:我想知道如何处理此下拉列表的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆