In MVC there is convention that we dont need any form tag in it. But there are certain requirements where we want to use the form tag for submitting data from the partial view in that case we will include the form tag in it.
So for doing that we need to first create the partial view into the controller action.
[HttpGet]
public ActionResult _Edit(int id = 0)
{
EmpMod = new EmployeeModel();
EmpMod.EmpId = id;
EmpRep = new EmployeeRepository();
EmpMod = EmpRep.EditDetails(EmpMod);
return PartialView("_Edit", EmpMod);
}
After this we will create the partial view that will contain the form tag for the submission of data on the submit button click.
@model Modal_PopUp_Basic.Models.EmployeeModel
@using (Html.BeginForm("SaveDetails","Home",new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<h4>EmployeeModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.EmpId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EmpId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.EmpId, "", new { @class = "text-danger" })
</div>
</div>
<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" }, id = "txtfirstname" })
@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", id = "txtmiddlename" } })
@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" }, id = "txtlastname" })
@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">
@Html.LabelFor(model => model.msg, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.msg, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.msg, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btnSave" />
@Html.ActionLink("Back to List", "Index")
</div>
</div>
}
Then at last we will create action for the submission of data on the submit button click of partial view.
[HttpPost]
public ActionResult SaveDetails(EmployeeModel EmpMod)
{
EmpRep = new EmployeeRepository();
EmpMod = EmpRep.SaveDetails(EmpMod);
return RedirectToAction("Index");
}
0 Comment(s)