I have a view which contains a partial view that contains a form and rendered when a button is clicked and is displayed in a popup on the view. The issue is when i click the submit button the form saves but it then redirects me to action's view which i don't want to happen. I want to be able to click the submit button and form to post but not return anything...keeping the view looking the same as before (appearing that nothing has happened)". Is there anyway to do this?

Here is my Partial View:

@model EmployeeMaster.Models.EmployeeAsset


<h3><u>Does this employee have any of the following assets?</u></h3>

@using (Html.BeginForm("EmployeeAssets","Employee",FormMethod.Post, new {@id = "assets-form"}))
{
    @Html.HiddenFor(model => model.EmpNo)
    @Html.HiddenFor(model => model.ID)
    @Html.HiddenFor(model => model.UserName)
    @Html.AntiForgeryToken()
    <table class="table table-striped table-bordered table-condensed" style="margin-bottom: 0px;">
        <thead>
            <tr>
                <th>Item</th>
                <th>Yes/No</th>
                <th>Item</th>
                <th>Yes/No</th>
                <th>Item</th>
                <th>Yes/No</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>
                    @Html.Label("Cellular Telephone")
                </td>
                <td>
                    @Html.CheckBoxFor(model => model.CellPhone, new {@class = "checkbox"})
                </td>
                <td>
                    @Html.Label("Mileage Log")
                </td>
                <td>
                    @Html.CheckBoxFor(model => model.MileageLog, new { @class = "checkbox" })
                </td>
                <td>
                    @Html.Label("Travel Expense Reports")
                </td>
                <td>
                    @Html.CheckBoxFor(model => model.TravelExpenseReports, new { @class = "checkbox" })
                </td>
            </tr>
            <tr>
                <td>
                    @Html.Label("Computer")
                </td>
                <td>
                    @Html.CheckBoxFor(model => model.Computer, new { @class = "checkbox" })
                </td>
                <td>
                    @Html.Label("Pager")
                </td>
                <td>
                    @Html.CheckBoxFor(model => model.Pager, new { @class = "checkbox" })
                </td>
                <td>
                    @Html.Label("Uniforms")
                </td>
                <td>
                    @Html.CheckBoxFor(model => model.Uniforms, new { @class = "checkbox" })
                </td>
            </tr>
            <tr>
                <td>
                    @Html.Label("Customer Database")
                </td>
                <td>
                    @Html.CheckBoxFor(model => model.CustomerDatabase, new { @class = "checkbox" })
                </td>
                <td>
                    @Html.Label("Phone Card")
                </td>
                <td>
                    @Html.CheckBoxFor(model => model.PhoneCard, new { @class = "checkbox" })
                </td>
                <td>
                    @Html.Label("Vehicle")
                </td>
                <td>
                    @Html.CheckBoxFor(model => model.Vehicle, new { @class = "checkbox" })
                </td>
            </tr>
            <tr>
                <td>
                    @Html.Label("Gas Card")
                </td>
                <td>
                    @Html.CheckBoxFor(model => model.GasCard, new { @class = "checkbox" })
                </td>
                <td>
                    @Html.Label("Security Card")
                </td>
                <td>
                    @Html.CheckBoxFor(model => model.SecurityCard, new { @class = "checkbox" })
                </td>
                <td>
                    @Html.Label("Co Credit Card")
                </td>
                <td>
                    @Html.CheckBoxFor(model => model.CoCreditCard, new { @class = "checkbox" })
                </td>
            </tr>
            <tr>
                <td>
                    @Html.Label("Keys")
                </td>
                <td>
                    @Html.CheckBoxFor(model => model.Keys, new { @class = "checkbox" })
                </td>
                <td>
                    @Html.Label("Tools")
                </td>
                <td>
                    @Html.CheckBoxFor(model => model.Tools, new { @class = "checkbox" })
                </td>
                <td>
                    @Html.Label("Other PPG Property")
                </td>
                <td>
                    @Html.CheckBoxFor(model => model.OtherPPGProperty, new { @class = "checkbox" })
                </td>
            </tr>
        </tbody>
    </table>
    <input type="submit" value="save"/>
}

Here are my actions:

    public ActionResult EmployeeAssets(int empNo)
    {
        var assets =  _employeeDb.EmployeeAssets.FirstOrDefault(e => e.EmpNo == empNo);
        if (assets == null)
        {
            var newasset = new EmployeeAsset()
            {
                EmpNo = empNo
            };
            _employeeDb.EmployeeAssets.Add(newasset);
            _employeeDb.SaveChanges();
            assets = newasset;
        }
        return PartialView(assets);
    }
    [HttpPost]
    public ActionResult EmployeeAssets(EmployeeAsset asset)
    {

        _employeeDb.Entry(asset).State = EntityState.Modified;
        _employeeDb.SaveChanges();
        return null;
    }

Here is my Jquery for getting the form:

 $.ajax({
                type: 'GET',
                url: '@Url.Action("EmployeeAssets", "Employee", new { empNo = Model.EmployeeNumber })',
                success: function (data) {
                    $('#assets-container').html(data);
                }
            });
            $("#assets-button").on("click", function () {
                $('#assets-container').bPopup(
                {
                    modal: true,
                    appendTo: '#employee-edit-form',
                    onClose: function () {
                        alert('Form is submitting');
                        $('#assets-form').submit();
                    }
                });
            });

Before posting:

enter image description here

After posting:

enter image description here

Related posts

Recent Viewed