C# 页面未按预期重定向
我正在尝试验证输入数据,如果验证为真,请转到下一个视图。但是,我可以看到我的代码在当前页面中不存在 控制器:C# 页面未按预期重定向,c#,html,asp.net,asp.net-mvc,model-view-controller,C#,Html,Asp.net,Asp.net Mvc,Model View Controller,我正在尝试验证输入数据,如果验证为真,请转到下一个视图。但是,我可以看到我的代码在当前页面中不存在 控制器: public ActionResult ForgotLoginId() { return View(); } [HttpPost] [ValidateAntiForgeryToken] public ActionResult ForgotLoginId(Testing ForgotL
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");
}
}
视图:
在这里,页面不会退出当前视图。当我试图在控制器中的[httppost]之后插入断点时,它甚至没有命中代码
另外,我不知道为什么我的观点会有一些领域的东西,我不知道它是什么。不确定这是否影响了我的输出
请帮我解决这个问题。我不确定这是否能解决您的问题,但您的视图代码应该如下所示:
@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>
}
@使用(Html.BeginForm(“ForgotLoginID”,“Corporation”,FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
如果(ViewBag.Message!=null)
{
@查看包。留言
}
@Html.TextAreaFor(a=>a.EmailID)
@Html.ValidationMessageFor(a=>a.EmailID)
@Html.TextAreaFor(a=>a.Password)
@Html.ValidationMessageFor(a=>a.Password)
提交
}
奇怪的是,你的大脑里有一个。此外,您没有显示整个视图,而不是“提交”按钮。输入。@JasperKent我已将该按钮添加到视图中。此外,您似乎正在生成双s。Html.TextAreaFor将在生成的Html中生成一个,因此您不需要将其包装在自己的Html中。您还有更多的问题,因为您没有提供[Required]字段CorporationName和TaxId,但这只意味着您的ModelState.IsValid为false。我假定您的控制器正确命名为CorporationController。是的,我可以看到我的ModelState.IsValid为false,不允许它输入初始if语句本身。当系统验证失败并返回到同一页面时,如何添加此消息?电子邮件地址或登录ID不匹配。请验证并再次输入详细信息。这看起来像一个单独的问题,因此请将其作为新问题发布。
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;
}
}
@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>
}