我有这个问题,搜索了很多,但现在答案正确。
我的页面上有一个联系表单,在我的_Layout页面上,但是当我点击按钮时,部分视图在新页面中打开。
我记得要包含jquery.unobtrusive-ajax.js。这就是我所拥有的。
控制器:
[HttpGet]
public ActionResult Call()
{
return PartialView("_PartialFooter");
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Call(CallMe callMe)
{
if(ModelState.IsValid)
{
}
return PartialView("_PartialFooter");
}
_Layout脚本位于底部的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>
}
_PartialFooter(局部视图)
@model servicemadsen.Models.CallMe
@Html.AntiForgeryToken()
<div class="row">
<div id="result" class="margin-bottom-5">
<div class="col-md-6">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control", @placeholder = "Navn" } })
</div>
<div class="col-md-6">
@Html.EditorFor(model => model.Phone, new { htmlAttributes = new { @class = "form-control", @placeholder = "Telefon" } })
</div>
<div class="col-md-12">
@Html.TextAreaFor(model => model.CallMeMessage, new { @class = "form-control", @placeholder = "Besked", @cols = 80, @rows = 7 })
</div>
<div class="col-md-12">
@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" })
</div>
</div>
</div>
希望有人可以提供帮助,这可能是我需要的一些虚拟事物
答案 0 :(得分:2)
你安装了微软jquery unobstrusive 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);
}
和
@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>
}
所以你可以看到变化。