Post Method获取一个空参数ASP .NET MVC

时间:2014-09-03 19:50:34

标签: c# asp.net-mvc asp.net-mvc-4 razor

当我通过我的代码调试时,我在Post EmployeeReviewEdit方法中获得了'review'参数的空值。当我浏览HttpGET方法时,'review'的返回值看起来是正确的,并且eveything在视图中正确填充,但是当我回发到操作时,参数为null,我似乎无法弄明白为什么。< / p>

以下是我的GET和POST操作:

[HttpGet]
public ActionResult EmployeeReviewEdit(int id, string employeeName)
{
    ViewBag.EmployeeName = employeeName;
    var review = _employeeDb.EmployeeReviews.Find(id);
    var cono = _employeeDb.EmployeeMasters.FirstOrDefault(e => e.EmployeeNumber ==  review.EmployeeNo).CompanyNumber;
    var list = _selectListLib.GetManagers((int)cono,Convert.ToInt32(review.ReviewBy));
    ViewBag.ManagerList = list;
    return View(review);

}

[HttpPost]
public ActionResult EmployeeReviewEdit(EmployeeReview review)
{
    _dbDataManipulation.UpdateEmployeeReview((int)review.EmployeeNo,(DateTime) review.ReviewDate, review.ReviewBy, CurrentUserName(), review.Id);
    return RedirectToAction("EmployeeReview", new { empNo = review.EmployeeNo});
}

这是我的观点:

@model EmployeeMaster.Models.EmployeeReview
@{
    ViewBag.Title = "EmployeeReviewEdit";
}

@section scripts
{
    <script type="text/javascript">
        $(function () {
            $('#ReviewDate').datepicker();
        });
    </script>
}

<div id="results-container" style="background-color: #c9c9c9; padding: 10px; margin-top: 40px;">
    <h2>@ViewBag.EmployeeName - Edit Review</h2>
    <div style="background-color: #9e2b3c; padding: 1px; margin-bottom: 10px"></div>

    @using (Html.BeginForm("EmployeeReviewEdit","Employee", FormMethod.Post, new{@id = "review-edit-form"}))
    {
        @Html.Label("Review Date: ", new { id = "reviewdate-label" })
        @Html.TextBoxFor(model => model.ReviewDate,"{0:d}", new { @class = "form-control" })
        @Html.Label("Review By: ", new { id = "reviewby-label" })
        @Html.DropDownListFor(model => model.ReviewBy, (List < SelectListItem >)ViewBag.ManagerList, new { @class = "form-control" })
        @Html.HiddenFor(model => model.EmployeeNo)
        @Html.HiddenFor(model => model.CreatedBy)
        @Html.HiddenFor(model => model.CreatedOn)
        @Html.HiddenFor(model => model.ModifiedBy)
        @Html.HiddenFor(model => model.ModifiedOn)
        @Html.HiddenFor(model => model.Id)

        <input type="submit" name="review" class="col-sm-1 btn btn-default" id="edit-button"value="Save" />

    }
</div>

我的问题是我的输入控件有一个名称“审核”,这导致我的参数混淆,这也称为“审核”我更改了输入的名称[提交]。

1 个答案:

答案 0 :(得分:3)

问题是我的HTTPPOST Action有一个名为“review”的参数,我视图上的提交输入按钮的名称为“review”。这导致了冲突,我可以通过删除输入上的名称部分或将名称更改为与参数名称不匹配的内容来解决。或者您可以更改参数的名称,这样就可以了。

要修复的行的可视示例:

<input type="submit" name="review" class="col-sm-1 btn btn-default" id="edit-button"value="Save" />

更改为:

<input type="submit" class="col-sm-1 btn btn-default" id="edit-button"value="Save" />