MVC4 剑道项目 Ajax.BeginForm UpdateTargetId 问题 [英] MVC4 Kendo Project Ajax.BeginForm UpdateTargetId Issue

查看:22
本文介绍了MVC4 剑道项目 Ajax.BeginForm UpdateTargetId 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚使用现有 MVC3 项目中的一些代码在 MVC4 中启动了一个新项目.我可以强制我的表单 ajax 重新加载特定的 DIV,但不使用正常的提交方法,只能使用测试 doSomthing() javascript 函数.我错过了什么?

澄清一下:第一个按钮不能正常工作,第二个可以 - 但我不想那样做.

查看

@using (Ajax.BeginForm("Method1", null,new AjaxOptions { HttpMethod = "post", UpdateTargetId = "divPartial1" },新 { id = "form1" })){<div class="数据">@Html.LabelFor(x => x.TotalSubmitted, new { @class = "total" })

@Html.FormatValue(Model.TotalSubmitted, "{0:n0}")

...

<div class="详细信息"><div id="divPartial1">@Html.Partial("ReportDashboardAppPartial")

<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 ki-search"/></按钮>

}<script type="text/javascript">函数 doSomething() {$.ajax({url: '@Url.Action("Method1", "Controller")',类型:'帖子',数据:$('form#form1').serialize(),成功:功能(结果){$('#divPartial1').html(result);}});}

_布局

@model BaseViewModel<!DOCTYPE html><html lang="zh-cn"><头><meta charset="utf-8"/><title>@ViewBag.Title</title><link href="~/favicon.ico" rel="快捷图标" 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("样式", 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("脚本", false)<身体>@Html.Partial("_AlertWindow")<div id="包装器"><标题><div id="登录显示">@Html.Partial("_LoginPartial")

<a href="@Url.Action("Index", "Home")"><div id="logo"></div></a><div id="标题"><h1>哈!</h1>

@(Html.Kendo().Menu().Name("Menu").BindTo("Main").SecurityTrimming(true))</标题><div id="main">@RenderBody()

<页脚><div id="version">@Html.ActionLink("Version " + @Model.CurrentVersion, "About", "Home")

</页脚>

@RenderSection("end_scripts", false)

我知道这应该可行.

解决方案

我遇到了同样的问题.解决方法是在_Layout.cshtml页面中添加一条语句.

@Scripts.Render("~/bundles/jqueryval")

ScriptBundle("~/bundles/jqueryval")的定义如下

bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include("~/Scripts/jquery.unobtrusive*","~/Scripts/jquery.validate*"));

I have just started a new project in MVC4 using some code from an existing MVC3 project. I can force my form to ajax reload the specific DIV, but not using the normal submit method, only with the test doSomthing() javascript function. What am I missing?

To clarify: The first button does not work right, the second one does - but I don't want to do it that way.

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>

I know this should work.

解决方案

I had the same problem. The solution is to add a statement in _Layout.cshtml page.

@Scripts.Render("~/bundles/jqueryval")

The definition of ScriptBundle("~/bundles/jqueryval") as below

bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
            "~/Scripts/jquery.unobtrusive*",
            "~/Scripts/jquery.validate*"));

这篇关于MVC4 剑道项目 Ajax.BeginForm UpdateTargetId 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆