I'll keep it simple.
My controller
public ActionResult Registrationform()
{
if (Session["UserID"] == null)
{
return RedirectToAction("Index", "Main");
}
else
{
return View();
}
}
[HttpPost]
public ActionResult Registrationform(AddressRegistration model)
{
var db = new AddressDBEntities();
if (ModelState.IsValid)
{
var Data = db.tblProfiles.Create();
Data.PROF_HomeAddr = model.Home_Address;
Data.PROF_WorkAddr = model.Work_Address;
db.tblProfiles.Add(Data);
db.SaveChanges();
return RedirectToAction("Index", "Members");
}
return View(model);
}
My model
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace Address.Models.Address
{
public class AddressRegistration
{
[Required(AllowEmptyStrings= false, ErrorMessage="Este campo es necesario.")]
public string Home_Address { get; set; }
[Required(AllowEmptyStrings = false, ErrorMessage = "Necesary field.")]
public string Work_Address { get; set; }
}
}
My view
@model Address.Models.Address.AddressRegistration
@using (Html.BeginForm("Registrationform", "Members", FormMethod.Post, new {id = "wizardform"}))
{
@Html.ValidationSummary(true, "Please fix the errors.")
@Html.TextBoxFor(m => m.Home_Address)
@Html.TextBoxFor(m => m.Work_Address)
<input type="submit" value="submit" />
}
I'm trying to update a record in my database after the user has logged in his account. He can fill this form and update his address.
In my database currently those are NULL
by default.
I have a primary key set to the column named PROF_UserID
.
If the user tries accessing the MembersController.cs
views he/she will get redirected if (Session["UserID"] == null)
.
I'm trying to update the record of that specific PROF_UserID
which is the one logged in already by adding the home address to PROF_HomeAddress
and work address to PROF_WorkAddress
in the database but the method I'm using only creates a new record, not update it.
I've search for a while and I can't find any answers on how to do it in MVC 4 C# and what not.
Thanks, if you need more info please comment.
EDIT:
I've changed a bit of the code base on the answer.
Controller
[HttpPost]
public ActionResult Registrationform(AddressRegistration model)
{
var db = new AddressDBEntities();
if (ModelState.IsValid)
{
var updateData = db.tblProfiles.FirstOrDefault(r => r.PROF_UserID == model.UserID);
if (updateData != null)
{
updateData.PROF_HomeAddr = model.Home_Address;
updateData.PROF_WorkAddr = model.Work_Address;
db.SaveChanges();
}
return RedirectToAction("Index", "Members");
}
Response.Write("Error");
return View();
}
Model
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace Address.Models.Adress
{
public class AddressRegistration
{
public int UserID { get; set; }
[Required(AllowEmptyStrings= false, ErrorMessage="Necesary field.")]
public string Home_Address { get; set; }
[Required(AllowEmptyStrings = false, ErrorMessage = "Necesary field.")]
public string Work_Address { get; set; }
}
}
View
@model Address.Models.Address.AddressRegistration
@using (Html.BeginForm("Registrationform", "Members", FormMethod.Post, new {id = "wizardform"}))
{
@Html.HiddenFor(m=> m.UserID)
@Html.ValidationSummary(true, "check for errors.")
@Html.TextBoxFor(m => m.Home_Address)
@Html.TextBoxFor(m => m.Work_Address)
@Html.ValidationMessageFor(m=>m.Work_Address)
<input type="submit" value="submit" />
}
I've tried this but it always ends up giving me the @Html.ValidationSummary(true, "check for errors.")
error and the Response.Write("Error")
one.
The ModelState.IsValid
in the controller is returning false for some reason.
What am I doing wrong?
If i remove the @Html.HiddenFor(m=>m.UserID)
it will return with model.UserID
always 0 which will not find in the database, how can I make it so it finds the UserID the current user logged in has?
My MainController
has
private bool ValidateUser(string Email, string Password)
{
bool isValid = false;
using (var db = new AddressDBEntities())
{
var User = db.tblProfiles.FirstOrDefault(u => u.PROF_Email == Email);
if (User.PROF_Password == Password)
{
Session["UserID"] = User.PROF_UserID;
isValid = true;
}
}
return isValid;
}