如何解决这个问题SelectListItem [英] how sloved this problem SelectListItem

查看:121
本文介绍了如何解决这个问题SelectListItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在表单中提交按钮时填充DropDownListFor with funcion这个错误



没有类型为'IEnumerable< SelectListItem>'的ViewData项具有密钥' ID_post'。



  public  ActionResult AddUser()
{
if (ModelState.IsValid)
{

if (ViewData [ Fill_Name_post] == null && ViewData [ Fill_Name_baker] == null
{
ViewData [ Fill_Name_post] = ClassPost.GenerateNumbers();
ViewData [ Fill_Name_baker] = Classtypebaker.GenerateNumbers();
}
return 查看();
}
return 查看();
}


[HttpPost]

public ActionResult AddUser([Bind(Include) = FName,FFamily,FFamily,Fsenfi,Fholidays)]用户postData)
{
if (ModelState.IsValid)
{
string status = ;
用户_User = 用户();

if string .IsNullOrEmpty(postData.FName)|| string .IsNullOrEmpty(postData.Fsenfi.ToString())|| string .IsNullOrEmpty(postData.FFamily)|| string .IsNullOrEmpty(postData.Fholidays)|| postData.FTypeBaker == 0 || postData.ID_post = = 0
{
ViewBag.name = 测试 ;
status = 0;
}
// todo:向存储库添加临时资料
< span class =code-keyword> if
(postData == null
return Json( false );
// int baker = _ClassPost.checkbaker(postData。);
< span class =code-keyword> if (status == 1
{

db.Users.Add(postData);
db.SaveChanges();


}
return 查看();
// else
{


// 返回Json(new {status},JsonRequestBehavior.AllowGet);

}
}
else
return 查看() ;
}

public static 列表< SelectListItem> GenerateNumbers()
{Context context = new Context();

var numbers =( from p in context.Posts
select new SelectListItem
{
Text = p.FNamePost,
Value = p.ID_Post.ToString()
});
return numbers.ToList();
}

解决方案

1.您正在使用,在您的列表中(方法 GenerateNumbers()),类型为 SelectListItem 的项目,此类没有名为
的属性
ID_Post 也没有属性 FNamePost



2.解决方案是使用现有属性此类:文本。有关详细信息,请参阅 MSDN [< a href =http://msdn.microsoft.com/en-us/library/system.web.mvc.selectlistitem(v=vs.118).aspx\"target =_ blanktitle =New Window> ^ ]。

iam fill DropDownListFor with funcion when submit button in form iam this error

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

 public ActionResult AddUser()
        {
            if (ModelState.IsValid)
            {
                
                if (ViewData["Fill_Name_post"] == null && ViewData["Fill_Name_baker"] ==null)
                {
                ViewData["Fill_Name_post"] = ClassPost.GenerateNumbers();
                 ViewData["Fill_Name_baker"] = Classtypebaker.GenerateNumbers();
               }
                return View();
            }
            return View();
        }


[HttpPost]
       
        public ActionResult AddUser([Bind(Include = "FName, FFamily, FFamily,Fsenfi,Fholidays")]  User postData)
        {
            if (ModelState.IsValid)
            {
                 string status = "";
                User _User = new User();

                if (string.IsNullOrEmpty(postData.FName) || string.IsNullOrEmpty(postData.Fsenfi.ToString()) || string.IsNullOrEmpty(postData.FFamily) || string.IsNullOrEmpty(postData.Fholidays) || postData.FTypeBaker == 0 || postData.ID_post == 0)
                {
                    ViewBag.name = "test";
                      status = "0";
                }
                //todo: Add temp to repository
                if (postData == null)
                    return Json(false);
                //  int baker = _ClassPost.checkbaker(postData.);
                if (status == "1")
                {

                    db.Users.Add(postData);
                    db.SaveChanges();

                    
                }
                return View();
                //  else
                {

                  
                    // return Json(new { status }, JsonRequestBehavior.AllowGet);

                }
            }
            else
                return View();
        }

  public static List<SelectListItem> GenerateNumbers()
        {Context context = new Context();
            
            var numbers = (from p in context.Posts
                           select new SelectListItem
                           {
                               Text = p.FNamePost,
                               Value = p.ID_Post.ToString()
                           });
            return numbers.ToList();
        }

解决方案

1.You are using, in your list (method GenerateNumbers()), items of type SelectListItem , and this class does not have property named
ID_Post also neither property FNamePost.

2.The solution is to use the exiting properties of this class: Text and Value. For details see in MSDN[^].


这篇关于如何解决这个问题SelectListItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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