title: ASP.NET MVC: How to use linq lambda expression with a ViewModel to get data?
body: <p>How to use a linq expression  to use a ViewModel to get data from from one of it's domain models? When I try to get the inv_mast_uid that is associated to the item_id the user enters, I only get a 0 and not a unique integer like 10345. I do get all other necessary data from the ViewModel.</p>
  
  <p>Here is an imaging showing my three tables:
  <a href="https://i.stack.imgur.com/GIFh0.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GIFh0.jpg" alt="enter image description here"></a></p>
  
  <p>Here is my attempt to create a ViewModel to be used to add new alternate codes to items:</p>
  
  <pre><code>public class AlternateCodeViewModel
  {
  
  
      [Key]
      [Column(Order = 0)]
      public int inv_mast_uid { get; set; }
  
      [Key]
      [Column(Order = 1)]
      [StringLength(50)]
      [Display(Name = "Alternate Code")]
      public string Alternate_Code_Item_ID { get; set; }
  
      [Key]
      [Column(Order = 2)]
      public int Alternate_Code_Brand_ID { get; set; }
  
      [Required]
      [StringLength(1)]
      [Display(Name = "Exact Flag")]
      public string Exact_Flag { get; set; }
  
      [Required]
      [StringLength(1)]
      [Display(Name = "Delete Flag")]
      public string Delete_Flag { get; set; }
  
      public string item_id { get; set; }
  
  
      public IEnumerable&lt;web2_item_brands&gt; web2_item_brands { get; set; }
  
      [ForeignKey("inv_mast_uid")]
      public inv_mast inv_mast { get; set; }
  
      public AlternateCodeViewModel()
      {
          Delete_Flag = "N";
          Exact_Flag = "Y";
      }
  }
  </code></pre>
  
  <p>Here is my controller method:</p>
  
  <pre><code>[HttpPost]
      [ValidateAntiForgeryToken]
      public ActionResult AddAltCode(AlternateCodeViewModel avm)
      {
          if (ModelState.IsValid)
          {
  
              var newCode = new web2_item_alternate_code
              {
                  inv_mast_uid = avm.inv_mast_uid,
                  Alternate_Code_Item_ID = avm.Alternate_Code_Item_ID,
                  Alternate_Code_Brand_ID = avm.Alternate_Code_Brand_ID,
                  Delete_Flag = avm.Delete_Flag,
                  Exact_Flag = avm.Exact_Flag
              };
  
              db.web2_item_alternate_code.Add(newCode);
              db.SaveChanges();
              return RedirectToAction("AltCodeIndex");
          }
          return View();
      }
  </code></pre>
  
  <p>If it helps here is an image showing my newCode object and it's missing inv_mast_uid property:</p>
  
  <p><a href="https://i.stack.imgur.com/TgMg1.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TgMg1.jpg" alt="enter image description here"></a></p>
  
  <h2>EDIT: As suggested, the following additional information has been added</h2>
  
  <p>My controller that initializes the view:</p>
  
  <pre><code>ublic ActionResult AddAltCode()
      {    
          ViewBag.brandsdd = new SelectList(db.web2_item_brands.OrderBy(x =&gt; x.brand_name), "item_brand_uid", "brand_name");
          return View();
      }
  </code></pre>
  
  <p>My view:</p>
  
  <pre><code>@using System.Web.Mvc.Html
  @using System.Linq;
  @model Non_P21_Quote_System_v1._0.ViewModels.AlternateCodeViewModel
  
  @{
      ViewBag.Title = "AddAltCode";
      Layout = "~/Views/Shared/_Layout.cshtml";
  }
  
  &lt;h2&gt;Add Alternate Code&lt;/h2&gt;
  
  
  @using (Html.BeginForm())
  {
      @Html.AntiForgeryToken()
  
      &lt;div class="form-horizontal"&gt;
  
      &lt;hr /&gt;
      @Html.ValidationSummary(true, "", new { @class = "text-danger" })
  
  
      &lt;div class="form-group"&gt;
          @Html.LabelFor(model =&gt; model.item_id, "Item ID", htmlAttributes: new { @class = "control-label col-md-2" })
          &lt;div class="col-md-10"&gt;
              @Html.EditorFor(model =&gt; model.item_id, new { htmlAttributes = new { @class = "form-control" } })
              @Html.ValidationMessageFor(model =&gt; model.item_id, "", new { @class = "text-danger" })
          &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;div class="form-group"&gt;
          @Html.LabelFor(model =&gt; model.web2_item_brands, "Brand Name", htmlAttributes: new { @class = "control-label col-md-2" })
          &lt;div class="col-md-10"&gt;
              @Html.DropDownList("Alternate_code_brand_id", new SelectList(ViewBag.brandsdd, "Value", "Text") , htmlAttributes: new { @class = "form-control" })
                  @*@Html.ValidationMessageFor(model =&gt; model.web2_item_brands, "", new { @class = "text-danger" })*@
          &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;div class="form-group"&gt;
          @Html.LabelFor(model =&gt; model.Alternate_Code_Item_ID, htmlAttributes: new { @class = "control-label col-md-2" })
          &lt;div class="col-md-10"&gt;
              @Html.EditorFor(model =&gt; model.Alternate_Code_Item_ID, new { htmlAttributes = new { @class = "form-control" } })
              @Html.ValidationMessageFor(model =&gt; model.Alternate_Code_Item_ID, "", new { @class = "text-danger" })
          &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;div class="form-group"&gt;
          &lt;div class="col-md-offset-2 col-md-10"&gt;
              &lt;input type="submit" value="Add" class="btn btn-default" /&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>}</p>
  
  <p>An image of my view:
  <a href="https://i.stack.imgur.com/9jAWB.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9jAWB.jpg" alt="enter image description here"></a></p>