Hello in my app I have a dropdown list
in my controller I use this
public ActionResult Create()
{
webdata db = new webdata();
ViewBag.annCategories = new SelectList(db.annCategories, "kind", "an_kindtext");
return View();
}
EDIT in my HttpPost I have
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ann_ID,Pubdate,kind,title")] announcements announcements)
{
if (ModelState.IsValid)
{
db.announcements.Add(announcements);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(announcements);
}
and in my view I have this
@Html.DropDownList("annCategories",null, new { id = "annCategories" })
I see the values in my dropdown list, when I click the create button a new record is stored in the database. But it always saves the "Select a category" which is the first option. Can someone help with this? thank you
EDIT
My announcement model
public class announcements
{
[Key]
[Display(Name = "ID")]
public int ann_ID { get; set; }
[Display(Name = "Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
public DateTime Pubdate { get; set; }
[Display(Name = "Category")]
public int kind { get; set; }
[Display(Name = "title")]
public String title { get; set; }
}
My announcement categories model
public class annCategories
{
[Key]
[Display(Name = "Category")]
public int kind { get; set; }
[Display(Name = "Description")]
public String an_kindtext { get; set; }
}