Hier habe ich einen MainController, in dem ich zwei Aktionen namens Create und PhotoUpload habe. Hier ist der Code für Aktion erstellen.Wie man eine Ansicht in asp.net MVC in eine andere Ansicht versetzen?
// 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);
}
Hier ist der Code für PhotoUpload Aktion.
[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();
}
Und das sind die Benutzer und Foto-Modelle. Dies ist das Benutzermodell
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; }
}
Dies ist das Photomodel ist
public class PhotoModel
{
[Required]
public HttpPostedFileBase PhotoFile { get; set; }
}
Und das ist, was ich als Ansicht bin immer jetzt. Das ist mein/Main/Ansicht erstellen
Und das ist mein/Main/PhotoUpload Ansicht
Jetzt möchte ich in meiner Ansicht erstellen statt ProfileImage
was diese PhotoUpload Ansicht setzen. Wo ändere ich das und wie?
Können Sie die in Konventionen gebaut verwenden? '@ Html.EditorFor (x => x.PhotoFile)' – mxmissile
Gut ich speichere nur String (Adresse) von Foto in meiner Datenbank und ich füge das Foto (.jpg oder was auch immer) zu einem Ordner hinzu, wenn ich Datenbankoperationen mache . Daher kann ich diese Konvention nicht verwenden, da ich nur eine Zeichenfolge in meiner Datenbank ablege. –
Vielleicht mit '@ Html.Action' – Hackerman