如何在asp.net MVC的另一个视图中放置视图?

时间:2016-07-26 20:26:17

标签: asp.net-mvc asp.net-mvc-4

这里我有一个MainController,其中有两个名为Create和PhotoUpload的动作。以下是“创建操作”的代码。

// 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

这是我/ Main / PhotoUpload View

enter image description here

现在我想在我的创建视图中放置这个PhotoUpload视图而不是ProfileImage。我在哪里更改此以及如何更改?

1 个答案:

答案 0 :(得分: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();
}

进一步阅读: