在MVC4中提交后重定向到登录页面

时间:2013-11-19 10:27:28

标签: asp.net-mvc-4 redirect login simplemembership

在提交此视图的操作后 - 浏览器重定向到登录页面!!

即使是经过身份验证的用户

也是如此

(mvc4-vs2012-simplemembership)

查看:

....
@using (Html.BeginForm("Create", "Comment"))
{
    <fieldset> 
        <div class="container">
            <div class="row-fluid">
                <div class="span8">
                    @if (WebSecurity.IsAuthenticated)
                    {
                        <div class="iran text-success">
                            <i class="icon-user"></i>
                            username:<span>@WebSecurity.CurrentUserName</span>
                        </div>
                        <div class="">
                            <input type="hidden" name="ArticleId" />
                        </div>
                        <div class="controls">
                            @Html.TextArea("Description", new { @Class = "span6", @Rows = 4 })
                        </div>
                        <div class="controls">
                            <span class="span4">
                                <p>
                                    <input type="submit" class="btn btn-primary" value="submit comment" />
                                </p>
                            </span>
                        </div>
                    }
                    else
                    {
                        <div class="iran">
                            @Html.ActionLink("you must first log in", "LoginUser", "Account")
                        </div>
                    }
....

控制器:

[HttpPost]
public ActionResult Create(FormCollection form)
{
    try
    {    
        var ins = new Comment();
            ins.Description = form["Description"];
            ins.ArticleId = Convert.ToInt16(form["ArticleId"]);
            ins.DateSend = Shamsi();
            ins.TimeSend = DateTime.Now.ToString("HH:mm:ss");
            ins.UserId = WebSecurity.CurrentUserId;
            bank.Comments.InsertOnSubmit(ins);
            bank.SubmitChanges();

        return RedirectToAction("Index","Home");
    }
    catch { return null;}
}

1 个答案:

答案 0 :(得分:0)

您的代码中有错误。隐藏字段“ArticleId”无法转换为int,因为它为null。此错误消息正在try-catch块中被吞没,并且您没有观察和/或被通知它。修复此问题后,您可能还会遇到其他问题。我建议在调试时删除try-catch。我还建议检查“ModelState”以确保它有效,例如在保存之前确保输入所有字段。