Asp.net mvc 3 MVC 3更新模型&;保存更改

Asp.net mvc 3 MVC 3更新模型&;保存更改,asp.net-mvc-3,Asp.net Mvc 3,这里是一个控制器的行动方法,我必须上传一个用户的个人资料图像 [HttpPost] public ActionResult UploadPhoto(int id, FormCollection form) { Profile profile = db.Profiles.Find(id); var file = Request.Files[0]; if (file.ContentLength > 512000)

这里是一个控制器的行动方法,我必须上传一个用户的个人资料图像

    [HttpPost]
    public ActionResult UploadPhoto(int id, FormCollection form)
    {
        Profile profile = db.Profiles.Find(id);

        var file = Request.Files[0];

        if (file.ContentLength > 512000)
        {
            ModelState.AddModelError(string.Empty, "Please limit your photo to 500 KB");
        }

        bool IsJpeg = file.ContentType == "image/jpeg";
        bool IsPng = file.ContentType == "image/png";
        bool IsGif = file.ContentType == "image/gif";

        if (!IsJpeg && !IsPng && !IsGif)
        {
            ModelState.AddModelError(string.Empty, "Only .jpeg, .gif, and .png images allowed");
        }

        if (file == null || file.ContentLength <= 0)
        {
            ModelState.AddModelError(string.Empty, "You must select an image to upload");
        }

        if (ModelState.IsValid)
        {
            try
            {
                string newFile = Path.GetFileName(file.FileName);
                file.SaveAs(Server.MapPath("/Content/users/" + User.Identity.Name + "/" + newFile));
                profile.ProfilePhotoPath = "/Content/users/" + User.Identity.Name + "/" + newFile;
                UpdateModel(profile);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        return View();
    }
它将ProfilePhotoPath值显示为“System.Web.HttpPostedFileWrapper”

现在,当应用程序点击下一行时:

UpdateModel(profile);
db.SaveChanges();
它显示ProfilePhotoPath值为“/Content/users/WebWired/myprofilepic.png”,应该是

但是,当应用程序点击下一行时:

UpdateModel(profile);
db.SaveChanges();
突然,ProfilePhotoPath值再次变为“System.Web.HttpPostedFileWrapper”。。。这就是它被保存的方式

如果这还不够奇怪的话,在我开始向文件上传添加逻辑之前,它确实起了作用,但这真的不应该与它有任何关系,因为它传递了所有这些

有人知道这里发生了什么,为什么会这样做,我做错了什么吗?

UpdateModel()使用控制器的值提供程序的值更新您的配置文件对象,即POST参数等。如果它找到一个名为“ProfilePhotoPath”的POST参数,您的profile.ProfilePhotoPath属性将设置为该值,覆盖刚刚手动设置的值

您的
字段(或用于将文件发布到服务器的任何方法)似乎有一个name属性:“ProfilePhotoPath”。该字段将变成服务器上的
HttpPostedFileWrapper
对象,其中包含有关发布文件的信息(内容长度、类型、文件名等)。对象UpdateModel将分配给profile.ProfilePhotoPath属性(因为它与属性同名)。由于它将一个对象分配给一个字符串属性,它将强制该对象成为一个字符串,从而产生“System.Web.HttpPostedFileWrapper”。

UpdateModel()使用控制器的值提供程序的值更新您的配置文件对象,即,如果它找到一个名为“ProfilePhotoPath”的POST参数,POST参数等,您的profile.ProfilePhotoPath属性将设置为该值,覆盖您刚才手动设置的值


您的
字段(或用于将文件发布到服务器的任何方法)似乎有一个name属性:“ProfilePhotoPath”。该字段将变成服务器上的
HttpPostedFileWrapper
对象,其中包含有关发布文件的信息(内容长度、类型、文件名等)。对象UpdateModel将分配给profile.ProfilePhotoPath属性(因为它与属性同名)。因为它将一个对象分配给一个字符串属性,所以它会将该对象强制为一个字符串,从而产生“System.Web.HttpPostedFileWrapper”。

“ProfilePhotoPath”似乎是上载文件的表单名。“你是对的,它是。。。哇,谢谢你,真是太棒了!就我而言。。。再次完美地工作。我只是编辑以消除关于我是如何得出这个结论的大量流言蜚语(并且完全忘记了Controller::UpdateModel是MVC提供的方法-我从未使用过它)。那是我的废话!时刻:-)似乎“ProfilePhotoPath”是您上传文件的表单名。“你说得对,它是。。。哇,谢谢你,真是太棒了!就我而言。。。再次完美地工作。我只是编辑以消除关于我是如何得出这个结论的大量流言蜚语(并且完全忘记了Controller::UpdateModel是MVC提供的方法-我从未使用过它)。那是我的废话!时刻:-)