【问题标题】:How to populate two tables in create method [asp.net mvc]如何在创建方法中填充两个表 [asp.net mvc]
【发布时间】:2019-03-17 19:31:18
【问题描述】:

我的数据库中有两个表:referentsusers

Referents:
FirstName|LastName|Phone|Password|ConfirmPassword|UserName|Email

还有

Users:
FirstName|LastName|UserRole|Password|ConfirmPassword|UserName

目前我的控制器中的Create 方法看起来像:

// GET: Referents/Create
        public ActionResult Create()
        {
            return View();
        }
    // POST: Referents/Create
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "ReferentID,FirstName,LastName,Phone,Email,Password,ConfirmPassword")] Referents referents)
    {
        if (ModelState.IsValid)
        {
            db.Referents.Add(referents);          
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(referents);
    }

很明显,现在我只填充 Referents

如何同时用某些数据(用户名、姓氏、用户名)填充Users 表?

UserRole 列中我想写字符串“referent”。

EDIT 添加模型: 参考:

namespace StudentService.Models
{
    public class Referents
    {
        [Key]
        public int ReferentID { get; set; }

    [Required(ErrorMessage = "Морате унети име!")]
    public string FirstName { get; set; }

    [Required(ErrorMessage = "Презиме је обавезно!")]
    public string LastName { get; set; }

    [Required(ErrorMessage = "Корисничко име је обавезно!")]
    [Index(IsUnique = true)]
    public string UserName { get; set; }

    [Required(ErrorMessage = "Унесите исправан број телефона.")]
    [DataType(DataType.PhoneNumber)]
    public string Phone { get; set; }

    [Required(ErrorMessage = "Унесите исправну адресу електронске поште.")]
    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }

    [Required(ErrorMessage = "Шифра је обавезна!")]
    [DataType(DataType.Password)]
    public string Password { get; set; }

    [Compare("Password", ErrorMessage = "Морате потврдити лозинку!")]
    [DataType(DataType.Password)]
    public string ConfirmPassword { get; set; }

    }
}

【问题讨论】:

  • 用户名字段是唯一键吗?这两个表(引用者和用户)在数据库中是否使用外键相关?

标签: sql asp.net asp.net-mvc database


【解决方案1】:

您需要为users 创建项目并将其添加到您的数据库中。

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ReferentID,FirstName,LastName,Phone,Email,Password,ConfirmPassword")] Referents referents)
{
    if (ModelState.IsValid)
    {
        // Create the user data for the current referent.
        Users currentUser = new Users(){
            FirstName = referents.FirstName,
            UserRole = "referent"
            // .... Finish initializing fields of your model.
        };

        db.Referents.Add(referents); 
        // Save the new user in your database.         
        db.Users.Add(currentUser);

        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(referents);
}

如果没有看到您的模型代码,很难具体说明,但您应该能够将其用作示例。只需将 currentUser 对象初始化为您需要的对象即可。根据您的架构,这可能不起作用。

【讨论】:

  • 我刚刚添加了我的模型。
  • 好的 -- 您应该能够使用上面的示例代码并进行设置。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多