1

我是 MVC 的新手,我遇到了这个问题,请帮助我,当我尝试将图像上传到我的网站项目时,出现此错误“没有具有键 'TagId' 的 'IEnumerable' 类型的 ViewData 项。” 这是我的视图模型:

@model ImageSharingWithAuth.Models.ImageView

@{
    ViewBag.Title = "Upload an Image";
}

<h2>@ViewBag.Title </h2>

@{Html.BeginForm("Upload", "Images", FormMethod.Post, new {enctype ="multipart/form-data"});}

<fieldset>
    <legend >
        Enter Image Information
    </legend>
        @Html.AntiForgeryToken()

    <p>
        Caption: @Html.TextBoxFor(model=>model.Caption)
                @Html.ValidationMessage("Caption")

    </p>
    <p>

       Select a tag : @Html.DropDownListFor(model=>model.TagId , ViewBag.Tags as SelectList)

    </p>
    <p>
        Description :<br />
        @Html.TextAreaFor(model=> model.Description,5,40,null)
                @Html.ValidationMessage("Description")

    </p>
    <p>

        Date taken: @Html.TextBoxFor(model=>model.DateTaken)
                @Html.ValidationMessage("DateTaken")


    </p>
    <p>
        <input type="file" name="ImageFile" />
    </p>
    <p>
    <input type="submit" value="Upload" />

    </p>
</fieldset>

<p>@ViewBag.Message</p>
<p> @Html.ValidationSummary() </p>
@{Html.EndForm(); }

这是上传的获取和设置:

private ImageSharingDB db = new ImageSharingDB();
        [HttpGet]
        public ActionResult Upload()
        {

            CheckAda();

            //string userid = GetLoggedInUser();
            //if (userid == null) {
            //    return ForceLogin();
            //}
            //else

            {
                ViewBag.Message = "";
                //IEnumerable<Tag> tags = db.Tags;

               ViewBag.Tags = new SelectList(db.Tags, "Id", "Name",1);


                return View();
            }
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Upload(ImageView image, HttpPostedFileBase ImageFile)
        {

            CheckAda();
            TryUpdateModel(image);


            if (ModelState.IsValid)
            {
                String userid = GetLoggedInUser();
                //if (userid != null)
                //{
                //    return ForceLogin();
                //}
                //else 

                    User user = db.Users.SingleOrDefault(u => u.Userid.Equals(userid));

                    if (user != null)
                    {


                        // save image info on the db


                        Image imageEntity = new Image();
                        imageEntity.Id = image.Id;
                        imageEntity.Caption = image.Caption;
                        imageEntity.Description = image.Description;
                        imageEntity.DateTaken = image.DateTaken;

                        imageEntity.Approved = false;
                        imageEntity.User = user;
                        imageEntity.TagId = image.TagId;


                        if (ImageFile != null && ImageFile.ContentLength > 0)
                        {
                            db.Images.Add(imageEntity);
                            db.SaveChanges();
                            String imgFileName = Server.MapPath("~/Content/Images/img-"+imageEntity.Id+".jpg");
                            ImageFile.SaveAs(imgFileName);

                            return View("Details", image);

                            //return RedirectToAction("Details", imageEntity.Id);
                        }
                        else
                        {
                            ViewBag.Message = "No such image file specified";
                            return View();
                        }


                    }
                    else
                    {
                        ViewBag.Message = "No Scuh userid registered";
                        return View();

                    }



            }
            else
            {
                ViewBag.Message = "Please crrect the errorsin the form!";
                return View();
            }

        }

这是 ImageView 的模型:

namespace ImageSharingWithAuth.Models
{
    public class ImageView
    {

        [Required]
        [StringLength(40)]
        public String Caption { get; set; }
        [Required]
        public int TagId { get; set; }
        [Required]
        [StringLength(200)]
        public String Description { get; set; }
        [Required]
        [DataType(DataType.Date)]
         [DisplayFormat(DataFormatString = "{0:d}",ApplyFormatInEditMode=true)]
        public DateTime DateTaken { get; set; }

        [ScaffoldColumn(false)]
        public int Id;
        [ScaffoldColumn(false)]
        public String Userid { get; set; }
        [ScaffoldColumn(false)]
        public String TagName { get; set; }





    }
}

和图像模型:

namespace ImageSharingWithAuth.Models
{
    public class Image
    {

        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public virtual int Id {get;set;}
        [MaxLength(40)]
        public virtual string Caption { get; set; }
        [MaxLength(200)]
        public virtual string Description { get; set; }
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:d}")]
        public virtual DateTime DateTaken { get; set; }

        public virtual bool Approved { get; set; }

        [ForeignKey("User")]

        public virtual int UserId { get; set; }
        public virtual User User { get; set; }

        [ForeignKey("Tag")]
        public virtual int TagId { get; set; }
        public virtual Tag Tag { get; set; }

        public Image() {
            Approved = false;
        }
    }

}
4

1 回答 1

1

在控制器的 httpPost 方法的 else 块中尝试这个

 else
            {
                ViewBag.Message = "Please crrect the errorsin the form!";
                 ViewBag.Tags = new SelectList(db.Tags, "Id", "Name",1);
                return View();
            }
于 2013-10-18T10:33:53.530 回答