Question:
I'm doing a user registration, but I'm managing to save only the Username and Password in the database. How can I save the Last Name together?
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 the information is:
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 password, but I need to save more information as last name, but I'm not able to
Answer:
Hi, I know the WebSecurity.CreateUserAndAccount()
method. You can try something like
WebSecurity.CreateUserAndAccount(NomeUsuario, Senha,
new { SobrenomeUsuario = SobrenomeUsuario });
The idea is similar to sending data in an ActionResult
. Hope this helps.
I'm studying the same subject on the Alura
website and they teach it that way.