While writing code in MVC you always create model and the view associated with it.
Model is the like the code page where you write your code logic and the view is like HTML where the designing CSS and the scripting is being performed.
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
This is the controller where you write your programming logic.
@model MVC_Demo.Models.EmployeeModel
@{
ViewBag.Title = "Home";
}
<h2>Home</h2>
<div>
<h4>EmployeeModel</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.FirstName)
</dt>
<dd>
@Html.DisplayFor(model => model.FirstName)
</dd>
<dt>
@Html.DisplayNameFor(model => model.MiddleName)
</dt>
<dd>
@Html.DisplayFor(model => model.MiddleName)
</dd>
<dt>
@Html.DisplayNameFor(model => model.LastName)
</dt>
<dd>
@Html.DisplayFor(model => model.LastName)
</dd>
<dt>
@Html.DisplayNameFor(model => model.PhoneNo)
</dt>
<dd>
@Html.DisplayFor(model => model.PhoneNo)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Address)
</dt>
<dd>
@Html.DisplayFor(model => model.Address)
</dd>
<dt>
@Html.DisplayNameFor(model => model.errormsg)
</dt>
<dd>
@Html.DisplayFor(model => model.errormsg)
</dd>
</dl>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { /* id = Model.PrimaryKey */ }) |
@Html.ActionLink("Back to List", "Index")
</p>
This is the view associated with the model.
If you want to use the same view for two different actions like when you register the user and when you edit the user .
One way to do this is to create two separate views for it which is longer and the tedious task.
Other way is to make that view common in both the actions.
@model MVC_Demo.Models.EmployeeModel
@{
ViewBag.Title = "RegisterEmployee";
}
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<h2>RegisterEmployee</h2>
@using (Html.BeginForm("RegisterEmployeeDetails", "Register", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>EmployeeModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => Model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => Model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => Model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => Model.MiddleName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => Model.MiddleName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => Model.MiddleName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => Model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => Model.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => Model.LastName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => Model.PhoneNo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => Model.PhoneNo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => Model.PhoneNo, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => Model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => Model.Address, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => Model.Address, "", 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>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
This is my register view which i have used for registering and editing user.
EmployeeRepository EmpRepObj = null;
EmployeeModel EmpModObj = null;
public ActionResult Home()
{
return View();
}
[HttpGet]
public ActionResult RegisterEmployee()
{
//if (TempData["EmpModel"] != null)
// return View((EmployeeModel)TempData["EmpModel"]);
return View();
}
[HttpPost]
public ActionResult RegisterEmployeeDetails(EmployeeModel EmpModel)
{
EmpModObj = new EmployeeModel();
try
{
if (ModelState.IsValid)
{
EmpRepObj = new EmployeeRepository();
EmpRepObj.SaveDetails(EmpModel);
}
else
{
return View("RegisterEmployee");
}
}
catch (Exception ex)
{
EmpModObj.msg = ex.Message;
}
return View(EmpModel);
}
[HttpPost]
public ActionResult EditDetails(EmployeeModel EmpModel)
{
try
{
EmpRepObj = new EmployeeRepository();
EmpModel = EmpRepObj.EditDetails(EmpModel);
}
catch (Exception ex)
{
EmpModObj.msg = ex.Message;
}
ModelState.Clear();
//TempData["EmpModel"] = EmpModel;
//return RedirectToActionPermanent("RegisterEmployee", EmpModel);
return View("RegisterEmployee", EmpModel);
}
}
So in the edit details action i have returned to the register employee view again reusing it.
0 Comment(s)