【问题标题】:Object reference not set to an instance of an object. mvc post你调用的对象是空的。 mvc 帖子
【发布时间】:2014-08-26 22:24:40
【问题描述】:

当我单击更新按钮时,我得到了 TestCaseModel md 的空值。我无法找到我做错的地方。 (我正在尝试获取组合框的选定值)

我的模特

   public class TestCaseModel
    {
       public Product pro  {get;set;}
       public List<ProductCatagory> lst { get; set; }

    }

    public class Product {

        public string Name { get; set; }
        public string Description { get; set; }


    }


    public class ProductCatagory {

        public string ID { get; set; }
        public string Name { get; set; }

    }

我的控制器

    public ActionResult Index()
    {

        TestCaseModel md = new TestCaseModel();
        md.lst = new List<ProductCatagory> {
            new ProductCatagory { ID="1",Name="science book"}, 
            new ProductCatagory {ID="2",Name="cartoon book"} 
        };

        md.pro = new Product {Description="here is the des.",Name="Book" };

        return View(md);
    }



    [HttpPost]
    public ActionResult Update(TestCaseModel md)
    {


        X.Msg.Notify(new NotificationConfig
        {
            Icon = Icon.Accept,
            Title = "Working",
            Html = md.pro.Name+">>"
        }).Show();


        return this.Direct();
    }

我的看法

  @Html.X().ResourceManager()

   @Html.X().TextFieldFor(m=>m.pro.Name).Flex(1).FieldLabel("ID").Value(@Model.pro.Name)

     @Html.X().ComboBox().FieldLabel("Urun katagori").Flex(1).Store(Html.X().Store().ID("Store41")
    .Model(Html.X().Model().Fields(new ModelField("ID"), new ModelField("Name"))).DataSource(@Model.lst)
    ).ValueField("ID").DisplayField("Name").Value(@Model.pro.Name)


      @Html.X().Button().Text("Update").Icon(Icon.PageSave).DirectClickAction("Update","TestCase")

【问题讨论】:

  • 请发布堆栈跟踪

标签: asp.net-mvc ext.net


【解决方案1】:

如果没有堆栈跟踪,很难说代码有什么问题;

但是,鉴于您到目前为止所展示的内容,我想说,如果您怀疑任何类是问题,那么您应该尝试在构造函数中初始化类中的所有值。

尝试以下方法:

public class TestCaseModel
{
   public Product pro  {get;set;}
   public List<ProductCatagory> lst { get; set; }

   public TestCaseModel(){
      pro = String.Empty;
      lst = new List<ProductCatagory>();
   }
}

public class Product {

    public string Name { get; set; }
    public string Description { get; set; }

    public Product(){
        Name = String.Empty;
        Description = String.Empty;
    }
}


public class ProductCatagory {

    public string ID { get; set; }
    public string Name { get; set; }

    public ProductCatagory (){
        ID = String.Empty
        Name = String.Empty;
    }
}

【讨论】:

  • 始终解释代码为何有效以及与原始代码相比有何不同。
  • 感谢您的建议!
  • 不,我认为问题不是通过构造初始化
  • @sakir,你定义了更新控制器动作了吗?您的项目中的控制器名称是否为“TestCase”?您是否更改了 .DirectClickAction?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-27
  • 2013-07-22
相关资源
最近更新 更多