【问题标题】:HTML.Partial() is not working when displaying it?HTML.Partial() 在显示时不起作用?
【发布时间】:2014-05-19 09:40:14
【问题描述】:

在 Asp.net mvc 5 中,我们有一个登录页面,它实现 HTML.BeginForm() 以将数据发布到控制器,如果用户名和密码不正确,我们将使用部分视图将错误发送回前端。一切正常,如果出现错误,屏幕上只会显示错误消息,没有其他内容。

控制器

[HttpPost]
public ActionResult SingleSSOMPLogin(SSoStateModel model)
{
    //Check the Code with SP providers and Authorization server.
      if(model.SAMLAssertion.validMPData)
      {
        return RedirectToAction("SSOServiceMP" , "SAML");
      }else
      { 
         //Error message processed.
         model.errorMessage = SSOState.SAMLAssertion.errorMessage;
         return PartialView("_LoginError" , model);
      }
}

视图包含以下代码

<div class="peripheral">
        <div class="panel panel-default panel-peripheral">
            @{Html.Partial("_LoginError", Model);}
            <div class="panel-heading clearfix">Please Log In</div>
            <div class="panel-body">
                <div class="brand-logo eop-logo">
                    <script type="text/javascript">
                        function changeImage() {
                            document.getElementById("Logo").src = '@Url.Content("~/Content/images/TopLogo.gif")';
                        }
                    </script>
                    <img id="Logo" width="200" src='@Url.Content("~/Content/images/TopLogo.gif")' onerror="changeImage()" />


                    @{ Html.EnableClientValidation(true);}

                    <!--Ajax call to Controller-->
                    @using (Html.BeginForm("SingleSSOMPLogin", "Accounts"))
                    {
                        @Html.ValidationSummary(true);

                        <div id="group-email" class="form-group col-md-12 ">
                            @Html.TextBoxFor(m => m.Email, new { @class = "form-control", placeholder = "Please enter your Email address" })
                            @Html.ValidationMessageFor(m => m.Email)
                        </div>

                        <div id="group-password" class="form-group col-md-12">
                            @Html.PasswordFor(m => m.Password, new { @class = "form-control", placeholder = "Please enter your password" })
                            @Html.ValidationMessageFor(m => m.Password)
                        </div>

                        <div class="form-group">
                            <div class="col-md-12">
                                <button type="submit" class="btn btn-primary pull-left">Login</button>
                                <a id="forgot" href='@Url.Action("ForgotPassword","Accounts")' class="btn btn-link btn-sm pull-right">Forgot your password?</a>
                            </div>
                        </div>
                    }
                </div>
            </div>
        </div>
    </div>

局部视图

@{
    Layout = null;
}

@if (Model.Result!= null)
{
    <div class="alert alert-warning alert-dismissable">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        <strong>@Model.Result.errorMessage</strong>
    </div>
}

发生错误时,我会再次重定向到同一个视图,所有查询参数都消失了,只显示错误消息。

如何解决以下问题?

【问题讨论】:

    标签: c# html asp.net asp.net-mvc-4 asp.net-mvc-partialview


    【解决方案1】:

    局部视图将只返回您定义的片段。 因此,当从“完整”视图调用时,

    @{Html.Partial("_LoginError", Model);}
    

    它将生成视图的相应部分。

    在您的情况下,最常见的是添加模型错误并返回完整视图(必须具有 ValidationSummary 部分):

    [HttpPost]
    public ActionResult SingleSSOMPLogin(SSoStateModel model)
    {
        //Check the Code with SP providers and Authorization server.
          if(model.SAMLAssertion.validMPData)
          {
            return RedirectToAction("SSOServiceMP" , "SAML");
          }else
          { 
             //Error message processed.
             ModelState.AddModelError("error", SSOState.SAMLAssertion.errorMessage);             
             return View(model);
          }
    }
    

    如果您想使用局部视图,您必须从 javacript ajax 调用中调用它并将响应插入到您的页面中。使用 jQuery,它或多或少类似于:

    $.ajax({ url: <partial view url>,
             type: 'GET',
             success: function (result) {
               $(updateSelector).hide().html(result).effect("fade", "fast");
             }
          });
    

    【讨论】:

    • 谢谢,但是在返回视图时。还会去掉查询参数,这些参数很重要吗?
    • 我不确定我是否正确理解了您的问题...当您返回 View(model) 时,视图将包含模型中的所有信息(来自 url 查询和您的表单数据)。你不会丢失任何东西。
    • 我的意思是:下一个收到的帖子将包含相同的模型信息(当然除了用户修改表单)
    猜你喜欢
    • 2013-03-19
    • 1970-01-01
    • 2019-01-09
    • 1970-01-01
    • 1970-01-01
    • 2012-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多