如何从partialview重定向到某个操作,以便在重定向后仍然保留partialview结果 [英] how to redirect to an action from partialview such that the partialview results still remain after redirecting

查看:80
本文介绍了如何从partialview重定向到某个操作,以便在重定向后仍然保留partialview结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。我是MVC的新手,也希望能解释我的问题。

我的所有页面都有一个登录框(在_Login.cshtml文件中),直接通过_layout.cshtml调用文件在@Html.Partial(_ Login,model)表单中。在我的Home控制器的索引页面中,我从数据库中检索所有注释并在索引视图中列出它们。在浏览器中,一切顺利。列出所有评论,登录框出现在页面左侧。但是当我输入错误的用户名或密码时,错误消息将从_Login操作呈现,但所有注释都会消失。显然这是因为索引页面在错误消息被触发后没有加载。请指导我如何做到这一点,我有两条评论和被解雇的错误信息。

感谢任何帮助。这些是我的代码。

//_Layout.cshtml

Hi all. I'm very new in MVC and hope to explain my problem as well.
I have a Login box(in _Login.cshtml file) in all of my pages which is called directly through the _layout.cshtml file in the @Html.Partial("_Login",model) form. In index page of my Home controller, I retrieve all comments from database and list them in index view. In browser at first everything goes well. All comments are listed and loginbox is appeared in left side of page. But when i enter incorrect Username or Password,the error message is rendered from _Login action,but all comments are disappeared. Obviously this is because the index page doesn't load after error message fired.Please guide me how can do this such that i have both comments and fired error message.
Appreciate any help.These are my codes.
//_Layout.cshtml

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="refresh" />
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
</head>
<body>
    <div id="loginbox" >       
   @Html.Partial("_Login",new CSS_Test.ViewModels.CommentViewModel())                     
    </div>
    <div id="first_head">
        <span class="times"> @{Html.RenderAction("TimeSummary","Home");}</span>
    </div>
</body>



//共享文件夹中的_Login.cshtml


//_Login.cshtml in Shared folder

@model CSS_Test.ViewModels.CommentViewModel       
@using (Html.BeginForm("_LogIn", "Account",FormMethod.Post,new{id="login", @class="enter_form"} ))
    {

        if (@Session["UserName"]==null)
        {
        @Html.TextBoxFor(m =>this.Model.LogModel.UserName, new { @class="UserName", placeholder = "Username" ,autofocus="autofocus",@Autocomplete= "off"})    
       <br />
        @Html.ValidationMessageFor(m => this.Model.LogModel.UserName)    
        <BR />
        @Html.PasswordFor(m => this.Model.LogModel.Password, new { @class = "Password", placeholder = "Password" })
        <br/>       
            @Html.ValidationMessageFor(m => this.Model.LogModel.Password)       
        <BR />    
        <div style="color:#f00;font-weight:bold" id="kkk">
            @if (ViewData.ModelState["Error"]!=null) {               
                   @Html.ValidationMessage("Error")           
               }
          </div>
       }
        if (@Session["UserName"] != null)
        {   
     <div style="color:#4cff00;font-weight:bold;direction:rtl">
      @Session["UserName"] welcome <br />
         <div>@Html.ActionLink("Logout","LogOff","Account")</div>
         </div>
            if (Request.UrlReferrer != null) { 
                if (@Request.Url.AbsoluteUri.ToString().Contains("Account"))            
            {
         <div style="padding-removed7px; ">
         <a href="@Url.Action("Put_Comment", "admin", new { UserName = @Session["UserName"] })">Administrator Part</a>
             </div>
       }
       }
        }
       if (@Session["UserName"] == null)
        {
        <br />
        @Html.CheckBoxFor(m => this.Model.LogModel.RememberMe)
        @Html.LabelFor(m => this.Model.LogModel.RememberMe)
        <BR />
        <input type="submit"  value="Submit" class="btn btn-primary" />
            
       }
    }
</div>



// _帐户管理员中的登录操作


//_Login Action in Account Controller

[HttpPost]
public ActionResult _LogIn(CSS_Test.ViewModels.CommentViewModel model, string returnUrl)
{
    if (ModelState.IsValid)
    {
        if (Membership.ValidateUser(model.LogModel.UserName, model.LogModel.Password))
        {

            FormsAuthentication.SetAuthCookie(model.LogModel.UserName, model.LogModel.RememberMe);
            if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
            {
                return Redirect(returnUrl);
            }

            @Session["UserName"] = model.LogModel.UserName;

        }

        ViewData.ModelState.AddModelError("Error", "Incorrect Username Or Password");
                  }

    return View(model);
}



,最后是索引行动


and finally index action

<pre lang="c#">
 public ActionResult Index()
        {               
            MyContext mydb =new MyContext("MyContext");
                var All_Comments = mydb.Comments.ToList<Comment>();

                CommentViewModel mymodel = new CommentViewModel()
                {
                    all_comments=All_Comments
                };
            return View(mymodel);           
                }

推荐答案

谢谢所有朋友提出很多意见来帮助我!!!!

但我发现我需要使用Jquery脚本使用
Thank you all friends to put many comments for help me!!!!
But i founded that i need to use Jquery script to submit the form using


.ajax函数将表单提交给_Login action.Here是我的更改代码。

/ /_Layout.cshtml

.ajax function to _Login action.Here is my change codes.
//_Layout.cshtml
@model CSS_Test.ViewModels.CommentViewModel       
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="../../Scripts/jquery-1.7.1.min.js"></script>
<script>


(document).ready(function(){
(document).ready(function () {


这篇关于如何从partialview重定向到某个操作,以便在重定向后仍然保留partialview结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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