8

I cannot figure out why my view only passes back a NULL for a model to my controller. This is for an Edit Post method. I checked other controllers with Edit Post methods that are structured the same way as this one and they work fine. It seems to be just this view and controller.

Here is my view:

@model Non_P21_Quote_System_v1._0.Models.gl_code

@{
    ViewBag.Title = "Edit";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Edit</h2>

@if (TempData["Message"] != null)
{
    <div style="color:green">
        @TempData["Message"]
    </div><br />
}
@if (ViewBag.error != null)
{
    <div style="color:red">
        <h3>@ViewBag.error</h3>
    </div><br />
}

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>gl_code</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.ID)

        <div class="form-group">
            @Html.LabelFor(model => model.GL_code, "GL Code", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.GL_code, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.GL_code, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.GL_description, "Gl Description", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.GL_description, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.GL_description, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.expense_type_ID, "Expense", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("expense_type_ID", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.expense_type_ID, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.eag, "Employee Account Group", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("eag", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.eag, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "gl_Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Here is my controller method:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit([Bind(Include = "ID,GL_code,GL_description,expense_type_ID,eag")] gl_code gl_code)
{
    if (ModelState.IsValid)
    {           
        db.Entry(gl_code).State = EntityState.Modified;
        await db.SaveChangesAsync();
        return RedirectToAction("gl_Index");      
    }
    ViewBag.eag = new SelectList(db.employee_account_group, "ID", "eag_name");
    ViewBag.expense_type_ID = new SelectList(db.expense_type, "ID", "type", gl_code.expense_type_ID);
    return View(gl_code);
}

When I debug it, I see the model being passed in is of value NULL. I am seeing this on the controller side at the the parameters part of the Edit method.

2 Answers 2

14

Its null because your model contains a property named gl_code and you have also named the parameter for your model gl_code in the POST method.

Change the name of one or the other and the model will bind correctly.

What is happening internally is that the form submits a name/value pair for each successful form control, in your case gl_code=someValue. The DefaultModelBinder first initializes a new instance of your model. It then reads the form values and finds a match for the property in your model and sets it to someValue. But it also finds a match in the method parameters and tries set the value of the parameter to someValue, which fails (because you cannot do gl_code gl_code = "someValue";) and the model becomes null.

Sign up to request clarification or add additional context in comments.

1 Comment

Unbelievable how I just spent an hour pulling my hair out because of this.
2

It appears you have a property on your view model called gl_code. In your controller, you also refer to the view model as gl_code.

Try changing this.

public async Task<ActionResult> Edit(gl_code gl_code)

To

public async Task<ActionResult> Edit(gl_code model)

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.