asp.net-mvc如何把一个视图放在 asp.net MVC 中的另一种观点?

发布时间: 2016/10/9 5:09:37
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

在这里我有两个动作命名创建和 PhotoUpload MainController 了这里是创建操作的代码。

// GET: Main/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Main/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Email,Password,FirstName,LastName,Gender,Birthday,ProfileImage,AboutUser")] User user)
        {
            if (ModelState.IsValid)
            {
                db.Users.Add(user);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(user);
        }

这里是 PhotoUpload 操作的代码。

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult PhotoUpload(PhotoModel model)
        {
            if (model.PhotoFile.ContentLength > 0)
            {
                var fileName = Path.GetFileName(model.PhotoFile.FileName);
                var filePath = Server.MapPath("/Content/Users/Images");
                string savedFileName = Path.Combine(filePath, fileName);
                model.PhotoFile.SaveAs(savedFileName);

            }
            return View(model);
        }

        public ActionResult PhotoUpload()
        {
            return View();
        }

这些都是用户和照片机型。这是用户模型

public partial class User
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public User()
        {
            this.Friends = new HashSet<Friend>();
            this.Friends1 = new HashSet<Friend>();
            this.Photos = new HashSet<Photo>();
        }

        public int UserId { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Gender { get; set; }
        public System.DateTime Birthday { get; set; }
        public string ProfileImage { get; set; }
        public string AboutUser { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Friend> Friends { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Friend> Friends1 { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Photo> Photos { get; set; }
    }

这是 PhotoModel

public class PhotoModel
    {
        [Required]
        public HttpPostedFileBase PhotoFile { get; set; }
    }

这是我现在想为一个视图。这是我主/创建视图enter image description here

这是我主/PhotoUpload 视图

enter image description here

现在我想把这 PhotoUpload 视图而不是 ProfileImage 我创建的视图里面的东西。我在哪里换这和如何?

解决方法 1:

您应该使用 ViewModel 那是传输数据,从意见的推荐的做法,在这种情况下你可以做以下正如 @StephenMuecke 的评论

视图模型

public class UserViewModel
{
    public string Email { get; set; }
    public string Password { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Gender { get; set; }
    public System.DateTime Birthday { get; set; }
    public string ProfileImage { get; set; }
    public string AboutUser { get; set; }
    [Required]
    public HttpPostedFileBase PhotoFile { get; set; }
}

控制器

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(UserViewModel model)
{
    if (ModelState.IsValid)
    {
        AddUser(model);
        SavePhoto(model.PhotoFile);
        return RedirectToAction("Index");
    }
    return View(user);
}
private void SavePhoto(HttpPostedFileBase file)
{
    if (file.ContentLength > 0)
    {
        var fileName = Path.GetFileName(file.FileName);
        var filePath = Server.MapPath("/Content/Users/Images");
        string savedFileName = Path.Combine(filePath, fileName);
        file.SaveAs(savedFileName);
    }
}
private void AddUser(UserViewModel model)
{
    var user = new User
    {
        Email = model.Email, Password = model.Password, FirstName = model.FirstName, LastName = model.LastName, Gender = model.Gender, Birthday = model.Birthday, ProfileImage = model.ProfileImage, AboutUser = model.AboutUser
    };
    db.Users.Add(user);
    db.SaveChanges();
}

进一步阅读︰

官方微信
官方QQ群
31647020