【问题标题】:Asp.Net MVC 5, View not posting back to the controllerAsp.Net MVC 5,查看不回发到控制器
【发布时间】:2015-11-04 13:31:57
【问题描述】:

我正在使用 Asp.Net MVC 5 创建一个简单的登录表单。一切都很好,但是当我在提供用户 ID 和密码后单击提交按钮时,视图不会返回到所需的控制器操作 (LogIn)。这是行动:

        [HttpPost]
        public ActionResult LogIn(User user)
        {
            var auth_user = CheckAuthentication(user);
            if(auth_user!=null)
            {
                Session["user"] = new User() { UserId = user.UserId, Name = user.Name };
                return RedirectToAction("Index", "User");
            }
            return View();
        }
        [AllowAnonymous]
        public ActionResult LogIn()
        {
            return View();
        }

和视图:

@model FinancialManagement.Models.User

@{
    ViewBag.Title = "LogIn";
}

<h2>LogIn</h2>

@using (Html.BeginForm("LogIn", "User", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
        <h4>User</h4>
        <hr />
        @Html.ValidationSummary(true)
        <div class="form-group">
            @Html.LabelFor(model => model.UserId, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.UserId)
                @Html.ValidationMessageFor(model => model.UserId)
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Password, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Password)
                @Html.ValidationMessageFor(model => model.Password)
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Log In" class="btn btn-default" />
            </div>
        </div>
    </div>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

这是我完整的用户控制器:

    [Authorize]
    public class UserController : Controller
    {
        private FinancialManagmentEntities db = new FinancialManagmentEntities();

        // GET: /User/
        public ActionResult Index()
        {
            return View(db.Users.ToList());
        }

        // GET: /User/Details/5
        public ActionResult Details(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            User user = db.Users.Find(id);
            if (user == null)
            {
                return HttpNotFound();
            }
            return View(user);
        }

        // GET: /User/Create
        [AllowAnonymous]

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

        // POST: /User/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(User user)
        {
            if (ModelState.IsValid)
            {
                db.Users.Add(user);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(user);
        }
        ActionResult LogOut()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Home");
        }
        User CheckAuthentication(User user)
        {
            return db.Users.Where(u => u.UserId == user.UserId && u.Password == user.Password).FirstOrDefault();
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        [AllowAnonymous]
        public ActionResult LogIn(User user)
        {
            var auth_user = CheckAuthentication(user);
            if(auth_user!=null)
            {
                Session["user"] = new User() { UserId = user.UserId, Name = user.Name };
                return RedirectToAction("Index", "User");
            }
            return View();
        }
        [AllowAnonymous]
        public ActionResult LogIn()
        {
            return View();
        }
        public ActionResult Edit(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            User user = db.Users.Find(id);
            if (user == null)
            {
                return HttpNotFound();
            }
            return View(user);
        }
        // POST: /User/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include="UserId,Name,Password")] User user)
        {
            if (ModelState.IsValid)
            {
                db.Entry(user).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(user);
        }
        // GET: /User/Delete/5
        public ActionResult Delete(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            User user = db.Users.Find(id);
            if (user == null)
            {
                return HttpNotFound();
            }
            return View(user);
        }

        // POST: /User/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(string id)
        {
            User user = db.Users.Find(id);
            db.Users.Remove(user);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }

【问题讨论】:

  • 您的控制器名称是什么?您说“不会回到所需的控制器动作”,您的意思是它会转到其他控制器/动作,点击提交后到底发生了什么?它是否停留在同一页面上?还要检查浏览器控制台中是否有客户端错误。
  • 控制器名称为'User',与调用视图的位置相同
  • 那么您是否检查了一个断点以查看是否找到了用户?从您发布的代码中,如果用户找不到相同的视图,则会刷新。因此,当找不到用户时,将您收到的user 模型传回以查看。
  • 您的视图中的类型是否正确,您有“FinancialManagement.Models.User”是我猜您控制器中的用户是身份用户类型
  • 您显示的代码在您提交表单时点击Login() post 方法。但它永远不会转到Index() 方法(或任何其他标有[Authorize] 的方法,因为LogIn() 方法中的代码没有授权用户。并且以纯文本形式存储密码是一种可怕的做法!

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 razor


【解决方案1】:

尝试像这样指定视图的名称和模型:

return View("NameOfView", YourModel);

如果不起作用,请输入绝对路径:

return View("~/Views/FolderName/ViewName.cshtml");

【讨论】:

  • 问题是从视图返回到控制器。不打算从控制器查看
猜你喜欢
  • 2018-08-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多