sakir
sakir

Reputation: 3502

Object reference not set to an instance of an object. mvc post

When I click the update button ,I am getting the null value of TestCaseModel md.I couldnt get the where I am doing wrong. (I am trying to get the selected value of the combobox)

my model

   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; }

    }

my controler

    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();
    }

and my view

  @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")

Upvotes: 0

Views: 186

Answers (1)

Aelphaeis
Aelphaeis

Reputation: 2613

Its hard to really say whats wrong with the code without the stack trace;

however, given what you've shown thus far, I'd say that if you suspect that any of the classes are the problem then you should try initializing all the values in the classes in the constructor.

Try the following:

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;
    }
}

Upvotes: 1

Related Questions