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:
Firstly you need to amend your ServiceRegistration model to take in your user id so we can attach it to our entity. I've also added data annotations for validation purposes but you can choose to ignore these if you like.
Next you need two actions. One to serve the form from a GET request and one save the record from a POST request.
Notice how we're passing the user's id over
Im not sure how your dbContext is finding or adding users so you will need to add this yourself
And finally your view should be named AddUserInfo and should look like this
I've just wrote this on my mac's notepad application so there maybe a couple of syntax errors, so I apologise in advance.
I hope this helps.
If you redirect the user to a different view page, you need to use TempData instead of ViewBag. Here is a rough draft of code sample. You may need to modify it to fit your needs.
Model
In your HTTPPOST call, add one line in the validateuser condition statement
View