This is the part-7 of asp.net mvc 5 series using entity framework and identity from scratch.In this tutorial we will make a tightly bound patient registration form using razor engine.Also includes how to update the entity model from database? and how to select the data from database using entity framework? So there is a complete step by step process of making patient registration form.If you have any query regarding this tutorial, please comment right below and your query will be answered as soon as possible. Other part of series:
- Creating ASP.NET MVC 5 project Part-1?
- Creating SQL Server database Part-2?
- Creating Entity Framework project Part-3 ?
- Customize the default template of ASP.NET MVC 5 Part-4 ?
- How to add a controller and view Part-5 ?
- How to Add Model Part-6 ?
- How to make a patient registration form?
- Update the entity model from database?
- First of all there is a change in database table name from patient to tblpatient due to namespace issue of patient class, so please change the name of table first by right click on the table name in sql server and rename it.How to update the entity model from database in asp.net mvc 5? Let's update the entity model, open the HIMS.Model.edmx diagram and right click anywhere in the .edmx diagram and go to update model from database option.e.g
- Pop-up window will appear, just checked the table checkbox and click on the finish button.
- Your model is updated with the latest table name tblpatient. e.g
- Model property & drop-down select list binding?
- For making tightly bound patient registration form, initialize the patient model class and drop-down select list in RegistrationViewModel first or copy and paste the following code in your RegistrationViewModel.e.g
- Code:
using HIMS.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace HIMS.Patient.Registration.Models
{
public class RegistrationViewModel
{
public tblPatient patient { get; set; }
public SelectList genderList { get; set; }
public SelectList martialStatusList { get; set; }
public SelectList countryList { get; set; }
public SelectList provinceList { get; set; }
public SelectList districtList { get; set; }
public SelectList cityList { get; set; }
}
}
- Binding a view with ViewModel?
- Initialize the RegistrationViewModel in your patient form/view and make a patient registration form or html with razor engine syntax tightly bound with the RegistrationViewModel.e.g
- Copy & paste the following code in your Patient view.
@model HIMS.Patient.Registration.Models.RegistrationViewModel
@{
ViewBag.Title = "Patient";
}
<style>
input, select, textarea {
max-width: 100%;
}
</style>
<div class="col-md-12">
@using (Ajax.BeginForm("savePatient", "Patient", null, new AjaxOptions
{
HttpMethod = "POST",
OnSuccess = "SucessCallBack(data)",
OnFailure = "FailureCallBack(data)"
}, new { id = "patientForm" }))
{
<div class="panel panel-default">
<div class="panel-heading">
<div class="panel-title">
Patient Registration
</div>
</div>
<div class="panel-body">
<div class="col-md-12">
<div class="col-md-4">
<div class="form-group">
@Html.LabelFor(x => x.patient.FName)
@Html.TextBoxFor(x => x.patient.FName, new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.patient.FName, null, new { @class = "ErrorMsg" })
</div>
</div>
<div class="col-md-4">
<div class="form-group">
@Html.LabelFor(x => x.patient.MName)
@Html.TextBoxFor(x => x.patient.MName, new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.patient.MName, null, new { @class = "ErrorMsg" })
</div>
</div>
<div class="col-md-4">
<div class="form-group">
@Html.LabelFor(x => x.patient.LName)
@Html.TextBoxFor(x => x.patient.LName, new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.patient.LName, null, new { @class = "ErrorMsg" })
</div>
</div>
</div>
<div class="col-md-12">
<div class="col-md-6">
<div class="form-group">
@Html.LabelFor(x => x.patient.Gender)
@Html.DropDownListFor(x => x.patient.Gender, Model.genderList, "--Select gender--", new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.patient.Gender, null, new { @class = "ErrorMsg" })
</div>
</div>
<div class="col-md-6">
<div class="form-group">
@Html.LabelFor(x => x.patient.Marital_Status)
@Html.DropDownListFor(x => x.patient.Marital_Status, Model.martialStatusList, "--Select gender--", new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.patient.Marital_Status, null, new { @class = "ErrorMsg" })
</div>
</div>
</div>
<div class="col-md-12">
<div class="col-md-6">
<div class="form-group">
@Html.LabelFor(x => x.patient.PrimaryAddress)
@Html.TextAreaFor(x => x.patient.PrimaryAddress, new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.patient.PrimaryAddress, null, new { @class = "ErrorMsg" })
</div>
</div>
<div class="col-md-6">
<div class="form-group">
@Html.LabelFor(x => x.patient.SecondaryAddress)
@Html.TextAreaFor(x => x.patient.SecondaryAddress, new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.patient.SecondaryAddress, null, new { @class = "ErrorMsg" })
</div>
</div>
</div>
<div class="col-md-12">
<div class="col-md-3">
<div class="form-group">
@Html.LabelFor(x => x.patient.CountryId)
@Html.DropDownListFor(x => x.patient.CountryId, Model.countryList, "--Select country--", new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.patient.CountryId, null, new { @class = "ErrorMsg" })
</div>
</div>
<div class="col-md-3">
<div class="form-group">
@Html.LabelFor(x => x.patient.ProvinceId)
@Html.DropDownListFor(x => x.patient.ProvinceId, Model.provinceList, "--Select province--", new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.patient.ProvinceId, null, new { @class = "ErrorMsg" })
</div>
</div>
<div class="col-md-3">
<div class="form-group">
@Html.LabelFor(x => x.patient.DistrictId)
@Html.DropDownListFor(x => x.patient.DistrictId, Model.districtList, "--Select district--", new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.patient.DistrictId, null, new { @class = "ErrorMsg" })
</div>
</div>
<div class="col-md-3">
<div class="form-group">
@Html.LabelFor(x => x.patient.CityId)
@Html.DropDownListFor(x => x.patient.CityId, Model.cityList, "--Select city--", new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.patient.CityId, null, new { @class = "ErrorMsg" })
</div>
</div>
</div>
<div class="col-md-12">
<div class="col-md-2 pull-right">
<input type="submit" id="savebtn" class="btn btn-primary pull-right" value="Save" />
</div>
</div>
</div>
</div>
}
</div> - Adding a RegistrationMethod model class?
- This is model class contains all the methods of crud operations.Basically designed for the communication with the database entities directly.You can directly communicate with database entities in the controller action result also but i have created a separate class for making things easy. Let's create a RegistrationMethod class, right click on Models folder and go to Add > Class.Pop-up window will appear and enter the name of class RegistrationMethods.cs and click to Add button.
- Your RegistrationMethods class is created, copy & paste the following code in your RegistrationMethods class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HIMS.Model;
using System.Web.Mvc;
namespace HIMS.Patient.Registration.Models
{
public class RegistrationMethods
{
Entities db = new Entities();
public SelectList genderList()
{
List<SelectListItem> genderlistitem = new List<SelectListItem>();
genderlistitem.Add(new SelectListItem { Text = "Male", Value = "Male" });
genderlistitem.Add(new SelectListItem { Text = "Female", Value = "Female" });
return new SelectList(genderlistitem, "Value", "Text");
}
public SelectList martalStatusList()
{
List<SelectListItem> maritalstatuslistitem = new List<SelectListItem>();
maritalstatuslistitem.Add(new SelectListItem { Text = "Single", Value = "Single" });
maritalstatuslistitem.Add(new SelectListItem { Text = "Married", Value = "Married" });
return new SelectList(maritalstatuslistitem, "Value", "Text");
}
public SelectList countryList()
{
List<Country> countryList= db.Countries.ToList();
List<SelectListItem> countrylistitem = new List<SelectListItem>();
foreach (var x in countryList)
{
countrylistitem.Add(new SelectListItem { Text = x.Name.ToString(), Value = x.CountryID.ToString() });
}
return new SelectList(countrylistitem, "Value", "Text");
}
public SelectList provinceList(int countryId)
{
List<Province> provinceList = db.Provinces.Where(x=>x.CountryId==countryId).ToList();
List<SelectListItem> provincelistitem = new List<SelectListItem>();
foreach (var x in provinceList)
{
provincelistitem.Add(new SelectListItem { Text = x.Name.ToString(), Value = x.Id.ToString() });
}
return new SelectList(provincelistitem, "Value", "Text");
}
public SelectList districtList(int provinceId)
{
List<District> districtList = db.Districts.Where(x=>x.ProvinceId==provinceId).ToList();
List<SelectListItem> districtlistitem = new List<SelectListItem>();
foreach (var x in districtList)
{
districtlistitem.Add(new SelectListItem { Text = x.Name.ToString(), Value = x.Id.ToString() });
}
return new SelectList(districtlistitem, "Value", "Text");
}
public SelectList cityList(int districtId)
{
List<City> citylList = db.Cities.Where(x => x.DistrictId == districtId).ToList();
List<SelectListItem> citylistitem = new List<SelectListItem>();
foreach (var x in citylList)
{
citylistitem.Add(new SelectListItem { Text = x.Name.ToString(), Value = x.Id.ToString() });
}
return new SelectList(citylistitem, "Value", "Text");
}
}
}
- Update the Registration Controller?
- Update the Patient action method, bind the RegistrationMethods class with the RegistrationViewModel or assigning the RegistrationMethods result to ViewModel.Let's copy & paste the following code in your Registration controller.using HIMS.Patient.Registration.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace HIMS.Patient.Registration.Controllers
{
public class RegistrationController : Controller
{
// GET: Registration
public ActionResult Patient()
{
try
{
RegistrationViewModel model = new RegistrationViewModel();
RegistrationMethods method = new RegistrationMethods();
List<SelectListItem> emptyList = new List<SelectListItem>();
model.genderList = method.genderList();
model.martialStatusList = method.martalStatusList();
model.countryList = method.countryList();
model.provinceList = new SelectList(emptyList);
model.districtList = new SelectList(emptyList);
model.cityList = new SelectList(emptyList);
return View(model);
}
catch (Exception ex)
{
throw;
}
}
}
}
- Update the Patient action method, bind the RegistrationMethods class with the RegistrationViewModel or assigning the RegistrationMethods result to ViewModel.Let's copy & paste the following code in your Registration controller.
- Patient Registration Form?
- Your patient registration form is ready, you can run the application by just click on the run button.
- Patient form looks like the following diagram.e.g
Next »
No comments:
Post a Comment