Post方法正在获取一个空参数ASP.NETMVC

本文关键字:一个 参数 NETMVC ASP 方法 获取 Post | 更新日期: 2023-09-27 18:20:29

当我通过代码进行调试时,我在Post EmployeeReviewEdit方法中得到了"review"参数的空值。当我遍历HttpGET方法时,"review"的返回值看起来是正确的,并且在视图中正确填充了所有内容,但当我返回到操作时,参数为null,我似乎不明白为什么。

以下是我的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>

我的问题是我的输入控件有一个名称"review",这对我的参数(也称为"review)造成了混淆。我更改了输入的名称[submit]

Post方法正在获取一个空参数ASP.NETMVC

问题是我的HTTPPOST操作有一个名为"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" />