I have ViewModel which contains some proprty of class. Code below.
public class ViewModel
{
public Doctor VmDoctor { get; set; }
public Patient VmPatient { get; set; }
public List<Visit> VmVisit { get; set; }
public List<Hours> hours { get; set; }
public List<Hours> hours2 { get; set; }
public Schedule schedule { get; set; }
public bool BlockBtn { get; set; }
public Test test { get; set; }
}
In this case important property is Patient VmPatient. This is a model which has been generated by Database Model First. He has validation.Code below.
public partial class Patient
{
public Patient()
{
this.Visits = new HashSet<Visit>();
}
public int PatientID { get; set; }
[Required(ErrorMessage = "Podaj imię.")]
public string name { get; set; }
[Required(ErrorMessage = "Podaj nazwisko.")]
public string surname { get; set; }
[Required(ErrorMessage = "Podaj pesel.")]
[RegularExpression(@"^\(?([0-9]{11})$", ErrorMessage = "Nieprawidłowy numer pesel.")]
public string pesel { get; set; }
[Required(ErrorMessage = "Podaj miasto.")]
public string city { get; set; }
[Required(ErrorMessage = "Podaj kod pocztowy.")]
public string zipCode { get; set; }
[Required(ErrorMessage = "Podaj e-mail.")]
[EmailAddress(ErrorMessage = "Nieprawidłowy adres e-mail")]
public string email { get; set; }
[Required(ErrorMessage = "Podaj telefon komórkowy.")]
[RegularExpression(@"^\(?([0-9]{9})$", ErrorMessage = "Nieprawidłowy numer telefonu.")]
public string phone { get; set; }
public virtual ICollection<Visit> Visits { get; set; }
}
and i have Main Index where return my ViewModel because, display two Models in the same View. Code below
public ActionResult Index(int id)
{
ViewModel _viewModle = new ViewModel();
schedule = new Schedule();
if(Request.HttpMethod == "Post")
{
return View(_viewModle);
}
else
{
idDr = id;
_viewModle.schedule = schedule;
_viewModle.BlockBtn = _repository.BlockBtn(schedule);
_viewModle.VmDoctor = db.Doctors.Find(idDr);
_viewModle.hours = _repository.GetHours();
foreach (var item in _viewModle.hours)
{
_viewModle.hours2 = _repository.GetButtonsActiv(item.hourBtn, item.count, idDr, schedule);
}
}
if (_viewModle == null)
{
return HttpNotFound();
}
return View(_viewModle);
}
inside View Index i display my objects and rendered partial _FormPatient.Code below.
@model Dentist.Models.ViewModel
<div class="container-select-doctor">
<div class="row">
<div class="text-left">
<div class="row">
<div class="content">
<div class="profileImage">
<div class="imageContener"><img style="margin:1px;" src="@Url.Content("~/Images/" + System.IO.Path.GetFileName(@Model.VmDoctor.image))" /></div>
</div>
<div class="profileInfo">
<div class="profileInfoName">@Model.VmDoctor.name @Model.VmDoctor.surname</div>
<div class="profileInfoSpeciality">@Model.VmDoctor.specialty</div>
</div>
</div>
</div>
</div>
@ViewBag.firstDay<br />
@ViewBag.lastDay<br />
<div class="text-middle">
<div class="content">
<div id="partialZone">
@Html.Partial("_TableSchedule")
</div>
</div>
</div>
<div class="text-right">
<div class="content">
@Html.Partial("_FormPatient")
</div>
</div>
</div>
</div>
and last step is form which has been rendered inside Main Index by @Html.partial.code below
@model Dentist.Models.ViewModel
@using (Html.BeginForm("Create","Patient"))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<font color="red">@ViewBag.Pesel</font>
<div class="form-horizontal">
<div class="form-group">
@Html.LabelFor(model => model.VmPatient.email, htmlAttributes: new { @class = "control-label col-md-2" }, labelText: "E-mail:")
<div class="col-md-10">
@Html.TextBoxFor(model => model.VmPatient.email, new { htmlAttributes = new { @class = "form-control" } })
@*<input class="form-control" id="email" name="email" type="text" value="">*@
@Html.ValidationMessageFor(model => model.VmPatient.email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.VmPatient.phone, htmlAttributes: new { @class = "control-label col-md-2" }, labelText: "Telefon kom.:")
<div class="col-md-10">
@Html.TextBoxFor(model => model.VmPatient.phone, new { maxlength = 9 })
@*<input class="form-control" maxlength="9" id="phone" name="phone" type="text" value="" />*@
@Html.ValidationMessageFor(model => model.VmPatient.phone, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
Please pay attention that this form redirect to another Controller where data will be validate and save to database. Method where data from FORM will be validate and save. code below
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Patient pat)
{
ViewModel vm = new ViewModel();
DentistEntities db = new DentistEntities();
if (ModelState.IsValid)
{
db.Patients.Add(pat);
db.SaveChanges();
}
return RedirectToAction("Index", "Visit", new { id = VisitController.idDr });
}
Conclusion How can i get validation for this form! I have observed that,every time modelstate.isvalid return false.. I dont have any ideas so I would like to ask you for help. Best regards.
patis null in your POST method - user3559349