如何dropdownlist
在我的视图中插入一个类别?
我有模特
public class IdeaModel
{
[Required]
[Display(Name = "Idea tilte")]
[StringLength(250, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
public string IdeaTilte { get; set; }
[Required]
[Display(Name = "CategoryName")]
public string CategoryName { get; set; }
[Required]
[Display(Name = "CategoryID")]
public int CategoryID { get; set; }
[Display(Name = "Main content")]
[DataType(DataType.MultilineText)]
[StringLength(1000, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 30)]
public string Content { get; set; }
}
我的观点(我想要一个下拉列表,而不是 TextBoxFor CategoryName 和 CategoryID)
@model TakesCare.Models.IdeaModel
<section id="">
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary()
@ViewBag.Status
<fieldset>
<legend>Add an idea</legend>
<ol>
<li>
@Html.LabelFor(m => m.IdeaTilte)
@Html.TextBoxFor(m => m.IdeaTilte)
</li>
//one dropdownlist, instead of TextBoxFor CategoryName and CategoryID
<li>
@Html.LabelFor(m => m.CategoryName)
@Html.TextBoxFor(m => m.CategoryName)
</li>
<li>
@Html.LabelFor(m => m.CategoryID)
@Html.TextBoxFor(m => m.CategoryID)
</li>*@
<li>
@Html.LabelFor(m => m.Content)
@Html.EditorFor(m => m.Content)
</li>
</ol>
<input type="submit" value="AddIdea" />
</fieldset>
}
</section>
我有类别:
“猫 1”,id=1
“Cat2”,id=2
“Cat3”,id=3
我的第一个问题是——我应该在哪里存储这些数据?(类别名称和 ID)
在我的IdeaModel
?
第二个问题是-如何在dropdownlist
我的所有类别中显示-然后,如何从中获取 HttpPost 中的 ID 值dropdownlist
?什么是最简单的解决方案?