简体   繁体   中英

Redirect To Login Page After Submit In MVC4

after submit action for this view- browser redirect to login page!!

even for authenticated user

(mvc4-vs2012-simplemembership)

View:

....
@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>
                    }
....

Controller:

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

You have an error in your code. Hidden field "ArticleId" cannot be cast to int because it is null. This error message is being swallowed up in the try-catch block and you are not observing and/or being notified of it. Once this is fixed you still may have additional problems. I would recommend to remove the try-catch while debugging. I would also recommend to check the "ModelState" to make sure it is valid, such as making sure all fields are entered, prior to saving.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM