将ajax数据传递给控制器​​时查看不呈现

时间:2018-05-23 09:07:47

标签: c# asp.net-mvc

我的控制器中有以下actoin:

[HttpPost]
    public ActionResult ShowClient(string ClientCode)
    {
        if (ClientCode == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

        }
        ClientViewModel clientViewModel = this.clientService.FindByClientCode(ClientCode);
        if (clientViewModel == null)
        {
            return HttpNotFound();

        }

        return View("ShowClient", clientViewModel);
    }

以下ajax代码正在点击控制器中的操作:

$.ajax({
                type: "POST",
                url: "/Client/ShowClient",
                data: "ClientCode=" + code/*{ "ClientCode": code }*/, //First item has latest ID
                //contentType: "application/json; charset=utf-8",
                success: function (data) {
                    if (data.length !== 0) {
                        console.log(data);
                    }
                },
                error: function (data) {
                    console.log(data);
                }
            });

观点:

@model BusinessModels.ClientViewModel


@{
    ViewBag.Title = "Show Client";
}

some usual html form

但我的观点没有显示 当我调试代码时,我可以看到clientViewModel确实包含数据,调试确实在我的视图中。 知道为什么它没有显示视图吗?

也许我可以使用RedirectToAction?

1 个答案:

答案 0 :(得分:0)

您需要在div中加载HTML并需要定义dataType:" HTML"。

  $.ajax({
              type: "POST",
            url: "/Client/ShowClient",
            data: "ClientCode=" + code/*{ "ClientCode": code }*/, //First item has latest ID
                contentType: "application/json; charset=utf-8",
                dataType: "html",
                success: function (data) {
                    $('#dialog').html(data);
                },
                failure: function (response) {
                    alert(response.responseText);
                },
                error: function (response) {
                    alert(response.responseText);
                }
            });
        });

有关详细信息,请参阅Working example