【问题标题】:Page not redirecting as intended页面未按预期重定向
【发布时间】:2020-07-04 21:57:51
【问题描述】:

我正在尝试验证输入数据,如果验证为真,请转到下一个视图。但是,我可以看到我的代码在当前页面不存在。

控制器:

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");
            }
        }

查看:

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

型号:

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

这里,页面没有退出当前视图。当我尝试在Controller中的[httppost]之后插入断点时,它甚至没有命中代码。

另外,我不确定为什么我的视图在字段中有一些东西,我不知道它是什么。不确定这是否会影响我的输出。

ForgotLoginID View

请帮帮我。

【问题讨论】:

  • 中有一个 很奇怪。此外,您没有显示整个
    ,特别是提交 button.input。
  • @JasperKent 我已将按钮添加到视图中。
  • 另外,您似乎正在生产双
  • 您还有其他问题,因为您没有提供 [必填] 字段 CorporationName 和 TaxId,但这仅意味着您的 ModelState.IsValid 为 false。我假设您的控制器正确命名为 CorporationController。
  • 是的,我可以看到我的 ModelState.IsValid 为 false,不允许它进入初始 if 语句本身。

标签: c# html asp.net asp.net-mvc model-view-controller


【解决方案1】:

我不确定这是否能解决您的问题,但您的视图代码应该类似于:

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

【讨论】:

  • 系统验证失败返回同一页面时如何添加此消息?
  • 这似乎是一个单独的问题,所以请将它作为一个新问题发布。
猜你喜欢
  • 1970-01-01
  • 2013-03-06
  • 1970-01-01
  • 2012-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-15
  • 2015-04-03
相关资源
最近更新 更多