如何在MVC Core中替换@ Ajax.ActionLink

时间:2016-08-30 12:11:44

标签: asp.net-mvc razor asp.net-core-mvc

由于MVC Core不支持@Ajax ...我现在如何在剃刀页面中执行以下操作?

@Ajax.ActionLink("All","All",new AjaxOptions() {HttpMethod="GET",UpdateTargetId="div1",InsertionMode=InsertionMode.Replace})
<div id ="div1">
</div>

1 个答案:

答案 0 :(得分:1)

你不能不使用JqueryJavascript ,你可以做这样的事情来满足你的需求。

如果您未使用Jquery,请Download Latest Jquery加入您的项目。

<div id ="div1">
</div>

首先在Jquery

中添加此点击事件
$("#div1").click(function) {
        $.ajax({
            type: "POST",
            url: "/Controller/getData/",
            success: function (data) {
                $("#div1").empty();
                $("#div1").append(data.Html);
            },
            error: function (e) {

            }
       })
   }

获取HTML:

[HttpPost]
public ActionResult getData(PartnerDetail partner)
{
    // Your Logical Stuff..
    string viewName = "_childDiv"; // Specify fully qualified path if different Folder.
    string htmlData = RenderPartialView(viewName, modelData);
    return Json(new
    {
        Html = htmlData,
        SomeExtraData = true
    });
}

public string RenderPartialView(string viewName, object Model)
{
    string htmlData = "";
    if (string.IsNullOrEmpty(viewName))
        viewName = ControllerContext.RouteData.GetRequiredString("action");
    ViewData.Model = Model;
    using (StringWriter sw = new StringWriter())
    {
        ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
        ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
        viewResult.View.Render(viewContext, sw);

        htmlData = sw.GetStringBuilder().ToString();
    }
    return htmlData;
}

它会刷新您的div1内容而不刷新页面。