asp.net-mvc ASP.net MVC3 - 带有 Ajax 回传的 Razor 视图和 PartialViews

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4888521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:50:35  来源:igfitidea点击:

ASP.net MVC3 - Razor Views and PartialViews with Ajax Postbacks

asp.net-mvcasp.net-mvc-3razor

提问by DavidAndroidDev

I have been very unsuccessful in getting this to work!

我一直很失败让这个工作!

In a view...

在一个观点...

@model Project.Models.Account.ForgotPasswordModel

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

<h2>ForgotPassword</h2>

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

I render this partialView...

我渲染这个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;

        }

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.htmlhttp://stackoverflow.com/questions/4655365/mvc3-submit-ajax-form

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

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); }.

但没有什么能解决这个问题。我已将 InsertionMode 更改为所有值而没有更改。我已将 @Html.Partial 更改为类似 @{ Html.RenderPartial("_ForgotPasswordUserNameAjax", ViewData.Model); 的代码块。}.

That doesn't work...

那行不通...

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

我的想法(和耐心)快用完了!

Please help!

请帮忙!

采纳答案by DavidAndroidDev

EDITPEBKAC.

编辑佩布卡克。

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!

忘记升级项目时,我添加了新的jquery.unobtrusive-ajax.js 文件,但从未将它们包含在_Layout.cshtml 页面中。在修复问题中添加了该库。对不起大家!

Original PostI 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.

原帖我开始认为这是一个错误。再次获取未转换的项目 (MVC2) 并将其转换为 MVC3。我将所有原始页面保留为 aspx/ascx 格式并运行该项目。我尝试了页面。同样的问题仍然出现。回到MVC2,它工作正常。又试了一次MVC3,问题又出现了。

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

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

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/

回答by Clicktricity

Since you are returning only a partial view, that is all that gets processed. This functionality is more strictly adhered to in MVC3 because of the way that Razor views are processed.

由于您仅返回部分视图,因此处理完毕。由于 Razor 视图的处理方式,此功能在 MVC3 中得到了更严格的遵守。

Simply change your controller action to the following:

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

[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;

        }

回答by GvS

I think the "main" view is also called ForgotPassword, just as the partial views.

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

Since the controller returns only a PartialViewResult, no layout is used.

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

Create a different action for the parent view and the ajax calls.

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