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.
Here is an imaging showing my three tables:

Here is my attempt to create a ViewModel to be used to add new alternate codes to items:
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<web2_item_brands> web2_item_brands { get; set; }
[ForeignKey("inv_mast_uid")]
public inv_mast inv_mast { get; set; }
public AlternateCodeViewModel()
{
Delete_Flag = "N";
Exact_Flag = "Y";
}
}
Here is my controller method:
[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();
}
If it helps here is an image showing my newCode object and it's missing inv_mast_uid property:
EDIT: As suggested, the following additional information has been added
My controller that initializes the view:
ublic ActionResult AddAltCode()
{
ViewBag.brandsdd = new SelectList(db.web2_item_brands.OrderBy(x => x.brand_name), "item_brand_uid", "brand_name");
return View();
}
My view:
@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";
}
<h2>Add Alternate Code</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.item_id, "Item ID", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.item_id, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.item_id, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.web2_item_brands, "Brand Name", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Alternate_code_brand_id", new SelectList(ViewBag.brandsdd, "Value", "Text") , htmlAttributes: new { @class = "form-control" })
@*@Html.ValidationMessageFor(model => model.web2_item_brands, "", new { @class = "text-danger" })*@
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Alternate_Code_Item_ID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Alternate_Code_Item_ID, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Alternate_Code_Item_ID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Add" class="btn btn-default" />
</div>
</div>
</div>
}

