【问题标题】:Database first MVC, object not populating数据库第一个 MVC,对象未填充
【发布时间】:2016-10-10 22:11:18
【问题描述】:

我必须使用数据库优先方法,解决方案有 3 个项目。 2 是类库,第三是实际的 MVC 项目。一个类库有 edmx 设计器。另一个有不同的模型类。这两个库被 MVC 项目引用。我的问题是该对象没有填充值。我想这是某个地方的命名冲突。对象 tblRDBUser 未填充。数据库中也有一个具有相同对象名称的表。对象属性也匹配列名。我没有从视图中获取值到控制器中。

项目一:

RDB.DataModel, it contains the EDMX designer.

项目二:RDB.Models

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RDB.DataModel;

public class Authenticate
{
    public string AuthenticateUser(tblRDBUser User)
    {
        Book_TradEntities users = new Book_TradEntities();
        return (from p in users.tblRDBUsers where p.UserName == User.UserName && p.Password == User.Password select p.UserName).FirstOrDefault();
    }

}

项目3:

查看:

@model RDB.DataModel.tblRDBUser

@{
    ViewBag.Title = "Index";
}
@Html.TextBoxFor(a => a.UserName)
@Html.TextBoxFor(a => a.Password)
@Html.ActionLink("Login", "Authenticate");

控制器:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using RDB.DataModel;
using RDB.Models;

namespace CignaRDB.Controllers
{
public class RDBWebController : Controller
{
    //
    // GET: /RDBWeb/
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Authenticate(tblRDBUser User)
    {
        if (ModelState.IsValid)
        {
            Authenticate user = new Authenticate();
            user.AuthenticateUser(User);
            return View();
        }
        return View();
    }
}
}

【问题讨论】:

  • 我真的看到纯文本密码存储了吗?或者这是您的代码的简化?
  • @trailmax 哦,这是简化的代码。
  • 您将不得不对您的问题进行更多解释。什么对象没有被填充?您认为您的图层如何影响这一点?
  • 啊!好的!说没有明文密码存储! -))
  • 我认为您的视图中缺少“表单”元素。你没有提到哪个对象没有填充。当您单击按钮时,您是否没有从视图获取值到控制器?

标签: asp.net-mvc entity-framework asp.net-mvc-5


【解决方案1】:

主要问题。您创建链接@Html.ActionLink("Login", "Authenticate"),点击他后只需将用户重定向到身份验证方法。安装一个链接,您应该生成提交表单的按钮。所以你的视图代码应该是这样的

@model RDB.DataModel.tblRDBUser
@{
    ViewBag.Title = "Index";
}
@using (Html.BeginForm("Authenticate"))
{
   @Html.TextBoxFor(a => a.UserName)
   @Html.TextBoxFor(a => a.Password)

   <input type="submit" value="Login" />

}

您的控制器看起来也很奇怪,可能是因为这是代码简化的结果,但至少应该在您的模型无效时进行下一次更改(return View("Index");):

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using RDB.DataModel;
using RDB.Models;

namespace CignaRDB.Controllers
{
public class RDBWebController : Controller
{
    //
    // GET: /RDBWeb/
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Authenticate(tblRDBUser User)
    {
        if (ModelState.IsValid)
        {
            Authenticate user = new Authenticate();
            user.AuthenticateUser(User);
            return View();
        }
        return View("Index");//because view name and Action method not match
    }
}
}

【讨论】:

  • 非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-07
  • 2014-04-12
  • 2017-09-24
  • 1970-01-01
  • 1970-01-01
  • 2017-10-10
  • 1970-01-01
相关资源
最近更新 更多