1

I'm trying my hand and Asp.net MVC 4 and having an issue with an Ajax form returning a whole new page instead of just updating a div.

Here's the razor html code:

<div class="All">
    <div class="Search">
        @using (Ajax.BeginForm( new AjaxOptions { UpdateTargetId = "CurrentSku" } ))
        {
        @Html.Label("Enter Sku:")
        @Html.TextBox("textBox1")
        <input type="submit" value="Find" />
        }
    </div>
    <div id="CurrentSku">
        <span>No Sku selected.</span>
    </div>

and here's the controller:

    public ActionResult Index()
    {
        // Pay no attention to this, just a place holder
        return View( db.xInventoryExt.Take(1) );
    }

    [HttpPost]
    public ActionResult Index(string textBox1)
    {
        if (db.xInventoryExt.Count(a => a.InvtID == textBox1) < 1)
        {
            return Content("Sku not found.", "text/html");
        }

        var ret = db.xInventoryExt.First(b => b.InvtID == textBox1);

        return Content(ret.ToString(), "text/html");
    }

I was reading that this sometimes happens when not including MicrosoftAjax.debug.js but I can't find a copy of that file anywhere.

4

1 回答 1

2

我在读到不包括 MicrosoftAjax.debug.js 时有时会发生这种情况

实际上,您需要包含jquery.unobtrusive-ajax.js脚本。MicrosoftAjax.debug.js是旧版本 ASP.NET MVC 的一部分,现在完全过时了。~/App_Start/BundleConfig.cs因此,基本上,如果您使用 ASP.NET MVC 4 并使用默认捆绑包_Layout.cshtml

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
@RenderSection("scripts", required: false)

现在你的 Ajax.* 助手可以工作了,你也可以启用不显眼的 jQuery 验证。这将由于此~/bundles/jqueryval捆绑包的定义方式而发生:

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

如果您不使用捆绑包,则只需按顺序包含相应的脚本:

<script type="text/javascript" src="~/scripts/jquery-1.8.2.js"></script>
<script type="text/javascript" src="~/scripts/jquery.unobtrusive-ajax.js"></script>
@RenderSection("scripts", required: false)
于 2013-05-14T15:37:26.837 回答