2016-07-26 60 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(); 
} 

对于进一步阅读:

相关问题