2015-09-04 27 views
1

我是新來的MVC,並試圖瞭解ViewModels。我知道如何使用Create和ViewModel,但我不確定如何使用View Model進行編輯?如何在MVC中使用ViewModel進行編輯?

我的VM:

public class BookingViewModel 
{ 
    [Display (Name = "Select Patient")] 
    public Guid PatientId { get; set; } 
    public IEnumerable<SelectListItem> PatientList { get; set; } 

    [Display(Name = "Select Practice")] 
    public Guid PracticeId { get; set; } 
    public IEnumerable<SelectListItem> PracticeList { get; set; } 

    [Display(Name = "Select Optician")] 
    public Guid OpticianId { get; set; } 
    public IEnumerable<SelectListItem> OpticiansList { get; set; } 

    public Optician Optician { get; set; } 

    [Display(Name = "Select Date")] 
    [DataType(DataType.Date)] 
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")] 
    public DateTime Date { get; set; } 

    [Display(Name = "Select Time")] 
    public Guid TimeId { get; set; } 
    public IEnumerable<SelectListItem> TimeList { get; set; } 
} 

我的控制器:

public ActionResult Create() 
{ 
    // Creates a new booking 
    BookingViewModel bookingViewModel = new BookingViewModel(); 
    // Initilises Select List 
    ConfigureCreateViewModel(bookingViewModel); 

    return View(bookingViewModel); 

} 

// Initilises Select List 
public void ConfigureCreateViewModel(BookingViewModel bookingViewModel) 
{ 
    // Displays Opticians Name - Needs changed to full name 
    bookingViewModel.OpticiansList = db.Opticians.Select(o => new SelectListItem() 
    { 
     Value = o.OpticianId.ToString(), 
     Text = o.User.FirstName 
    }); 

    // Displays Patients name - needs changed to full name DOB 
    bookingViewModel.PatientList = db.Patients.Select(p => new SelectListItem() 
    { 
     Value = p.PatientId.ToString(), 
     Text = p.User.FirstName 
    }); 

    // Displays Practice Name 
    bookingViewModel.PracticeList = db.Practices.Select(p => new SelectListItem() 
    { 
     Value = p.PracticeId.ToString(), 
     Text = p.PracticeName 
    }); 

    // Displays Appointment Times 
    bookingViewModel.TimeList = db.Times.Select(t => new SelectListItem() 
    { 
     Value = t.TimeId.ToString(), 
     Text = t.AppointmentTime 
    }); 


} 


// Allows Admin to create booking for patient 
// POST: Bookings1/Create 
[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Create(BookingViewModel bookingViewModel) 
{ 
    // to ensure date is in the future 
    if (ModelState.IsValidField("Date") && DateTime.Now > bookingViewModel.Date) 
    { 
     ModelState.AddModelError("Date", "Please enter a date in the future"); 
    } 



    // if model state is not valid 
    if (!ModelState.IsValid) 
    { 
     // Initilises Select lists 
     ConfigureCreateViewModel(bookingViewModel); 
     return View(bookingViewModel); // returns user to booking page 
    } 
    else // if model state is Valid 
    { 
     Booking booking = new Booking(); 
     // Sets isAvail to false 
     booking.isAvail = false; 
     booking.PracticeId = bookingViewModel.PracticeId; 
     booking.Optician = bookingViewModel.Optician; 
     booking.PatientId = bookingViewModel.PatientId; 
     booking.Date = bookingViewModel.Date; 
     booking.TimeId = bookingViewModel.TimeId; 

     // Generates a new booking Id 
     booking.BookingId = Guid.NewGuid(); 
     // Adds booking to database 
     db.Bookings.Add(booking); 
     // Saves changes to Database 
     db.SaveChanges(); 
     // Redirects User to Booking Index 
     return RedirectToAction("Index"); 
    } 
} 

我真的不知道該如何編輯視圖模型,任何建議將不勝感激

public ActionResult Edit(Guid? id) 
{ 
    if (id == null) 
    { 
     return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
    } 
    Booking booking = db.Bookings.Find(id); 
    if (booking == null) 
    { 
     return HttpNotFound(); 
    } 

    BookingViewModel bookingViewModel = new BookingViewModel() 
    { 
     Date = booking.Date, 
     OpticianId = booking.OpticianId, 
     PatientId = booking.PatientId, 
     PracticeId = booking.PracticeId, 
     TimeId = booking.TimeId 
    }; 

    return View(booking, bookingViewModel); 
} 

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Edit(Booking booking) 
{ 


    if (ModelState.IsValid) 
    { 

     db.Entry(booking).State = EntityState.Modified; 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 

    return View(booking); 
} 
+0

你從目前的工作中得到了什麼結果? – tcrite

+0

@tcrite GET編輯返回視圖 – coto2

回答

2

Controller.View接受2個模型/對象的方法沒有超載。

Edit() GET方法必須

public ActionResult Edit(Guid? id) 
{ 
    .... 
    BookingViewModel bookingViewModel = new BookingViewModel() 
    { 
    .... 
    } 
    // Call the ConfigureCreateViewModel() method so that you SelectList's are populated 
    // as you have done in the Create() method (ConfigureViewModel might be a better name?) 
    ConfigureCreateViewModel(bookingViewModel); 
    return View(bookingViewModel); // adjust this 
} 

和POST方法必須

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Edit(BookingViewModel model) 
{ 
    if (!ModelState.IsValid) 
    { 
    ConfigureCreateViewModel(model) 
    return View(model); 
    } 
    // Get your data model and update its properties based on the view model 
    Booking booking = db.Bookings.Find(id); 
    booking.PracticeId = bookingViewModel.PracticeId; 
    booking.OpticianId = bookingViewModel.OpticianId; 
    .... // etc 

    db.Entry(booking).State = EntityState.Modified; 
    db.SaveChanges(); 
    return RedirectToAction("Index"); 
} 

和你的看法應該有@model BookingViewModel

邊注:您的視圖模型應該不是包含財產public Optician Optician { get; set; }(你綁定到財產public Guid OpticianId { get; set; }

+0

有一個無效的爭論錯誤再次謝謝斯蒂芬。我以爲你必須將Id傳遞給POST方法。我開始瞭解View Model。感謝你的幫助! – coto2

+0

還應該加上'BookingViewModel'應該包含一個屬性'公共Guid? ID {get;組; }'你在GET方法('ID = booking.ID')中設置的,並且在POST方法中它應該是'Booking booking = db.Bookings.Find(model.ID);'因爲你有一個名爲'ID '在方法中,它的自動綁定(例如,你不需要ID屬性的隱藏輸入) –

相關問題