I'm trying a server validation for an appointment form. I use the bellow model
public class ProgramareMF
{
[Key]
public int PacientID { get; set; }
[Required(ErrorMessage = "* Alege data la care vrei sa fi programat.")]
public string DataProgramare { get; set; }
[Required(ErrorMessage = "* Completati numele.")]
[StringLength(50, MinimumLength = 2, ErrorMessage = "* Numele trebuie să aibă cel puțin 2 caractere.")]
public string PacientNume { get; set; }
[Required(ErrorMessage = "* Completati prenumele.")]
public string PacientPrenume { get; set; }
[Required(ErrorMessage = "* Emailul nu poate fi gol.")]
[RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
@"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
@".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$",
ErrorMessage = "Adresa de e-mail nu are formatul valid (ex: [email protected])")]
public string PacientEmail { get; set; }
[Required(ErrorMessage = "* Completati numarul de telefon.")]
public string PacientTelefon { get; set; }
public string PacientSimptome { get; set; }
}
This is part of the view
@Html.ValidationSummary()
<div class="col-md-10">
@Html.EditorFor(model => model.DataProgramare, new { htmlAttributes = new { @class = "form-control", @placeholder = "Data Programare", @name = "datepicker"} }
</div>
I've done similar work with account validation and worksfine the only difference there is that I use TextBoxFor helpe in razor for the input.
The controller methods that does the insert into database using a stored procedure are shown bellow
public class ProgramareMFBusinessLayer
{
public void AddAppointmentMF(ProgramareMF programareMF)
{
string connectionString = ConfigurationManager.ConnectionStrings["MedicalDBContext"].ConnectionString;
try
{
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmdSp = new SqlCommand("spAddAppointmentMF", con);
cmdSp.CommandType = CommandType.StoredProcedure;
using (var command = con.CreateCommand())
{
//command.CommandType = CommandType.StoredProcedure;
//command.CommandText = "fnCreateAppoitmentXML";
SqlParameter paramPacientId = new SqlParameter();
paramPacientId.ParameterName = "@PacientId";
paramPacientId.Value = programareMF.PacientID;
cmdSp.Parameters.Add(paramPacientId);
SqlParameter paramDataProgramare = new SqlParameter();
paramDataProgramare.ParameterName = "@DataProgramare";
paramDataProgramare.Value = programareMF.DataProgramare;
cmdSp.Parameters.Add(paramDataProgramare);
SqlParameter paramPacientNume = new SqlParameter();
paramPacientNume.ParameterName = "@PacientNume";
paramPacientNume.Value = programareMF.PacientNume;
cmdSp.Parameters.Add(paramPacientNume);
SqlParameter paramPacientPrenume = new SqlParameter();
paramPacientPrenume.ParameterName = "@PacientPrenume";
paramPacientPrenume.Value = programareMF.PacientPrenume;
cmdSp.Parameters.Add(paramPacientPrenume);
SqlParameter paramEmail = new SqlParameter();
paramEmail.ParameterName = "@PacientEmail";
paramEmail.Value = programareMF.PacientEmail;
cmdSp.Parameters.Add(paramEmail);
SqlParameter paramTelefon = new SqlParameter();
paramTelefon.ParameterName = "@PacientTelefon";
paramTelefon.Value = programareMF.PacientTelefon;
cmdSp.Parameters.Add(paramTelefon);
SqlParameter paramSimptome = new SqlParameter();
paramSimptome.ParameterName = "@PacientSimptome";
paramSimptome.Value = programareMF.PacientSimptome;
cmdSp.Parameters.Add(paramSimptome);
con.Open();
cmdSp.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
//return ex.Message;
}
[Authorize]
[HttpPost]
public ActionResult ProgramareMF(FormCollection formCollection)
{
BusinessLayer.ProgramareMF programareMF = new BusinessLayer.ProgramareMF();
programareMF.PacientID = WebSecurity.GetUserId(User.Identity.Name);
programareMF.DataProgramare = formCollection["DataProgramare"];
programareMF.PacientNume = formCollection["PacientNume"];
programareMF.PacientPrenume = formCollection["PacientPrenume"];
programareMF.PacientEmail = formCollection["PacientEmail"];
programareMF.PacientTelefon = formCollection["PacientTelefon"];
programareMF.PacientSimptome = formCollection["PacientSimptome"];
ProgramareMFBusinessLayer programareMFBusinessLayer = new ProgramareMFBusinessLayer();
programareMFBusinessLayer.AddAppointmentMF(programareMF);
return View();
//return RedirectToAction("Index");
}
Any idea why these code puts emtpy strings into the database and the attributes on the model are not triggerd??? Thanks