【问题标题】:Refresh the view from JQuery ajax call从 JQuery ajax 调用刷新视图
【发布时间】:2013-11-29 09:56:59
【问题描述】:

我有一个 cshml 视图,我在 div 中调用另一个视图,如下所示

    <div class="family_name" id="DisplayPartilView">
        <div class="content_part content_part2">
         <h1 class="family_nameStyle">Additional Contacts  <button id="AddEmergencyContact" jid="@ViewData["offerId"].ToString()" class="btn highlight" data-dismiss="modal" >
                +</button></h1> 
            <h6>&nbsp;</h6>
            <div id="listChildContactAuthorisationsCI" class="table_form">
                <table style="width: 640px;" id="CIAuthtable">
                    <tr>
                        <td>Contact Name</td>
                         <td title="This contact is authorised to drop off and collect this child.">Collection</td>
                        <td title="This contact is an emergency contact for the child.">Emergency</td>
                        <td title="This contact can authorise the child to participate in centre excursions.">Excursion</td>
                        <td title="This contact can authorised the administering of medication to the child.">Medical</td>
                    </tr>
                    @if (Model.ChildContactAuthorisations != null)
                    {

                          @Html.EditorFor(x => x.ChildContactAuthorisations)
                    }
                </table>
            </div>
        </div>

    </div>

此视图与其他视图共享,因此它位于 Shared/Editor Template 文件夹中。 我正在使用来自另一个页面(addcontact)的ajax,如下所示

          $.ajax({
                data: $.parseJSON('{"Id" : "' + EntityId + '"}'),
                type: 'POST',
                url: '@(Url.Action("GetAdditionalContacts", "QkEnrolment"))',

                success: function (result) {
                    alert(result.outerHTML);

                    $('div#listChildContactAuthorisationsCI').html(result);
                },
                error: function () {
                    alert('Error');
                }
               });

添加联系人后,控件将跟随控制器功能进入控制器

      public ActionResult GetAdditionalContacts(int Id)
    {
        Id = Convert.ToInt32(TempData["offerId"]);
        User user = DependencyResolver.Current.GetService<ICrud<User>>().Where(x => x.Username == WebSiteContext.Current.UserId).FirstOrDefault();
        var offer = _offerRepo.Get(Id);
        var child = _childRepo.Get(offer.Child.Id);
        var childStubContactAuthorisations = child.GetContactAuthorisations().ToList();

        List<Section_ChildContactAuthorisation> ChildContactAuthorisations1 = GetAggregateChildContactAuthorisations(user, childStubContactAuthorisations);

        TempData["offerId"] = Id;

       return Json(ChildContactAuthorisations1, JsonRequestBehavior.AllowGet);

    }

但我想用从上述控制器函数返回的模型(包含新添加的联系人)更新视图。但是视图没有更新。

任何人都可以帮助我吗 谢谢, 维迪亚

【问题讨论】:

  • 您正在返回 json 并期待 ajax 成功中的 html。我可以知道为什么吗?
  • 我是第一次这样做...错误地发生了这种情况...您能告诉我正确的方法吗?

标签: c# ajax asp.net-mvc


【解决方案1】:

现在您在 $('div#listChildContactAuthorisationsCI') 中将 json 设置为 html,您必须遍历返回的 json 并从这些值创建适当的 html,然后将它们添加到您的 $('div#listChildContactAuthorisationsCI') .html(数据) 示例:

data = '';
for(i=0;i<result.length;i++) {
   data += '<li>' + result.whateverIsInhere + '</li>';
}

如果您不知道 json 中的内容,请使用 Console.Log(result),然后使用开发人员工具(chrome 中的 F12)并查看里面的内容。

【讨论】:

  • 嗨,我将 myajax 调用更改如下
  • 而不是调试器使用 Console.Log(data);然后查看开发者工具(例如,chrome 中的 F12)你得到了什么。然后使用 data["name"] 检索您的值。然后将这些数据设置在您需要的位置。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-07
  • 2018-06-10
  • 1970-01-01
  • 2011-09-13
  • 2012-06-10
  • 1970-01-01
相关资源
最近更新 更多