验证EditorFor helper mvc5

时间:2015-06-10 10:05:38

标签: sql-server asp.net-mvc validation razor asp.net-mvc-5

我正在为约会表单尝试服务器验证。我使用波纹管模型

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: nume@domeniu.ro)")]
        public string PacientEmail { get; set; }
        [Required(ErrorMessage = "* Completati numarul de telefon.")]
        public string PacientTelefon { get; set; }
        public string PacientSimptome { get; set; }
        }

这是视图的一部分

@Html.ValidationSummary()
                <div class="col-md-10">
                    @Html.EditorFor(model => model.DataProgramare, new { htmlAttributes = new { @class = "form-control", @placeholder = "Data Programare", @name = "datepicker"} }
                </div>

我在帐户验证和workfine方面做了类似的工作,唯一的区别是我在razor中使用TextBoxFor helpe作为输入。

使用存储过程插入数据库的控制器方法如下所示

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");
            }

知道为什么这些代码将emtpy字符串放入数据库并且模型中的属性不会被触发??? 感谢

0 个答案:

没有答案