0
선택한 항목의 ID를 데이터베이스에 저장해야합니다. 하지만 드롭 다운에서 항목을 선택하면 항상 null 값을 얻습니다. 컨트롤러 : 여기 드롭 다운 목록에서 선택한 항목의 ID를 가져옵니다
몇 가지 코드public ActionResult Create()
{
SelectList CategoryList = new SelectList(dc.Category.ToList(), "ID", "CategoryName");
ViewData["Categories"] = CategoryList;
ViewData.Model = new AdvertModel();
return View();
}
보기 :
<%:Html.DropDownList("Categories", ViewData["Categories"] as SelectList, new { @class = "dropdown" })%>
모델 : AdvertModel
public class AdvertModel
{
public Int32 ID { get; set; }
[Required(AllowEmptyStrings=false,ErrorMessage="Please enter the title of your Ad.")]
[Display(Name="Title")]
public string Title { get; set; }
[Required(AllowEmptyStrings=false,ErrorMessage="Please enter a description of your Ad.")]
[Display(Name = "Details")]
public string Details { get; set; }
[Required(AllowEmptyStrings=false,ErrorMessage="Please enter when your Ad. will be publish")]
[Display(Name = "Publish date")]
[DataType(DataType.Date)]
public DateTime PubDate { get; set; }
[Required]
public DateTime EntryDate { get; set; }
public bool AdStatus { get; set; }
[Required]
[Display(Name = "Category")]
public Category Category { get; set; }
}
그리고 지금 내가 선택한 항목의 ID를 얻으려면 :
public ActionResult Create(AdvertModel ad)
{
Advert nAD = new Advert();
nAD.Title = ad.Title;
nAD.Message = ad.Details;
nAD.PublishDate = ad.PubDate;
nAD.Category = ad.Category.ID;// here I always get null.
dc.Advert.AddObject(nAD);
dc.SaveChanges();
return View(ad);
}
어떤 생각을 잘못하고 있니?
을 내 현재 코드에서이 "Category_ID"라고하는 것은 없습니다 !! – kandroid
Asp.Net MVC는 Category.ID를 Category_ID로 변환합니다 ... 시도해보십시오 – Martin
Nop..the "Category_ID"가 작동하지 않습니다. – kandroid