I apologize if this is a dumb question, but I am stumped. This is my first MVC application I am attempting, coming from web forms. I have the following models:
public class IndividualItem
{
[Required][Key]
public string SerialNumber { get; set; }
public virtual NSN NSNnumber { get; set; }
public string Location { get; set; }
public string User { get; set; }
}
and
public class NSN
{
[Required][DisplayName("NSN")][StringLength(13)][Key]
public string NSNnumber { get; set; }
[Required]
[StringLength(100)]
[DisplayName("NSN Description")]
public string Description { get; set; }
public virtual ICollection<IndividualItem> Items { get; set; }
}
And here is the View Model
public class IndividualItemNSNselectList
{
public SelectList NSNlist { get; set; }
public IndividualItem IndividualItem { get; set; }
}
Controller
public ActionResult Create()
{
var view = new IndividualItemNSNselectList();
view.NSNlist = new SelectList(db.NSNs.ToList(),"NSNnumber","Description");
return View(view);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "SerialNumber,Location,User")] IndividualItem individualItem)
{
if (ModelState.IsValid)
{
db.IndividualItem.Add(individualItem);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(individualItem);
}
and drop down in view
<div class="form-group">
@Html.LabelFor(model => model.IndividualItem.NSNnumber, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.IndividualItem.NSNnumber,Model.NSNlist, " ")
@Html.ValidationMessageFor(model => model.IndividualItem.NSNnumber, "", new { @class = "text-danger" })
</div>
</div>
When I try to make the create view for the IndividualItem everything works, including the population of the drop down list. However, when I post the NSN object is not being input to the database.
I have done a lot of researching that lead me to this point but I cannot figure out what is wrong. Thank you for you assistance, let me know if you need me to clarify anything.