ASP.net MVC3 - Razor视图和PartialViews与Ajax提交

14

我一直没有成功让这个工作起来!

在一个视图中...

@model Project.Models.Account.ForgotPasswordModel

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

<h2>ForgotPassword</h2>

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

我呈现这个 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>   
}

这个控制器动作...

[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。没有其他东西。)我尝试了一些在网上找到的方法... 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更改为代码块,例如 @{ Html.RenderPartial("_ForgotPasswordUserNameAjax", ViewData.Model); }。
那行不通...
我已经没有任何想法(和耐心)!
请帮忙!
3个回答

8

编辑 PEBKAC。

我忘记在_Layout.cshtml页面中包含新的jquery.unobtrusive-ajax.js文件,这是我升级项目时添加的。加入该库后问题得到解决。对不起,大家!

原始帖子 我开始认为这是一个Bug。我又拿起未转换的MVC2项目并将其转换为MVC3。我保留了所有原始的aspx/ascx格式的页面并运行了该项目。我尝试了该页面,但仍然出现同样的问题。回到MVC2,一切正常。再试一次MVC3,问题又出现了。

我使用了与此非常相似的页面来转换项目...

http://mattsieker.com/index.php/2010/11/21/converting-asp-net-mvc2-project-to-mvc3/


1

由于您只返回了部分视图,因此只有该部分被处理。由于Razor视图的处理方式,MVC3更严格地遵守了这种功能。

只需将您的控制器操作更改为以下内容:

[HttpPost]
        public ActionResult 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 = View("ForgotPasswordAnswerAjax", model);
            }
            else
            {
                retVal = PartialView("_ForgotPasswordUserNameAjax", model);
            }

            return retVal;

        }

虽然这给了我更多的格式,但我完全错过了部分视图的父视图中的所有内容。当然,这不是很重要,但如果有其他页面有更多的内容,我被迫将其放在部分视图中?那么部分视图有什么用呢? - DavidAndroidDev

0

我认为“main”视图也被称为ForgotPassword,就像部分视图一样。

由于控制器仅返回PartialViewResult,因此不使用布局。

为父视图和ajax调用创建不同的操作。


嗯,我正在将这个项目从MVC2应用程序转换为MVC3,并将视图更改为Razor。在切换到MVC3之前,它们运行得非常完美。我本来期望不必更改控制器代码(除了使用下划线命名视图的新方式)。:( - DavidAndroidDev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接