Я создаю приложение MVC 3, которое содержит данные о «партнерах», однако мне также нужно иметь возможность удалять эти «партнеры» через приложение.Я использую Delete ActionResult, но каждый раз, когда я пытаюсь удалить запись, я получаю следующую ошибку сервера:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 14: <div class="form-grid-1">
Line 15: <div class="display-label">@Html.LabelFor(model => model.FirstName)</div>
Line 16: <div class="display-field">@Model.FirstName</div>
Line 17: </div>
Line 18:
Source File: c:\Users\Jackie\Documents\Visual Studio 2010\Projects\Associate Tracker versions\Associate Tracker v5\LoginFormExample\Views\Associate\Delete.cshtml Line: 16
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
ASP._Page_Views_Associate_Delete_cshtml.Execute() in c:\Users\Jaskharan Shoker\Documents\Visual Studio 2010\Projects\Associate Tracker versions\Associate Tracker v5\LoginFormExample\Views\Associate\Delete.cshtml:16
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +207
System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +81
System.Web.WebPages.StartPage.RunPage() +19
System.Web.WebPages.StartPage.ExecutePageHierarchy() +65
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76
System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +220
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +115
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +303
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +23
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +260
System.Web.Mvc.<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b() +19
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +177
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
System.Web.Mvc.Controller.ExecuteCore() +116
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8969117
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
Контроллер:
//
// GET: /Associate/Delete/AssociateId
public ActionResult Delete(int id)
{
using (var db = new AssociateDBEntities())
{
return View(db.Associates.Find(id));
}
}
//
// POST: /Associate/Delete/AssociateId
[HttpPost]
public ActionResult Delete(int id, Associate associate)
{
try
{
using (var db = new AssociateDBEntities())
{
db.Entry(associate).State = System.Data.EntityState.Deleted;
db.SaveChanges();
}
return RedirectToAction("ViewAll");
}
catch
{
return View();
}
Представление «Удалить»:
@model LoginFormExample.Models.Associate
@{
ViewBag.Title = "Delete";
}
<h2>Delete</h2>
<h3>Are you sure you want to delete this associate?</h3>
<fieldset>
<legend>Associate</legend>
@Html.HiddenFor(model => model.AssociateId)
<div class="form-grid-1">
<div class="display-label">@Html.LabelFor(model => model.FirstName)</div>
<div class="display-field">@Model.FirstName</div>
</div>
<div class="form-grid-2">
<div class="display-label">@Html.LabelFor(model => model.LastName)</div>
<div class="display-field">@Model.LastName</div>
</div>
<div class="form-grid-1">
<div class="display-label">@Html.LabelFor(model => model.Email)</div>
<div class="display-field">@Model.Email</div>
</div>
<div class="form-grid-2">
<div class="display-label">@Html.LabelFor(model => model.AddressLine1) </div>
<div class="display-field">@Model.AddressLine1</div>
</div>
<div class="form-grid-1">
<div class="display-label">@Html.LabelFor(model => model.AddressLine2)</div>
<div class="display-field">@Model.AddressLine2</div>
</div>
<div class="form-grid-2">
<div class="display-label">@Html.LabelFor(model => model.Postcode)</div>
<div class="display-field">@Model.Postcode</div>
</div>
<div class="form-grid-1">
<div class="display-label">@Html.LabelFor(model => model.Phone)</div>
<div class="display-field">@Model.Phone</div>
</div>
<div class="form-grid-2">
<div class="display-label">@Html.LabelFor(model => model.Mobile)</div>
<div class="display-field">@Model.Mobile</div>
</div>
<br />
</fieldset>
@using (Html.BeginForm()) {
<p>
<input type="submit" value="Delete" /> |
@Html.ActionLink("Back to view all", "ViewAll")
</p>
}
Класс «Ассоциированный»
namespace LoginFormExample.Models
{
public partial class Associate
{
public int AssociateId { get; set; }
[Required]
[Display(Name = "First name:")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last name:")]
public string LastName { get; set; }
[Required(ErrorMessage = "Please enter a valid email address")]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email address:")]
public string Email { get; set; }
[Display(Name = "Address line 1:")]
public string AddressLine1 { get; set; }
[Display(Name = "Address line 2:")]
public string AddressLine2 { get; set; }
public int RegionId { get; set; }
public int CityTownId { get; set; }
[Display(Name = "Postcode:")]
public string Postcode { get; set; }
[Display(Name = "Phone number:")]
public string Phone { get; set; }
[Display(Name = "Mobile number:")]
public string Mobile { get; set; }
У кого-нибудь есть идеи относительно того, почему я продолжаю получать этоошибка?Я проверил примеры приложений, и я не вижу каких-либо различий.