对于MVC中的e.preventDefault(),HttpPost to Controller为null

时间:2014-05-08 12:31:04

标签: jquery asp.net-mvc

我使用e.preventDefault();函数限制HttpPost操作命中两次。使用提交按钮,系统调用e.preventDefault();函数。之后,View将空对象返回给控制器。我已按照以下链接更正了HttpPost问题。

HttpPost action of a controller gets hit twice

如何解决此问题?

查看代码

@model EmployeeManagement.Model.EmployeeModel

@{
    ViewBag.Title = "EditEmployee";

}


<h2>Edit Employee</h2>
<div>

@using (@Html.BeginForm("EditEmployee", "Employee", FormMethod.Post, new { id = "EditEmployeeMsg" }))
{ 
    @Html.HiddenFor(a => a.employeeId)
   <div id="butonRight">
    <input type="submit" value="Save" />
    <input type="button" id="saveCancel" value="Cancel" />
   </div>

    <table>
        <tbody>
            <tr>
                <td>Title</td>
                <td>@Html.DropDownListFor(a => a.titleId, (IEnumerable<SelectListItem>)ViewBag.TitleEdit)</td>
            </tr>
            <tr>
                <td>First Name</td>
                <td>@Html.TextBoxFor(a => a.firstName)</td>
            </tr>
            <tr>
                <td>Last Name</td>
                <td>@Html.TextBoxFor(a => a.lastName)</td>
            </tr>
            <tr>
                <td>Email</td>
                <td>@Html.TextBoxFor(a => a.email)</td>
            </tr>
            <tr>
                <td>Date of Birth</td>
                <td>@Html.TextBoxFor(a => a.dateOfBirth, new { @class = "date_view", @Value = Model.dateOfBirth.ToString("dd/MM/yyyy") })</td>
            </tr>
            <tr>
                <td>Date of Join</td>
                <td>@Html.TextBoxFor(a => a.dateOfJoin, new { @class = "date_view", @Value = Model.dateOfJoin.ToString("dd/MM/yyyy") })</td>
            </tr>
            <tr>
                <td>Position</td>
                <td>@Html.DropDownListFor(a => a.positionId, (IEnumerable<SelectListItem>)ViewBag.PositionEdit)</td>
            </tr>
            <tr>
                <td>Division</td>
                <td>@Html.DropDownListFor(a => a.divisionId, (IEnumerable<SelectListItem>)ViewBag.DivisionEdit)</td>
            </tr>
            <tr>
                <td>Office</td>
                <td>@Html.DropDownListFor(a => a.locationId, (IEnumerable<SelectListItem>)ViewBag.OfficeLocEdit)</td>
            </tr>
            <tr>
                <td>Salary</td>
                <td>@Html.TextBoxFor(a => a.salary)</td>
            </tr>            
        </tbody>
    </table>

}
</div>

控制器代码

 [HttpPost]
    public ActionResult EditEmployee(EmployeeModel abcde)
    {
        bool updateResult = new EmployeeProvider().UpdateEmployee(abcde);            
        return Json(new { result = updateResult }, JsonRequestBehavior.AllowGet);

    }

Jquery代码

$(document).ready(function () {

    $('#EditEmployeeMsg').submit(EditEmployeeMessage);

}
);

function EditEmployeeMessage(e) {

    e.preventDefault();
    $.ajax({
        type: this.method,
        url: this.action,
        data: $(this).serialize(),
        success: function (data) {
            if (data.result) {
                alert('Update Succesfully.');

            }
            else
                alert('Update Failed.');
        },
        error: function (data) {
            alert('Update Failed.');
        }
    });
}

1 个答案:

答案 0 :(得分:0)

也许您的问题与您的浏览器类型有关(例如,如果您使用的是IE)。尝试将以下内容添加到您的代码中:

if (event.preventDefault) {
    event.preventDefault();
} else {
event.returnValue = false;
}