Click here to Skip to main content
15,887,821 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
JavaScript
$(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) {
                    //alert('thanks for submitting');
                    //$('#dialog').dialog('close');
                    
                    //Redirect to index page of employee types
                    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
C#
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)
        {
            //Check if employee type already exist
            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();

            // TODO: the model is valid => do some processing with it
            // and return a JSON result confirming the success
            return Json(new { success = true, url = Url.Action("EmployeeTypes") });
        }


Thanks and advance,
Dan
Posted
Updated 2-Jul-15 22:23pm
v2
Comments
Sreekanth Mothukuru 3-Jul-15 3:33am    
Try loading the dialog content "$('#dialog').load(" using @Url.Content or @Url.Action

Let us know if it works.
Member 11810533 3-Jul-15 3:50am    
Hi,

I've tried sir but still, it does not display anything except for the blank modal
Kindly see below
<pre lang="Javascript">
$(function () {
$('.edit').click(function () {
$('#dialog').load('@Url.Action("EditEmployeeType",new {id=1})', function () {
$(this).dialog({
show: {
effect: "blind",
duration: 1000
},
hide: {
effect: "blind",
duration: 1000
},
width: 600
});

//$.ajax({
// type: 'GET',
// url: '../Employee/EditEmployeeType',
// data: {},
// success: function (response) {
// $('#dialog').html(response);//loads the partial edit view into the dialog div...works fine to here.
// }
//});
});
return false;
});
});
</pre>
Sreekanth Mothukuru 3-Jul-15 4:24am    
Have you checked in web developer console tab ? Does it show any error message or log?
Seems like the path to the partial page is not valid. It should be from the root I guess Ex: ~/controller
Member 11810533 3-Jul-15 4:40am    
Thanks for the help, I managed to fix it. It was me passing wrong model to my view, I checking for console log but dont know it it does not register error thats why I was not able to check immediately

1 solution

Thanks for the help sir, yeah I check on console log it does not display error but I managed to fix it by scanning every line of code for possible error. Silly me it is my controller causing the error, before it was like below and the cause was I am passing wrong model to my view.
C#
var employeeType = _db.EmployeeTypes.Find(id);
            if (employeeType == null)
                return HttpNotFound();
            var editEmployee = new EmployeeTypeEditViewModel
            {
                Id = employeeType.Id,
                Name = employeeType.Name
            };
            return PartialView(employeeType);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900