ASP.net MVC3 - 剃刀视图和PartialViews使用Ajax回发 [英] ASP.net MVC3 - Razor Views and PartialViews with Ajax Postbacks

查看:96
本文介绍了ASP.net MVC3 - 剃刀视图和PartialViews使用Ajax回发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在得到这个工作非常成功!

I have been very unsuccessful in getting this to work!

在一个视图...

@model Project.Models.Account.ForgotPasswordModel

@{
    ViewBag.Title = "Forgot Password";
}

<h2>ForgotPassword</h2>

<span id='@ViewBag.ReplaceID'>
    @Html.Partial("_ForgotPasswordUserNameAjax", ViewData.Model)
</span>

我使这个partialView ...

I render this partialView...

@model Project.Models.Account.ForgotPasswordModel

@{
    this.Layout = null;
}

@using (Ajax.BeginForm("ForgotPassword", new AjaxOptions() { UpdateTargetId = ViewBag.ReplaceID, InsertionMode = InsertionMode.InsertAfter }))
{
    @Html.ValidationSummary(true, "Forgot Password was unsuccessful. Please correct the errors and try again.")
    <div id="login" class="box">
            <fieldset>
            <h2>Account Information</h2>
            <div class="inside">
                <div class="editor-label">
                    @Html.LabelFor(m => m.Username)
                </div>
                <div class="editor-field">
                    @Html.TextBoxFor(m => m.Username)
                    <br />
                    @Html.ValidationMessageFor(m => m.Username)
                    <br />
                </div>

                <p>
                    <input type="submit" value='Submit' />
                </p>
            </div>
        </fieldset>
    </div>   
}

和这个控制器动作...

And this controller action...

[HttpPost]
        public PartialViewResult ForgotPassword(ForgotPasswordModel model)
        {

            if (String.IsNullOrEmpty(model.Username))
            {
                ModelState.AddModelError("Username", ForgotPasswordStrings.USER_NAME_REQUIRED);
            }
            else
            {
                bool isGood = false;
                model.Question = this._security.ValidateUserNameGetSecurityQuestion(model.Username, out isGood);

                if (!isGood)
                {
                    ModelState.AddModelError("Username", ForgotPasswordStrings.USER_NAME_INVALID);
                }

            }
            PartialViewResult retVal = null;
            if (ModelState.IsValid)
            {

                retVal = PartialView("ForgotPasswordAnswerAjax", model);
            }
            else
            {
                retVal = PartialView("_ForgotPasswordUserNameAjax", model);
            }

            return retVal;

        }

然而,每一次,视图只返回PartialView,不包含在布局中。(所以只是我PartialView是在屏幕上。仅此而已。)我已经尝试了一些东西,我在网上找到的.. 。
<一href=\"http://www.compiledthoughts.com/2011/01/aspnet-mvc-razor-partial-views-with.html\">http://www.compiledthoughts.com/2011/01/aspnet-mvc-razor-partial-views-with.html
http://stackoverflow.com/questions/4655365/mvc3-submit-ajax-form

Yet, every single time, the view only returns the PartialView, not contained in the layout.(So just my PartialView is on the screen. Nothing else.) I've tried a few things I've found online... http://www.compiledthoughts.com/2011/01/aspnet-mvc-razor-partial-views-with.html http://stackoverflow.com/questions/4655365/mvc3-submit-ajax-form

但没有什么已经修复了这个问题。我已经改变了InsertionMode将所有值没有变化。我已经改变了@ Html.Partial像一个code座
@ {
Html.RenderPartial(_ ForgotPasswordUserNameAjax,ViewData.Model);
}

But nothing has fixed this issue. I've changed the InsertionMode to all values with no change. I've changed the @Html.Partial to a code block like @{ Html.RenderPartial("_ForgotPasswordUserNameAjax", ViewData.Model); }.

这不工作...

我运行的想法(和耐心)!

I'm running out of ideas (and patience)!

请帮帮忙!

推荐答案

修改
PEBKAC。

EDIT PEBKAC.

我忘了,当我升级项目,我添加了新的jquery.unobtrusive-ajax.js文件,但从未纳入他们_Layout.cshtml页面上。新增固定的问题该库。对不起你们!

I forgot when I upgraded the project, I added the new jquery.unobtrusive-ajax.js files, but never included them on the _Layout.cshtml page. Added that library in fixed the issue. Sorry guys!

原贴
我开始认为这是一个错误。以未转化的项目再次(MVC2)并将其转换为MVC3。我留在ASPX / ASCX格式的所有原始网页和运行项目。我试过页。仍然出现同样的问题。让我们回到MVC2,并能正常工作。试图MVC3一个更多的时间,这个问题再次发生。

Original Post I beginning to think this is a bug. Taking the unconverted project again (MVC2) and converting it to MVC3. I left all the original pages in the aspx/ascx format and ran the project. I tried the page. Same issue still occurs. Going back to MVC2, and it works fine. Tried MVC3 one more time, and the issue happens again.

我使用页面非常相似,这个转换的项目...

I converted the project using a page very similar to this...

<一个href=\"http://mattsieker.com/index.php/2010/11/21/converting-asp-net-mvc2-project-to-mvc3/\">http://mattsieker.com/index.php/2010/11/21/converting-asp-net-mvc2-project-to-mvc3/

这篇关于ASP.net MVC3 - 剃刀视图和PartialViews使用Ajax回发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆