0

I have two tables in my database: referents and users.

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

And

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

Currently Create method in my controler looks like:

// 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);
    }

Obvious, right now I'm populating only Referents.

How to also populate Users table with certain data (UserName, LastName, UserName) in the same time?

In UserRole column I want to write string "referent".

EDIT Added model: Referents:

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; }

    }
}
1
  • Is the UserName field Unique Key ? Are these two tables ( referents and users ) related in Database using foreign key ? Commented Oct 12, 2018 at 14:39

1 Answer 1

1

You need to create the item for users and add it your your database.

[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);
}

Without seeing your model code it is hard to be specific, but you should be able to use that as an example. Just finish initializing the currentUser object to what you need it to be. It is possible this won't work depending on your schema.

Sign up to request clarification or add additional context in comments.

2 Comments

I just added my model.
Ok -- You should be able to use the example code above and set this up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.