不存在具有键“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();
}
} |
|