Asp.net mvc 单击按钮后加载局部视图

Asp.net mvc 单击按钮后加载局部视图,asp.net-mvc,asp.net-ajax,asp.net-mvc-partialview,Asp.net Mvc,Asp.net Ajax,Asp.net Mvc Partialview,我有这个问题,已经搜索了很多,但现在有了正确的答案 我在我的_布局页面的页脚中有一个联系人表单,但当我单击按钮时,部分视图在新页面中打开 我记得要包括jquery.unobtrusive-ajax.js。这是我的 控制器: [HttpGet] public ActionResult Call() { return PartialView("_PartialFooter"); } [HttpPost] [ValidateAntiForger

我有这个问题,已经搜索了很多,但现在有了正确的答案

我在我的_布局页面的页脚中有一个联系人表单,但当我单击按钮时,部分视图在新页面中打开

我记得要包括jquery.unobtrusive-ajax.js。这是我的

控制器:

[HttpGet]
    public ActionResult Call()
    {
        return PartialView("_PartialFooter");
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Call(CallMe callMe)
    {
        if(ModelState.IsValid)
        {

        }
        return PartialView("_PartialFooter");
    }
_布局脚本位于底部的Body标记上方

 @using (Ajax.BeginForm("Call", "Home", new AjaxOptions { UpdateTargetId = "result" }))
                            {
                                <div id="result" class="margin-bottom-5">
                                    @Html.Action("Call", "Home")
                                </div>
                                <button class="btn btn-common-small margin-bottom-10 pull-right" type="submit">Send</button>
                            }

@Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @Scripts.Render("~/bundles/myscripts")
    @RenderSection("scripts", required: false)
@section Scripts {
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>

}
@使用(Ajax.BeginForm(“Call”、“Home”、新的AjaxOptions{UpdateTargetId=“result”}))
{
@动作(“呼叫”、“主页”)
发送
}
@Scripts.Render(“~/bundles/jquery”)
@Scripts.Render(“~/bundles/bootstrap”)
@Scripts.Render(“~/bundles/myscripts”)
@RenderSection(“脚本”,必需:false)
@节脚本{
}
_局部视图(局部视图)

@model servicemadsen.Models.CallMe
@Html.AntiForgeryToken()
@EditorFor(model=>model.Name,new{htmlAttributes=new{@class=“form control”,@placeholder=“Navn”})
@EditorFor(model=>model.Phone,new{htmlAttributes=new{@class=“form control”,@placeholder=“Telefon”})
@Html.TextAreaFor(model=>model.CallMeMessage,new{@class=“form control”、@placeholder=“Besked”、@cols=80、@rows=7})
@Html.ValidationMessageFor(model=>model.Name,string.Empty,new{@class=“field validation error”})
@Html.ValidationMessageFor(model=>model.Phone,string.Empty,new{@class=“field validation error”})
@Html.ValidationMessageFor(model=>model.CallMeMessage,string.Empty,new{@class=“field validation error”})

希望有人能帮上忙,这可能是我需要的一些虚拟的东西

你安装了microsoft jquery非结构化ajax吗?如果没有,试试看。我用你的代码和作品做了一些测试

编辑:我还为测试更改了一些代码

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Call(CallMe callMe)
    {
        if (ModelState.IsValid)
        {
            ModelState.Clear();
            callMe.CallMeMessage = callMe.CallMeMessage + " i was on the server";
        }
        return PartialView("_PartialFooter", callMe);
    }

@使用(Ajax.BeginForm(“Call”、“Home”、新的AjaxOptions{UpdateTargetId=“result”,InsertionMode=InsertionMode.Replace}))
{
@动作(“呼叫”、“主页”)
发送
}

因此,您可以看到更改。

如果重定向,则意味着
jquery.unobtrusive ajax.js
未正确加载。布局中有
@section Scripts{
没有任何意义(该部分包含jquery的第二个副本)。将
jquery.unobtrusive ajax.js
文件移动到
@Scripts.Render(“~/bundles/myscripts”)
(或者更好地使用
jqueryval
捆绑包,并删除
@section脚本){
是的,在nuget中,我已经安装了Microsofr.jQuery.Unobtrusive.Ajaxmicrosoft@tinaw25你在布局中添加脚本了吗?@tinaw25这是我的测试,希望能对你有所帮助。非常感谢!!!你是我真正的英雄:-)。现在可以了,我只是复制了你的一些代码,我很乐意帮忙!!;)
[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Call(CallMe callMe)
    {
        if (ModelState.IsValid)
        {
            ModelState.Clear();
            callMe.CallMeMessage = callMe.CallMeMessage + " i was on the server";
        }
        return PartialView("_PartialFooter", callMe);
    }
            @using (Ajax.BeginForm("Call", "Home", new AjaxOptions { UpdateTargetId = "result", InsertionMode = InsertionMode.Replace}))
        {
            <div id="result" class="margin-bottom-5">
                @Html.Action("Call", "Home")


            </div>
            <button class="btn btn-common-small margin-bottom-10 pull-right" type="submit">Send</button>
            }