Hi,
I'm trying to display my edit form using jquery UI modal, apparently I was able to display the modal but no control is showingI mean blank modal is being open.
Can anyone point what mistake did I make or suggestion to make it better?
Below is my script for opening the modal
$(function () {
$('#modalLink').click(function () {
$('#dialog').load(this.href, function () {
$(this).dialog({
show: {
effect: "blind",
duration: 1000
},
hide: {
effect: "blind",
duration: 1000
},
width: 600
});
bindForm(this);
});
return false;
});
$('.edit').click(function () {
$('#dialog').load(this.href, function () {
$(this).dialog({
show: {
effect: "blind",
duration: 1000
},
hide: {
effect: "blind",
duration: 1000
},
width: 600
});
bindForm(this);
});
return false;
});
});
function bindForm(dialog) {
$('form', dialog).submit(function () {
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (result) {
if (result.success) {
window.location = result.url;
} else {
$('#dialog').html(result);
bindForm();
}
}
});
return false;
});
}
Here is my index page
@model IEnumerable<PayrollSystem.Models.ViewModels.EmployeeTypeListViewModel>
@{
ViewBag.Title = "EmployeeTypes";
}
<script src="~/Scripts/Employee/EmployeeType.js"></script>
<h2>EmployeeTypes</h2>
<p>
<p>
@Html.ActionLink("New", "NewEmployeeType", "Employee", null, new { id = "modalLink" })
<div id="dialog"></div>
</p>
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.ActionLink("Edit", "EditEmployeeType", new { id = item.Id }, new { @class = "edit" }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
and last below is my Edit Partial page
@model PayrollSystem.Models.ViewModels.EmployeeTypeEditViewModel
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>EmployeeTypeEditViewModel</h4>
<hr />
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.Id)
<div class="form-group">
@Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
In case here also is my controller
public ActionResult EditEmployeeType(int id)
{
var employeeType = _db.EmployeeTypes.Find(id);
if (employeeType == null)
return HttpNotFound();
var editEmployee = new EmployeeTypeEditViewModel
{
Id = employeeType.Id,
Name = employeeType.Name
};
return PartialView(editEmployee);
}
[HttpPost]
public ActionResult EditEmployeeType(EmployeeTypeEditViewModel model)
{
var exist = _db.EmployeeTypes.Any(c => c.Name.ToLower() == model.Name.ToLower() && c.Id!=model.Id);
if (exist)
{
ModelState.AddModelError("Name", "Employee type already exist!");
}
if (!ModelState.IsValid)
{
return PartialView(model);
}
var employeeType = _db.EmployeeTypes.Find(model.Id);
if (employeeType == null)
return HttpNotFound();
_db.Entry(employeeType).State = System.Data.Entity.EntityState.Modified;
_db.SaveChanges();
return Json(new { success = true, url = Url.Action("EmployeeTypes") });
}
Thanks and advance,
Dan