我在MVC 5中有一個基本的Ajax調用設置,但似乎我的Ajax表單實際上發佈完整的表單,而不是在主視圖中返回PartialViewResult,整個窗口只是呈現與部分視圖的結果MVC 5 Ajax發佈整個表單 - 而不是ajax調用
建議我可能會在這裏失蹤?
我也有以下的jQuery在我_Layout.cshtml呈現
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
的MainView
@{
ViewBag.Title = "Puzzles 1 & 2";
}
<h2>@ViewBag.Title</h2>
<div>
@Html.Partial("Puzzle1Form")
</div>
PartialView
@model SixPivot_Code_Puzzles.Models.Puzzle1Model
@using (Ajax.BeginForm("Puzzle1","Puzzles",new AjaxOptions {
InsertionMode = InsertionMode.Replace,
HttpMethod = "POST",
UpdateTargetId = "puzzle1-result",
}))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Puzzle1</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.IntegerList, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.IntegerList, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.IntegerList, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Find Largest No." class="btn btn-default" />
</div>
</div>
</div>
}
<div id="puzzle1-result"></div>
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
}
Ç ontroller
[HttpPost]
public PartialViewResult Puzzle1(Puzzle1Model model)
{
if (ModelState.IsValid)
{
Puzzle1Model result = new Puzzle1Model();
result.LargestInteger = FindLargestInt(model.IntegerList).ToString();
result.IntegerList = model.IntegerList;
return PartialView("Puzzle1FormResult",result);
}
else {
return PartialView("Puzzle1Form",model);
}
}
PartialViewResult上的成功(Puzzle1FormResult.cshtml)
@model SixPivot_Code_Puzzles.Models.Puzzle1Model
<div>
<h4>Largest Integer</h4>
<hr />
<p>
Largest Integer for the list "@Model.IntegerList" is : @Model.LargestInteger
</p>
</div>
謝謝!我發現由於某種原因,當我將jquery.unobstrusive-ajax.min.js移至_layout時,助手開始工作 - 可能從我的一個子視圖文件夾路徑不正確 – Danish