未将对象引用设置到对象&QUOT的实例

未将对象引用设置到对象&QUOT的实例

本文介绍了ASP.NET MVC - Html.Textbox()抛出"未将对象引用设置到对象&QUOT的实例;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个奇怪的问题....当我使用的UpdateModel() TryUpdateModel() ,一切工作正常。当我尝试结合自己(如 MyObject.FirstName =收集[名字] ),我得到未设置为实例的对象引用对象错误。

这是一个有点难以解释,所以我会present的code:

  [的HandleError]
    的[AcceptVerbs(HttpVerbs.Post)
    公众的ActionResult创建(的FormCollection集合)
    {
        尝试
        {
            Model.Event EVNT =新Redline.RedlineTimeAttack.Model.Event();            //如果这是注释掉一切工作正常。
            //TryUpdateModel<Model.Event>(evnt);            //这将最终导致问题
            evnt.Description =收集[说明];
            evnt.EndDate =结束日期;
            evnt.EventName =收集[事件名称];
            。evnt.IsActive =收集[IsActive]包含(真);
            evnt.StartDate = STARTDATE;
            evnt.TrackId =的TrackID;
            evnt.WebContent =收集[的WebContent];
            如果(!evnt.IsValid)
            {
              的foreach(在evnt.GetRuleViolations)VAR误差()
              {
                ModelState.AddModelError(error.PropertyName,error.ErrorMessage);
              }
            }            //如果没有验证问题的话没问题,redirecttoaction
            //工作正常
            如果(ModelState.IsValid)
            {
                model.Events.InsertOnSubmit(EVNT);
                model.SubmitChanges();
                计算机[ControlMode] =编辑;
                返回RedirectToAction(编辑);
            }
            否则//返回查看,以便用户可以纠正问题导致空引用错误的观点(在第一次Html.Textbox炸弹(控件名称))
            {
                计算机[轨道] = GetTracks();
                返回视图(创建,EVNT);
            }
        }

下面的堆栈跟踪:

  System.NullReferenceException是由用户code未处理
消息=对象引用不设置到对象的实例。
来源=System.Web.Mvc
堆栈跟踪:
   在System.Web.Mvc.HtmlHelper.GetModelStateValue(字符串键,类型destinationType)
   在System.Web.Mvc.Html.InputExtensions.InputHelper(的HtmlHelper的HtmlHelper,inputType下inputType下,字符串名称,对象的值,布尔useViewData,布尔器isChecked,布尔SETID,布尔isExplicitValue,IDictionary`2 htmlAttributes)
   在System.Web.Mvc.Html.InputExtensions.TextBox(的HtmlHelper的HtmlHelper,字符串名称,对象的值,IDictionary`2 htmlAttributes)
   在System.Web.Mvc.Html.InputExtensions.TextBox(的HtmlHelper的HtmlHelper,字符串名称)
   在ASP.views_event_create_aspx .__ RenderContent2(HtmlTextWriter的__w,控制parameterContainer)在d:\\ TFSProjects \\红线时间攻击\\ MAIN \\来源\\ Redline.RedlineTimeAttack.Web \\查看\\事件\\ Create.aspx:行18
   在System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter的作家,儿童ICollection的)
   在System.Web.UI.Control.RenderChildren(HtmlTextWriter的作家)
   在System.Web.UI.Control.Render(HtmlTextWriter的作家)
   在System.Web.UI.Control.RenderControlInternal(HtmlTextWriter的作家的ControlAdapter适配器)
   在System.Web.UI.Control.RenderControl(HtmlTextWriter的作家的ControlAdapter适配器)
   在System.Web.UI.Control.RenderControl(HtmlTextWriter的作家)
   在ASP.views_shared_site_master .__ Render__control1(HtmlTextWriter的__w,控制parameterContainer)在d:\\ TFSProjects \\红线时间攻击\\ MAIN \\来源\\ Redline.RedlineTimeAttack.Web \\查看\\共享\\的Site.Master:29行
   在System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter的作家,儿童ICollection的)
   在System.Web.UI.Control.RenderChildren(HtmlTextWriter的作家)
   在System.Web.UI.Control.Render(HtmlTextWriter的作家)
   在System.Web.UI.Control.RenderControlInternal(HtmlTextWriter的作家的ControlAdapter适配器)
   在System.Web.UI.Control.RenderControl(HtmlTextWriter的作家的ControlAdapter适配器)
   在System.Web.UI.Control.RenderControl(HtmlTextWriter的作家)
   在System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter的作家,儿童ICollection的)
   在System.Web.UI.Control.RenderChildren(HtmlTextWriter的作家)
   在System.Web.UI.Page.Render(HtmlTextWriter的作家)
   在System.Web.Mvc.ViewPage.Render(HtmlTextWriter的作家)
   在System.Web.UI.Control.RenderControlInternal(HtmlTextWriter的作家的ControlAdapter适配器)
   在System.Web.UI.Control.RenderControl(HtmlTextWriter的作家的ControlAdapter适配器)
   在System.Web.UI.Control.RenderControl(HtmlTextWriter的作家)
   在System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint)
的InnerException:


解决方案

我发现了一些有识之士在这里:http://forums.asp.net/p/1396019/3006051.aspx

So I updated my code like so:

ModelState.SetModelValue("Description", new ValueProviderResult(ValueProvider["Description"].AttemptedValue, collection["Description"], System.Globalization.CultureInfo.CurrentCulture));
ModelState.SetModelValue("EventName", new ValueProviderResult(ValueProvider["EventName"].AttemptedValue, collection["EventName"], System.Globalization.CultureInfo.CurrentCulture));
ModelState.SetModelValue("EndDate", new ValueProviderResult(ValueProvider["EndDate"].AttemptedValue, collection["EndDate"], System.Globalization.CultureInfo.CurrentCulture));
ModelState.SetModelValue("StartDate", new ValueProviderResult(ValueProvider["StartDate"].AttemptedValue, collection["StartDate"], System.Globalization.CultureInfo.CurrentCulture));
ModelState.SetModelValue("TrackId", new ValueProviderResult(ValueProvider["TrackId"].AttemptedValue, collection["TrackId"], System.Globalization.CultureInfo.CurrentCulture));
ModelState.SetModelValue("WebContent", new ValueProviderResult(ValueProvider["WebContent"].AttemptedValue, collection["WebContent"], System.Globalization.CultureInfo.CurrentCulture));

The reason I am doing this is because I wanted to a. have all (or as much as possible) validation done in my Business Object, including required fields, and b. I wanted my own messages in the validation summary (e.g. "FieldX is a required field." instead of "A value is required."). If there's a better way to do this, please see my other question: http://stackoverflow.com/questions/646270/asp-net-mvc-custom-validation-message-for-value-types

这篇关于ASP.NET MVC - Html.Textbox()抛出&QUOT;未将对象引用设置到对象&QUOT的实例;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 02:24