I'm trying to update an existing record in a table of profiles using C# MVC.
I need to make 2 separate registration forms.
I have already done the first one and it's working as planned which creates the user record in the database with basic info.
[HttpPost]
public ActionResult Register(Registration signingUp)
{
var db = new ShareRideDBEntities();
if (ModelState.IsValid)
{
var Data = db.tblProfiles.Create();
Data.Prof_Email = signingUp.Email;
Data.Prof_Password = signingUp.Password;
db.tblProfiles.Add(Data);
int Saved = db.SaveChanges();
if (Saved != 0)
{
Response.Write("Success.");
}
else
{
Response.Write("Error.");
}
}
if (ModelState.IsValid)
{
if (ValidateUser(signingUp.Email, signingUp.Password))
{
FormsAuthentication.SetAuthCookie(signingUp.Email, false);
return RedirectToAction("Index", "Members");
}
else
{
ModelState.AddModelError("", "");
}
}
return View();
}
What I want to do now is add to that existing record some new info using another form.
The form is asking for his name, last name, phone number and an option (with radio buttons) if he/she is a male or female.
If you need more info please comment, I'll put it asap.
EDIT:
on my Model
public class Registration
{
[EmailAddress]
public string Email { get; set; }
[DataType(DataType.Password)]
public string Password { get; set; }
}
on my second model
public class ServiceRegistration
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Phone { get; set; }
}
I still don't have a the HttpPost
method done for the second registration since I am not sure how to do it.
EDIT:
Ok so I have made a few changes base on the answer I got
My ServiceRegistration.cs
model
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace ShareRide.Models.Service
{
public class ServiceRegistration
{
[Required]
public Guid UserID { get; set; }
[Required(AllowEmptyStrings= false, ErrorMessage="Necesary Field.")]
public string FirstName{ get; set; }
[Required(AllowEmptyStrings = false, ErrorMessage = "Necesary Field.")]
public string LastName{ get; set; }
[Required(AllowEmptyStrings = false, ErrorMessage = "Necesary Field.")]
public string Phone{ get; set; }
}
}
on my MembersController.cs
controller
public ActionResult Registrationform()
{
return View();
}
[HttpGet]
public ActionResult Registrationform(Guid UserID)
{
var model = new ServiceRegistration { UserID = UserID };
return View(model);
}
[HttpPost]
public ActionResult Registrationform(ServiceRegistration model)
{
var db = new ShareRideDBEntities();
if (ModelState.IsValid)
{
var Data = db.tblProfiles.Create();
Data.PROF_UserID = model.UserID;
Data.PROF_FirstName = model.Home_Address;
Data.PROF_LastName = model.Work_Address;
Data.PROF_Phone = model.Travel_Days;
db.SaveChanges();
return RedirectToAction("Index", "Members");
}
return View(model);
}
Now I'm getting an error on model.UserID;
Cannot implicitly convert type 'System.Guid' to 'int'
On my database PROF_UserID
is the primary key and is an int
.
Also I commented the Data.PROF_UserID = model.UserID;
so I could build but when I try to access the Registrationform
view it returns an error: