title: Passing multiple class objects to razor view through controller in mvc, asp.net
body: <p>I am trying to perform CRUD operations on two classes <code>PurchaseDetail</code> and <code>ItemDetail</code>. Made <code>PurchaseDetail</code> a base class and <code>ItemDetail</code> as child class. So on a single razor view I am passing the child class object. Now when I'm performing CRUD operation on child class everything works fine. But when trying the same with base class I'm getting null object while adding data by calling a create(ItemDetail obj), on HttpPost.</p>
  
  <p>Any solution for this? Is it a good practice to pass multiple classes to a view by using inheritance?</p>
  
  <p>Edit
  Elaboration:</p>
  
  <pre><code>public class PurchaseDetail
  {
      public int ID { get; set; }
  
      [Required]
      [Display(Name = "PPRF Name")]
      public string pprfName { get; set; }
      public int DepartmentID { get; set; }
  
      [Display(Name ="Department")]
      public string DepartmentName { get; set; }
      public int DepartmentSerial { get; set; }
      public List&lt;SelectListItem&gt; MaterialTypes { get; set; }
  </code></pre>
  
  <p>Above class is my base class with few properties. Then Below is my child class.</p>
  
  <pre><code>public class ItemDetail: PurchaseDetail
  {
      public int ItemID { get; set; }
      public int ItemCategoryID { get; set; }
      public string ItemCategoryName { get; set; }
      public int ProductID { get; set; }
  </code></pre>
  
  <p>If I would take both in different pages there was not any problem. But I want to pass both class onto same view so I went for Inheritance. I am popping up form for ItemDetail from the same view, to add an item.</p>
  
  <pre><code>@model AIIMSINTRANET.Models.ItemDetail
  </code></pre>
  
  <p>Firstly I am inserting ItemDetail by using Ajax call. Here things are working fine. </p>
  
  <pre><code>public JsonResult Add(ItemDetail item)
  { 
      //Insert Logic
  }
  </code></pre>
  
  <p>After this I want to insert PurchaseDetail in it's respective table. Since the view was same so every fields like TextBox, Dropdowns were filled while calling this method.</p>
  
  <pre><code> public ActionResult Create()
      {
          ItemDetail obj = new ItemDetail(); 
  
          obj.DeptIndentNo = GetIndentNumber2();
          obj.IndentDate = DateTime.Now.Date;
          obj.MaterialTypes = PurchaseProposalDO.PopulateMaterialType();
          //Initialized all other fields 
       }
  </code></pre>
  
  <p>Even fields like DropDown of ItemDetail class got populated during above function call. Now when I am submitting the form to Insert PurchaseDetail, it calls below function:</p>
  
  <pre><code>    [HttpPost]
      public ActionResult Create(ItemDetail obj)
      {
          // Insert Logic Here
      }
  </code></pre>
  
  <p>Now here is my problem. Why am I getting obj as null ?
  Even when I am inputting everything.</p>
  
  <pre><code>&lt;div class="form-group"&gt;
          @Html.LabelFor(model =&gt;model.pprfName, htmlAttributes: new { @class = "control-label col-md-2" })
          &lt;div class="col-md-10"&gt;
              @Html.EditorFor(model =&gt;model.pprfName, new { htmlAttributes = new { @class = "form-control" } })
              @Html.ValidationMessageFor(model =&gt; model.pprfName, "", new { @class = "text-danger" })
          &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>Please help.</p>