2016-07-26 65 views
1

這裏我有一個MainController,其中有兩個名爲Create和PhotoUpload的動作。以下是創建操作的代碼。如何在asp.net MVC的另一個視圖中放置一個視圖?

// 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事情我創建視圖裏面。我在哪裏改變這個和如何?

+0

你可以使用內置的約定? '@ H​​tml.EditorFor(X => x.PhotoFile)' – mxmissile

+0

好吧,我只是存儲照片的字符串(地址)在我的數據庫,我將照片(.jpg或其他)到一個文件夾,每當我做數據庫操作。所以我不能使用這個約定,因爲我只是在我的數據庫中存儲一個字符串。 –

+0

也許使用'@ Html.Action' – Hackerman

回答

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(); 
} 

對於進一步閱讀:

相關問題