NullReferenceException:对象引用未设置为对象的实例。没有道理

时间:2018-07-13 10:05:04

标签: c# asp.net asp.net-core

我的历史记录页面运行正常,但是现在当我尝试添加历史记录时,它并没有创建,并且给了我NullReferenceException: Object reference not set to an instance of an object.
 此行错误:

<h2>Canidate - @Model.Name</h2>

这是我的控制人:

 public IActionResult CandidateHistory(int Id)
    {

        using (var applicationcontext = new ApplicationContext())
        {
            var candidate = applicationcontext.Candidates.Where(s => s.Id.Equals(Id)).SingleOrDefault();
            if (candidate == null)
            {
                return NotFound();
            }

            return View(candidate);
        }
    }

    [HttpPost, ActionName("CandidateHistory")]
    [ValidateAntiForgeryToken]
    public IActionResult CandidateHistoryPost([Bind("Description, State, DateOfDescription, Saving, Interviewer, Feedback, FeedbackDate, Behavior,InterViewDate")]Candidate candidate, int Id)
    {
        try
        {

            using (var applicationContext = new ApplicationContext())
            {
                var candidates = applicationContext.Candidates.Where(s => s.Id.Equals(Id)).SingleOrDefault();   //Getting all the DATA from the Candidate with the Id passed in the Get Method
                candidates.DescriptionList.Add(candidate);
                applicationContext.Candidates.Add(candidates);
                applicationContext.SaveChanges();
                return RedirectToAction("CandidateHistory");
            }

        }
        catch (DbUpdateException ex)
        {
            //Log the error (uncomment ex variable name and write a log.
            ModelState.AddModelError("", "Unable to save changes. " + "Try again, and if the problem persists " + "see your system administrator.");
        }
        return View();
    }

我真的不知道为什么这会给我这个错误。

这也是我的观点:

    @model HCCBPOHR.Data.Candidate

 @{
   ViewData["State"] = "CandidateHistory";
 }
 <h2>Canidate - @Model.Name</h2>
 <label>History</label>
<hr />
<div class="panel panel-default">
<div class="panel-heading">
    <i class="fa fa-clock-o fa-fw"></i> History Of @Model.Name
</div>
<div class="panel-body">
    @foreach (var Description in Model.DescriptionList)
    {
        @if (Description.Saving == true)
        {
            <ul class="timeline">
                <li class="timeline">
                    <div class="timeline-badge">
                        <i class="fa fa-check"></i>
                    </div>
                    <div class="timeline-panel">
                        <div class="timeline-heading">
                            <h4 class="timeline-title">@Description.State</h4>
                            <p>
                                <small class="text-muted"><i class="fa fa-clock-o"></i> @Description.DateOfDescription.ToShortDateString()</small>
                            </p>
                        </div>
                        <div class="timeline-body">
                            <p><b>Description:</b> @Description.Description</p>
                        </div>
                        <div class="timeline-body">
                            <p><b>Interviewer:</b> @Description.Interviewer</p>
                        </div>
                        <div class="timeline-body">
                            <p><b>Feedback:</b> @Description.Feedback</p>
                        </div>
                        <div class="timeline-body">
                            <p><b>FeedbackDate:</b> @Description.FeedbackDate</p>
                        </div>
                        <div class="timeline-body">
                            <p> <b>Bad behavior:</b> @Description.Behavior</p>
                        </div>
                        <div class="timeline-body">
                            <p> <b>Applied For:</b> @Description.SelectedAd</p>
                        </div>
                    </div>
                </li>
            </ul>
        }
        @if (Description.Saving == false)
        {
            <ul class="timeline">
                <li class="timeline-inverted">
                    <div class="timeline-badge">
                        <i class="fa fa-check"></i>
                    </div>
                    <div class="timeline-panel">
                        <div class="timeline-heading">
                            <h4 class="timeline-title">@Description.State</h4>
                            <p>
                                <small class="text-muted"><i class="fa fa-clock-o"></i> @Description.DateOfDescription.ToShortDateString()</small>
                            </p>
                        </div>
                        <div class="timeline-body">
                            <p><b>Description:</b> @Description.Description</p>
                        </div>
                        <div class="timeline-body">
                            <p><b>Interviewer:</b> @Description.Interviewer</p>
                        </div>
                        <div class="timeline-body">
                            <p><b>Feedback:</b> @Description.Feedback</p>
                        </div>
                        <div class="timeline-body">
                            <p><b>FeedbackDate:</b> @Description.FeedbackDate</p>
                        </div>
                        <div class="timeline-body">
                            <p> <b>Bad behavior:</b> @Description.Behavior</p>
                        </div>
                        <div class="timeline-body">
                            <p> <b>Bad behavior:</b> @Description.InterViewDate</p>
                        </div>
                    </div>
                </li>
            </ul>
        }
    }
    </div>
 </div>
 <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@Model.Saving"> Add History Description</button>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-body">
            <form asp-action="CandidateHistory">
                <div asp-validation-summary="ModelOnly" class="text-danger"> 
    </div>
                 <div class="form-group">
                    <label>Estado</label>
                    <select asp-for="State" class="form-control">
                        <option value="FirstIntrerview">First Interview 
      </option>
                        <option value="SecoundInterview">Secound Interview</option>
                        <option value="Contract">Contract</option>
                    </select>
                </div>
                <div class="form-group">
                    <label asp-for="InterViewDate" class="control-label">Interview Date:</label>
                    <input asp-for="InterViewDate" class="form-control" value="" />
                    <span asp-validation-for="InterViewDate" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="Description" class="control-label">Description</label>
                    <input asp-for="Description" class="form-control" value="" />
                    <span asp-validation-for="Description" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label>Side of History</label>
                    <select asp-for="Saving" class="form-control">
                        <option value="false">Candidate </option>
                        <option value="true">Hitachi</option>
                    </select>
                </div>
                <div class="form-group">
                    <label>Interviewer</label>
                    <select asp-for="Interviewer" class="form-control">
                        <option value="Mananger">Mananger </option>
                        <option value="Diretora">Diretora</option>
                    </select>
                </div>
                <div class="form-group">
                    <label>FeedBack</label>
                    <select asp-for="Feedback" class="form-control">
                        <option value="Yes">Yes </option>
                        <option value="No">No</option>
                    </select>
                </div>
                <div class="form-group">
                    <label asp-for="FeedbackDate" class="control-label">Feedback Date:</label>
                    <input asp-for="FeedbackDate" class="form-control" value="" />
                    <span asp-validation-for="FeedbackDate" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label>Bad Behavior?</label>
                    <select asp-for="Behavior" class="form-control">
                        <option value="Yes">Yes </option>
                        <option value="No">No</option>
                    </select>
                </div>
                <div class="form-group">
                    <label asp-for="DateOfDescription" class="form-group">Date of the HistoryPost</label>
                    <input asp-for="DateOfDescription" class="form-group" />
                    <span asp-validation-for="DateOfDescription" class="text-danger"></span>
                </div>

                <div class="form-group">
                    <input type="submit" value="Create" class="btn btn-default" />
                </div>
            </form>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        </div>
    </div>
    </div>
 </div>

0 个答案:

没有答案