4

When I debug through my code I am getting a null value for the 'review' parameter in the Post EmployeeReviewEdit method. When I walk through the HttpGET method the return value of 'review' looks correct and eveything is populated correctly in the view but when I post back to to the action the parameter is null and i can't seem to figure it out why.

Here are my GET and POST actions:

[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});
}

Here is my View:

@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>

My issue was that my input control had a name "review" which was causing confusion to my parameter which is also called "review" i changed the name of the input[submit].

5
  • I don't see any obvious errors. Have you used Fiddler or an in-browser tool to look at the request body on the POST and confirm that it looks right? Are there any custom model binders configured for your application? Commented Sep 3, 2014 at 19:58
  • I actually just figured out the issue and posted it at the bottom of my post Commented Sep 3, 2014 at 20:00
  • Do i just delete this post now? Or do I just leave it? Commented Sep 3, 2014 at 20:00
  • 3
    Post your solution as an answer and accept it. See this help post as proof that it's ok to do this Commented Sep 3, 2014 at 20:02
  • 1
    I think you should leave it. The fact that the input[type=submit]'s name attribute caused a conflict with your parameter name is not obvious and is interesting (I didn't know that could happen). You should post your solution as an answer and accept it. Commented Sep 3, 2014 at 20:04

1 Answer 1

3

The issue is my HTTPPOST Action has a parameter named "review" and the submit input button on my view has a name "review". This caused a conflict which I was able to resolve by either removing the name portion on the input or changing the name to something that does not match the parameter name. Or you can change the name of the parameter and that will work just fine.

Visual Example of the line to fix:

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

CHANGE TO:

<input type="submit" class="col-sm-1 btn btn-default" id="edit-button"value="Save" />
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.