找回密码
 立即注册

QQ登录

只需一步,快速开始

bjbs08

新手上路

1

主题

1

帖子

21

积分

新手上路

积分
21
  • 20

    金币

  • 主题

  • 帖子

最新发帖
bjbs08
新手上路   /  发表于:2013-4-18 17:11  /   查看:6139  /  回复:1
不存在具有键“categoryid”的“IEnumerable<SelectListItem>”类型的 ViewData
代码如下:
<span class="rowcontent">
                <%:Html.DropDownListFor(model=>model.categoryid,(IEnumerable<SelectListItem>)ViewData["categories"])%>
                <%:Html.ValidationMessageFor(model=>model.categoryid) %>
</span>



[HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                User user = CurrentUser();
                Log log = new Log();
                log.Title = collection["Title"];
                log.Contents = collection["Contents"];
                log.Publishat = System.DateTime.Now;
                log.readtimes = 0;
                log.sequence = mvcblog.Log.Count() + 1;
                log.userid = user.UserID;
                log.categoryid = System.Convert.ToInt32(collection["categoryid"]);
                if (string.IsNullOrEmpty(collection["Title"]))
                {
                    var categories = (from c in mvcblog.Category
                                      where c.UserID == user.UserID
                                      select c).ToList();
                    ViewData["categories"] = new SelectList(categories, "categoryid", "Name");
                    TempData["message"] = "标题不能为空";
                    return View(log);
                }
                else
                {
                    mvcblog.Log.Attach(log);
                    mvcblog.Entry(log).State = EntityState.Added;
                    mvcblog.SaveChanges();
                    
                }
                return RedirectToAction("Index/"+user.UserID.ToString());  
            }
            catch
            {
                return View();
            }
        }

1 个回复

倒序浏览
ZenosZeng讲师达人认证 悬赏达人认证
超级版主   /  发表于:2013-4-27 11:31:00
沙发
回复 1楼bjbs08的帖子

可能是:  if (string.IsNullOrEmpty(collection[&quot;Title&quot;])) 这个地方 title 是空了,没有进入 if条件。

如有需要你也可以发一个可以重现你问题的工程给我们调试。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部