ASP.net序列不包含任何元素

时间:2015-04-12 02:55:29

标签: c# asp.net asp.net-mvc database sequence

我正在尝试创建一个用户可以上传他们制作的jar文件的网站,他们可以设置价格,标题,描述等。我已经完成了所有工作,但我忘了为插件添加图像。现在我添加了一个可以上传插件图像的位置。现在我收到了这个错误:

http://gyazo.com/04548f60557a3071af320c5095df54cc

以下是我创建插件的方法:

[ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,Name,UserId,Added,Updated,Price,Description,ViewCount,PluginFile,PluginPicture")] Plugin plugin)
        {
            if (!base.ModelState.IsValid)
            {
                return base.View(plugin);
            }
            Plugin plugin1 = (
                from cp in this.db._Plugins
                orderby cp.Id
                select cp).ToList<Plugin>().Last<Plugin>();
            int id = plugin1.Id + 1;
            plugin.UserId = base.User.Identity.GetUserId();
            plugin.SKU = string.Concat("PLG", id);
            plugin.Added = new DateTime?(DateTime.Now);
            plugin.Updated = new DateTime?(DateTime.Now);
            string str = base.Server.MapPath("~//Content//Plugins//");
            HttpPostedFileBase item = base.Request.Files["PluginFile"];
            if (item != null)
            {
                item.SaveAs(string.Concat(str, Path.GetFileName(item.FileName.ToString())));
                plugin.PluginFile = Path.GetFileName(item.FileName.ToString());
            }
            this.db._Plugins.Add(plugin);
            this.db.SaveChanges();
            HttpPostedFileBase httpPostedFileBase = base.Request.Files["PluginPicture"];
            if (httpPostedFileBase != null)
            {
                string userId = base.User.Identity.GetUserId();
                int num = (
                    from pl in this.db._Plugins
                    where (pl.Name == plugin.Name) && (pl.UserId == userId)
                    select pl.Id).SingleOrDefault<int>();
                string str1 = base.Server.MapPath("~//Content//pluginimages//");
                PluginsMediaPicture pluginsMediaPicture = new PluginsMediaPicture()
                {
                    PluginId = num
                };
                httpPostedFileBase.SaveAs(string.Concat(str1, Path.GetFileName(httpPostedFileBase.FileName.ToString())));
                pluginsMediaPicture.PluginPicture = Path.GetFileName(httpPostedFileBase.FileName.ToString());
                this.db._PluginsMediaPictures.Add(pluginsMediaPicture);
                this.db.SaveChanges();
            }
            return base.RedirectToAction("Index");
        }

0 个答案:

没有答案