4
我剛剛在MVC4中使用現有MVC3項目中的一些代碼啓動了一個新項目。我可以強制我的表單重新加載特定的DIV,但不使用正常的提交方法,只用測試doSomthing()javascript函數。我錯過了什麼?MVC4 Kendo項目Ajax.BeginForm UpdateTargetId問題
澄清:第一個按鈕不能正常工作,第二個按鈕不行 - 但我不想這樣做。
VIEW
@using (Ajax.BeginForm("Method1", null,
new AjaxOptions { HttpMethod = "post", UpdateTargetId = "divPartial1" },
new { id = "form1" }))
{
<div class="data">
@Html.LabelFor(x => x.TotalSubmitted, new { @class = "total" })<div class="number total">@Html.FormatValue(Model.TotalSubmitted, "{0:n0}")</div>
...
</div>
<div class="details">
<div id="divPartial1">
@Html.Partial("ReportDashboardAppPartial")
</div>
</div>
<div style="text-align: center;">
<button type="submit" class="k-button"><span class="k-icon k-i-search" /></button>
<button type="button" name="Save" value="Save" onclick="doSomething(); return false;"><span class="k-icon k-i-search" /></button>
</div>
}
<script type="text/javascript">
function doSomething() {
$.ajax({
url: '@Url.Action("Method1", "Controller")',
type: 'post',
data: $('form#form1').serialize(),
success: function (result) {
$('#divPartial1').html(result);
}
});
}
</script>
_layout
@model BaseViewModel
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title</title>
<link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<meta name="viewport" content="width=device-width" />
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/jquery")
<link href="@Url.Content("~/Content/kendo.compatibility.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/kendo/2012.3.1114/kendo.common.min.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/kendo/2012.3.1114/kendo.dataviz.min.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/kendo/2012.3.1114/kendo.default.min.css")" rel="stylesheet" type="text/css" />
@RenderSection("styles", false)
<script src="@Url.Content("~/Scripts/kendo/2012.3.1114/jquery.min.js")"></script>
<script src="@Url.Content("~/Scripts/kendo/2012.3.1114/kendo.all.min.js")"></script>
<script src="@Url.Content("~/Scripts/kendo/2012.3.1114/kendo.aspnetmvc.min.js")"></script>
@RenderSection("scripts", false)
</head>
<body>
@Html.Partial("_AlertWindow")
<div id="wrapper">
<header>
<div id="logindisplay">
@Html.Partial("_LoginPartial")
</div>
<a href="@Url.Action("Index", "Home")">
<div id="logo"></div>
</a>
<div id="title">
<h1>Ha!</h1>
</div>
@(Html.Kendo().Menu().Name("Menu").BindTo("Main").SecurityTrimming(true))
</header>
<div id="main">
@RenderBody()
</div>
<footer>
<div id="version">@Html.ActionLink("Version " + @Model.CurrentVersion, "About", "Home")</div>
</footer>
</div>
@RenderSection("end_scripts", false)
</body>
</html>
我知道這應該工作。
阿定義!夥計,你搖滾! –
注意:我必須將此添加到_layout.cshtml的末尾 - 在RenderBody()之後。 將我的頭撞到代碼太久後挽救了我。謝謝! – Buffalo