开发者

Object reference not set to an instance of an object - form object is null?

I'm caught up on this error and beating my head - it seemed like it would be a real form so this error (and my inability to resolve it) has me going insane.

I am simply trying to update 1 field in a table. The field being updated may or may not be null.

I'm getting an error in the controller. The 'ilpCareerGoal' object is coming back null and causes the error.

Controller:

[Authorize]
    public ActionResult editCareerGoal(int emplID)
    {
        ilpCareerGoal careerGoal = qService.getCareerGoal(emplID);
        return View(careerGoal);
    }

[Authorize]
    [Http开发者_运维问答Post]
    public ActionResult editCareerGoal(ilpCareerGoal careerGoal)
    {
        try
        {
            qService.editCareerGoal(careerGoal);
            return RedirectToAction("Index", "Home");
        }
        catch
        {               
            throw;
        }

    }

View:

@model ILP.Models.ilpCareerGoal

@{
ViewBag.Title = "editCareerGoal";
}

<h2>Edit Your Career Goal</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm("editCareerGoal", "Home", FormMethod.Post, new { id = "careerGoalForm" }))
{
@Html.ValidationSummary(false)
<fieldset>
    <legend>Career Goal</legend>
    <div class="editor-label">
        @Html.HiddenFor(model => model.emplID)
        Record information here about what you're striving for professionally.
    </div>
    <div class="editor-field">
        @Html.TextAreaFor(model => model.careerGoal, new { cols = "90", rows = "15" })
    </div>

    <p>
        <input type="submit" value="Update" />
    </p>
</fieldset>
}

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

When I debug I see that 'careerGoal' in my controller is null... I appreciate any help here!

Update - maybe it's a model thing? I actually have this field in a different model... here's my model code for the update just in case....

Model:

public bool editCareerGoal(ilpCareerGoal tcareerGoal)
        {
            employeeDataClassesDataContext careerGoals = new employeeDataClassesDataContext();
            ilpCareerGoal careerGoal;

            try
            {                   
                careerGoal = careerGoals.ilpCareerGoals.Single(c => c.emplID == tcareerGoal.emplID);
                careerGoal.careerGoal = tcareerGoal.careerGoal;
                careerGoals.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }


It's possible that the name collision between the property careerGoal in the ilpCareerGoal class and the variable named careerGoal of type ilpCareerGoal in your editCareerGoal action is causing the DefaultModelBinder to have a nervous breakdown.

Try changing the name of the variable in your action to something different, such as careerGoalModel.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜