是否可以从控制器中的方法刷新编辑视图?
我有一个局部视图,它对控制器做一个回调。从这个方法,我想刷新我的整个编辑视图基于我传递给这个方法的值。
public ActionResult AssignPractice(Guid id, Guid doctorId)
{
var doctorViewModel = Map.This(_doctorService.GetDoctor(doctorId)).To<DoctorViewModel>();
AddSelectLists(doctorViewModel);
var practice = _practiceService.GetPractice(id);
doctorViewModel.Practice = new PracticeSelectListViewModel()
{Item = practice, DoctorId = doctorId, SelectList = _practices};
return View("Edit", doctorViewModel);
}
提前感谢,
编辑:我将详细说明一点。我面临的情况如下:
一个医生需要被分配一个实践,这不仅影响医生的实践id,而且还影响我的doctorviewmodel上的其他20个属性,它们需要反映在视图上。也许我是在以错误的方式看待这个问题,要求解决上述问题。
如何调用你的控制器方法JSON,使用jQuery加载函数?你调用你的控制器方法,更新你的ViewModel并返回你的Edit View或其他东西。
jQuery:$('#buttonToRefresh').click(function () {
$('#mainContent').load('@Url.Content("YourAction", "YourController")', $('#yourFormID').serialize());
});
视图:
<div id="mainContent">
@Html.BeginForm("Action", "Controller", FormMethod.Post, new { id = "yourFormID" })
{
// Your content goes here.
}
</div>
控制器保持不变
好的,所以我使用了所有的解决方案来找到适合我的解决方案。
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);
}