How to save additional data in the database?

Asked

Viewed 214 times

3

I’m making a user registration, but I’m able to save only the Username and the Password in the database. How can I save the Surname along?

Below is my controller with its actions.

using ProjetoProtocolo_TCC.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using WebMatrix.WebData;

namespace ProjetoProtocolo_TCC.Controllers
{
    public class ContaController : Controller
    {
        // GET: Conta
        [HttpGet]

        public ActionResult Login()
        {

            return View();
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Login(Login logindata, string ReturnUrl )
        {
            if (ModelState.IsValid)
            {
               if (WebSecurity.Login(logindata.NomeUsuario, logindata.Senha))
               {
                   if(ReturnUrl!= null)
                   {
                       return Redirect(ReturnUrl);
                   }
                   return RedirectToAction("Index", "Home");
               }
               else
               {
                  ModelState.AddModelError("", "Nome de usuário ou senha inválidos");
                 return View(logindata);
               }


            }
            ModelState.AddModelError("", "Nome de usuário ou senha inválidos");
            return View(logindata);
           // return Content(logindata.NomeUsuario + ' ' + logindata.Senha);
        }

        [HttpGet]
        public ActionResult Register()
        {

            return View();

        }

        [HttpPost]
        [ValidateAntiForgeryToken]

        public ActionResult Register(Register registerdata, string role)
        {
            if (ModelState.IsValid)
            {

                try
                {
                    WebSecurity.CreateUserAndAccount(registerdata.NomeUsuario, registerdata.Senha);

                    var MINHAVARIAVEL = registerdata.SobrenomeUsuario.ToString();
                    //{Preciso salvar no banco de dados o valor do sobrenomeUsuario , porem
                // o websecurity so permite eu salvar o Username e password , teria algum jeito de salvar
                // os demais campos?}

                    Roles.AddUserToRole(registerdata.NomeUsuario, role);



                    return RedirectToAction("Index", "Home");
                }
                catch (MembershipCreateUserException exception )
                {

                    ModelState.AddModelError("", "O Nome de usuário já existe");
                    return View(registerdata);
                }
            }
            ModelState.AddModelError("", "O Nome de usuário já existe");
            return View(registerdata);
          //  return Content(registerdata.NomeUsuario + ' ' + registerdata.Senha);
        }
        public ActionResult Logout()
        {
            WebSecurity.Logout();
            return RedirectToAction("Index", "Home");
        }

    }
}

this is my class that are the information:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ProjetoProtocolo_TCC.Models
{
    public class Register
    {
        public string NomeUsuario { get; set; }
        public string SobrenomeUsuario { get; set; }
        public string Senha { get; set; }
    }
}

It’s a login code, in which I save the Username and your password , however I need to save more information as last name, but I’m not getting

1 answer

0

Hello, I know the method WebSecurity.CreateUserAndAccount(). You can try something like

WebSecurity.CreateUserAndAccount(NomeUsuario, Senha,
                new { SobrenomeUsuario = SobrenomeUsuario });

The idea is similar to sending data on a ActionResult. I hope I’ve helped.

I’m studying the same subject on the site Alura and they teach that way.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.