好的,所以我使用了所有解决方案来找到适合我的情况的解决方案。
Javascript:
function Doctor() {
this.id = null;
this.FirstName = "";
this.LastName = "";
this.Code = "";
this.PhoneNumber = "";
this.PhoneNumber2 = "";
this.PhoneNumberActive = 0;
this.PhoneNumber2Active = 0;
this.Email = "";
this.Practice = new Practice();
function AssignPractice() {
var myModel = new Doctor();
myModel.id = Id.value;
myModel.FirstName = FirstName.lastChangedValue;
myModel.LastName = LastName.lastChangedValue;
myModel.Code = Code.lastChangedValue;
myModel.PhoneNumber = PhoneNumber.lastChangedValue;
myModel.PhoneNumber2 = PhoneNumber2.lastChangedValue;
myModel.PhoneNumberActive = PhoneNumberActive.lastChangedValue;
myModel.PhoneNumber2Active = PhoneNumber2Active.lastChangedValue;
myModel.Email = Email.lastChangedValue;
myModel.Practice.id = cbbPractice.lastSuccessValue;
var postData = JSON.stringify(myModel);
$.ajax({
url: assignPracticeUrl,
type: 'POST',
data: postData,
success: function (data) {
$('#yourFormID').html(data);
}
});
看法:
@using (Html.BeginForm("Edit", "Doctor", FormMethod.Post, new { id = "yourFormID" }))
控制器:
public ActionResult AssignPractice(FormCollection form)
{
JavaScriptSerializer js = new JavaScriptSerializer();
var doctor = js.Deserialize<Doctor>(form[0]);
var doctorViewModel = Map.This(_doctorService.GetDoctor(doctor.Id)).To<DoctorViewModel>();
AddSelectLists(doctorViewModel);
var practice = _practiceService.GetPractice(doctor.Practice.Id);
var practiceViewModel = Map.This(practice).To<PracticeViewModel>();
doctorViewModel.Practice = new PracticeSelectListViewModel() { Item = practice, SelectList = _practices };
doctorViewModel.FirstName = doctor.FirstName;
doctorViewModel.LastName = doctor.LastName;
doctorViewModel.PhoneNumber = doctor.PhoneNumber;
doctorViewModel.PhoneNumber2 = doctor.PhoneNumber2;
doctorViewModel.PhoneNumberActive = doctor.PhoneNumberActive;
doctorViewModel.PhoneNumber2Active = doctor.PhoneNumberActive;
doctorViewModel.Email = doctor.Email;
doctorViewModel.OpeningHours = practiceViewModel.OpeningHours;
doctorViewModel.AppointmentHours = practiceViewModel.AppointmentHours;
doctorViewModel.AddressViewModel = practiceViewModel.AddressViewModel;
doctorViewModel.Website = practiceViewModel.Website;
doctorViewModel.Labo = practiceViewModel.Labo;
doctorViewModel.KeyChain = practiceViewModel.KeyChain;
return PartialView("Edit", doctorViewModel);
}