0

是否可以从我的控制器中的方法刷新编辑视图?

我有一个对控制器进行回调的局部视图。通过这个方法,我想根据我传递给这个方法的值刷新我的整个编辑视图。

  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);
    }

提前致谢,

编辑:

我会详细说明一下。我面临的情况如下:

需要为医生分配一个实践,这不仅会影响医生的 practiceId,而且还会影响我的医生视图模型上的 20 个其他属性,并且它们需要反映在视图中。也许我通过寻求上述解决方案来看待这个错误的方式。

4

2 回答 2

1

如何使用 jQuery 加载函数调用您的控制器方法 trhu JSON?你调用你的控制器方法,更新你的 ViewModel 并返回你的编辑视图或其他东西。

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>

您的控制器保持不变。

于 2013-04-10T16:10:36.517 回答
0

好的,所以我使用了所有解决方案来找到适合我的情况的解决方案。

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);
    }
于 2013-04-11T10:41:34.093 回答