Я столкнулся со странной проблемой .... когда я использую UpdateModel()
или TryUpdateModel()
, все работает нормально. Когда я пытаюсь связать себя (например, MyObject.FirstName = collection["FirstName"]
), я получаю ошибку "Object reference not set to an instance of an object"
.
Это немного сложно объяснить, поэтому я представлю код:
[HandleError]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection collection)
{
try
{
Model.Event evnt = new Redline.RedlineTimeAttack.Model.Event();
//When this is uncommented everything works fine.
//TryUpdateModel<Model.Event>(evnt);
//this will eventually lead to problems
evnt.Description = collection["Description"];
evnt.EndDate = enddate;
evnt.EventName = collection["EventName"];
evnt.IsActive = collection["IsActive"].Contains("true");
evnt.StartDate = startdate;
evnt.TrackId = trackId;
evnt.WebContent = collection["WebContent"];
if (!evnt.IsValid)
{
foreach (var error in evnt.GetRuleViolations())
{
ModelState.AddModelError(error.PropertyName, error.ErrorMessage);
}
}
//If there are no validation issues then no problem, redirecttoaction
//works properly
if (ModelState.IsValid)
{
model.Events.InsertOnSubmit(evnt);
model.SubmitChanges();
ViewData["ControlMode"] = "Edit";
return RedirectToAction("Edit");
}
else //returning to View so that user can correct issues causes a null reference error in the view (bombs at first Html.Textbox("ControlName"))
{
ViewData["Tracks"] = GetTracks();
return View("Create", evnt);
}
}
Вот трассировка стека:
System.NullReferenceException was unhandled by user code
Message="Object reference not set to an instance of an object."
Source="System.Web.Mvc"
StackTrace:
at System.Web.Mvc.HtmlHelper.GetModelStateValue(String key, Type destinationType)
at System.Web.Mvc.Html.InputExtensions.InputHelper(HtmlHelper htmlHelper, InputType inputType, String name, Object value, Boolean useViewData, Boolean isChecked, Boolean setId, Boolean isExplicitValue, IDictionary`2 htmlAttributes)
at System.Web.Mvc.Html.InputExtensions.TextBox(HtmlHelper htmlHelper, String name, Object value, IDictionary`2 htmlAttributes)
at System.Web.Mvc.Html.InputExtensions.TextBox(HtmlHelper htmlHelper, String name)
at ASP.views_event_create_aspx.__RenderContent2(HtmlTextWriter __w, Control parameterContainer) in d:\TFSProjects\Redline Time Attack\Main\Source\Redline.RedlineTimeAttack.Web\Views\Event\Create.aspx:line 18
at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
at System.Web.UI.Control.Render(HtmlTextWriter writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
at ASP.views_shared_site_master.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in d:\TFSProjects\Redline Time Attack\Main\Source\Redline.RedlineTimeAttack.Web\Views\Shared\Site.Master:line 29
at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
at System.Web.UI.Control.Render(HtmlTextWriter writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
at System.Web.UI.Page.Render(HtmlTextWriter writer)
at System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException: