Page not redirecting as intended

冷暖自知 提交于 2020-04-18 03:46:03

问题


I am trying to validate input data and if the validation is true, go to the next view. However, I can see my code is not existing the present page.

Controller:

public ActionResult ForgotLoginId()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult ForgotLoginId(Testing ForgotLIDAuthentication)
        {
            if (ModelState.IsValid)
            {
                using(SUPRTestingDBEntities2 db = new SUPRTestingDBEntities2())
                {
                    if (obj != null)
                    {
                        Session["LoginID"] = obj.EmailID.ToString();
                        Session["EmailID"] = obj.TaxID.ToString();
                        return RedirectToAction("LIDAuthentication");

                    }
                    else if (obj == null)
                    {
                        return View();
                    }
                }          
            }
            return View();
        }        

        public ActionResult LIDAuthentication()
        {
            if (Testing.IsUserValid())
            {
                return View();
            } 
            else
            {
                return RedirectToAction("ForgotLoginID");
            }
        }

View:

@using (Html.BeginForm("ForgotLoginID", "Corporation", FormMethod.Post))
{

    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    if (ViewBag.Message != null)
    {
    <div style="border: 1px solid red">
        @ViewBag.Message
    </div>
    }
<div>
<textarea name="EmailId" style="width:50%; border-color: black" required>@Html.TextAreaFor(a => a.EmailID)</textarea>
        <text>@Html.ValidationMessageFor(a => a.EmailID)</text>
<textarea name="TaxID" style="width:30%; border-color: black" required placeholder="xxxxxxxxx" maxlength="9">@Html.TextAreaFor(a => a.Password)</textarea>
        <text>@Html.ValidationMessageFor(a => a.Password)</text>
<button type="submit" style="width: 20%; color: white; background-color: deepskyblue; border-color:black" value="SubmitRequest" name="Submitbtn"><b>Submit</b></button>
</div>
}

Model:

public partial class Testing
    {
        public int LoginID { get; set; }
        [Required]
        public string EmailID { get; set; }
        [Required]
        public int TaxID { get; set; }

        public string Password { get; set; }
        [Required]
        public string CorporationName { get; set; }

        public static bool IsUserValid()
        {
            HttpContext context = HttpContext.Current;
            if (context.Session["LoginID"] != null)
            {
                return true;
            }
            else
                return false;
        }
    }

Here, the page not exiting the current view. When I tried to insert breakpoint after [httppost] in Controller, it is not even hitting the code.

Also, I am not sure why my view has something in fields, I don't know what it is. Not sure if this is affecting my output.

ForgotLoginID View

Please help me with this.


回答1:


I'm not sure if this will fix your problem, but your view code should look something like:

@using (Html.BeginForm("ForgotLoginID", "Corporation", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    if (ViewBag.Message != null)
    {
        <div style="border: 1px solid red">
            @ViewBag.Message
        </div>
    }

    @Html.TextAreaFor(a => a.EmailID)
    @Html.ValidationMessageFor(a => a.EmailID)
    @Html.TextAreaFor(a => a.Password)
    @Html.ValidationMessageFor(a => a.Password)
    <button type="submit" style="width: 20%; color: white; background-color: deepskyblue; border-color:black" value="SubmitRequest" name="Submitbtn"><b>Submit</b></button>
}


来源:https://stackoverflow.com/questions/60830483/page-not-redirecting-as-intended

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!