提交表单后在 ASP.NET MVC 中。它没有重定向到其他页面。它加载相同的页面
Posted
技术标签:
【中文标题】提交表单后在 ASP.NET MVC 中。它没有重定向到其他页面。它加载相同的页面【英文标题】:In ASP.NET MVC after submitting form. it is not redirecting to other page. its loading same page 【发布时间】:2020-04-15 12:16:53 【问题描述】:1.在ASP.NET MVC中注册成功后, 在登录时输入用户名和密码等字段并单击提交按钮后。 2.它没有登录也没有重定向到另一个页面,但它实际上看起来像刷新一个页面,并且在单击提交 btn 后用户名输入的数据存在。 我的控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using WebApplication6mvc16_12.Models;
namespace WebApplication6mvc16_12.Controllers
public class UserController : Controller
[HttpGet]
public ActionResult AddorEdit(int id = 0)//(int id = 0)
User userModel = new User();
return View(userModel);
[HttpPost]
public ActionResult AddorEdit(User userModel)
// using (UserRegistrationDatabase1Entities dbModel = new UserRegistrationDatabase1Entities () )
using (Database1Entitiesmodel2 db = new Database1Entitiesmodel2())
//System.InvalidOperationException: 'The entity type User is not part of the model for the current context.
//check the AplicationdbContext to be Assign to Var Model that your create
db.Users.Add(userModel);
db.SaveChanges();
ModelState.Clear();
ViewBag.SuccessMessage = "Registration Successful";
return View("AddorEdit", new User());
/// <summary>
/// login data is not getting accepted. on the web server. data access is possible but not redirecting to other page
/// </summary>
/// <returns></returns>
[HttpGet]
public ActionResult Login() //(string UserId)
return View();
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login(User objUser)
if (ModelState.IsValid)
// using (Database1Entitiesmodel2 entitiesmodel2 = new Database1Entitiesmodel2())
using (Database1Entitiesmodel2 db = new Database1Entitiesmodel2())
// var obj = entitiesmodel2.Users.Where(model => model.UserName.Equals(objUser.UserName) && model.Password.Equals(objUser.Password)).FirstOrDefault();
var var = db.Users.Where(model => model.UserName.Equals(objUser.UserName) && model.Password.Equals(objUser.Password)).FirstOrDefault();
if (var != null)
// Session["UserId"] = var.UserId.ToString();
// Session["UserName"] = var.UserName.ToString();
return RedirectToAction("UserDashboard", "UserController");
return View(objUser);
public ActionResult UserDashboard()
// if (Session["UserId"] != null)
return View();
/*
else
return RedirectToAction("Login");*/
我的登录
@model WebApplication6mvc16_12.Models.User
@
ViewBag.Title = "Login";
<h2>Login</h2>
@using (html.BeginForm("Login","User",FormMethod.Post))//(Html.BeginForm())
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div class="form-horizontal">
@*<select class="form-control" id="LoginAuthority" name="LoginAuthority" required title="here">
<option disabled selected>Select Role</option>
<option>Admin</option>
<option>Customer</option>
<option>Sevice Engineer</option>
</select>
*@
<hr />
@Html.ValidationSummary(true, "", new @class = "text-danger" )
@Html.HiddenFor(model => model.UserId)
<div class="form-group">
@Html.LabelFor(model => model.UserName, htmlAttributes: new @class = "control-label col-md-2" )
<div class="col-md-10">
@Html.EditorFor(model => model.UserName, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.UserName, "", new @class = "text-danger" )
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Password, htmlAttributes: new @class = "control-label col-md-2" )
<div class="col-md-10">
@Html.EditorFor(model => model.Password, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.Password, "", new @class = "text-danger" )
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Login" class="btn btn-default" />
@* <input type="reset" value="Reset" class="btn btn-default" />*@
</div>
</div>
</div>
<div>
@Html.ActionLink("Back to Registration", "AddorEdit")
</div>
@section Scripts
@Scripts.Render("~/bundles/jqueryval")
我的用户
namespace WebApplication6mvc16_12.Models
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
public partial class User
public int UserId get; set;
[Required ( ErrorMessage ="This field is required")]
[DisplayName("Full Name")]
public string UserName get; set;
[Required (ErrorMessage = "This field is required")]
[DisplayName("Email Address")]
public string Email get; set;
[Required (ErrorMessage =" This field is required")]
[DataType(DataType.Password)]
public string Password get; set;
[Required (ErrorMessage =" This field is required")]
[DisplayName("Confirm Password")]
[DataType(DataType.Password)]
[Compare("Password")]
public string ConfirmPassword get; set;
用户仪表板
@
ViewBag.Title = "UserDashboard";
<fieldset>
<legend>
Dashboard Contents
</legend>
@* @if (Session["UserName"] != null)
< text >
Welcome @Session["UserName"].ToString()
</ text >
*@
</fieldset>
<h2>UserDashboard</h2>
【问题讨论】:
您的模型状态无效为登录模型创建新类或从您的操作结果中删除 if (ModelState.IsValid) 【参考方案1】:这种行为似乎是模型验证中的一些错误。
调试登录后操作并查看 ModelState.IsValid 是否真的有效以及是否显示任何错误。
[更新]
我认为问题在于您在表单中只传递了 用户名 和 密码。当它进入登录操作时,ModelState.IsValid 将为 false,因为在您的模型中您有 UserId、Email 和 Confirm Password。尝试在 If(ModelState.IsValid)
之前添加此代码ModelState.Remove("UserId");
ModelState.Remove("Email");
ModelState.Remove("ConfirmPassword");
【讨论】:
试过了还是没用..为user_login创建了另一个model_class作为用户名和密码..还是不行 好吧,如果您的 ModelState 正常并且您的代码到达 using 子句,我可以看到您的 Db 访问有问题,或者您的查询无法让用户进入 DB【参考方案2】:我认为由于 null,代码正在移动到“返回 View(objUser)”。检查一次您在检索“var”时是否获得任何数据。
【讨论】:
实际上在提交详细信息后没有数据获取(此时您建议为 null ) 此时"var var = db.Users.Where(model => model.UserName.Equals(objUser.UserName) && model.Password.Equals(objUser.Password)).FirstOrDefault() ;" 向 db.Users 插入一些数据。【参考方案3】:数据库和我创建的模型存在一些问题。我删除了模型并使用更新的数据库重新创建了模型,但出现了错误。
【讨论】:
以上是关于提交表单后在 ASP.NET MVC 中。它没有重定向到其他页面。它加载相同的页面的主要内容,如果未能解决你的问题,请参考以下文章
在 ASP.net MVC 5 应用程序中单击按钮时使用 JavaScript/jQuery 将用户重定向到页面