Asp.net mvc 4 在ASP.NET MVC 4中填充和选择下拉列表值

Asp.net mvc 4 在ASP.NET MVC 4中填充和选择下拉列表值,asp.net-mvc-4,Asp.net Mvc 4,ASP.NET MVC中的下拉列表让我感到困惑。我有一个模型班。模型类有一个名为SelectedValue的字符串。此字符串表示以前选择的值。我有一个从数据库中提取的项目集合。集合的每个项都有一个ID和一个名称值。我的问题是,我不知道将这些信息导入UI的推荐方法是什么 我不确定是应该使用ViewBag还是模型。但是,一旦值存在,我甚至不确定填充UI的最佳方式是什么。我见过HTML助手和使用RAZOR语法的人。我很困惑。人们推荐什么 谢谢我这样做的方式是将下拉列表作为我的ViewModel的一部分

ASP.NET MVC中的下拉列表让我感到困惑。我有一个模型班。模型类有一个名为SelectedValue的字符串。此字符串表示以前选择的值。我有一个从数据库中提取的项目集合。集合的每个项都有一个ID和一个名称值。我的问题是,我不知道将这些信息导入UI的推荐方法是什么

我不确定是应该使用ViewBag还是模型。但是,一旦值存在,我甚至不确定填充UI的最佳方式是什么。我见过HTML助手和使用RAZOR语法的人。我很困惑。人们推荐什么


谢谢

我这样做的方式是将下拉列表作为我的ViewModel的一部分。。。。说它是产品。。。。因此,在我的ViewModel中,我有一个

public SelectList Products { get; set;}
public int ProductId { get; set; }  // For the the selected Product
因此,当视图加载时,我填充模型以返回控制器中的视图,我会执行以下操作:

model.Products = new SelectList(MyEnumerableofProducts, "ValueField", "TextField", {value of the selected item in the dropdown, as a string)
@{
ViewBag.Title = "Create";
 }

<h2>Create</h2>

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)

<fieldset>
    <legend>Player</legend>

    <div class="editor-label">
            @Html.LabelFor(model => model.TeamId, "Pick Your Team")
    </div>
    <div class="editor-field">
        @Html.DropDownList("TeamId", String.Empty)
        @Html.ValidationMessageFor(model => model.TeamId)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.FirstName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.FirstName)
        @Html.ValidationMessageFor(model => model.FirstName)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.LastName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.LastName)
        @Html.ValidationMessageFor(model => model.LastName)
    </div>        

    <p>
        <input type="submit" value="Create" />
    </p>
  </fieldset>
 }

 <div>
     @Html.ActionLink("Back to List", "Index")
 </div>

 @section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
在我看来,我会:

@Html.DropDownListFor(model => model.ProductId, Model.Products, "Please Select")



return View(model)

我就是这样做的,假设您有两个模型
团队和玩家

Player.cs

public class Player
{
    [HiddenInput(DisplayValue = false)]
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }        

    [Required]
    [ForeignKey("Team")]
    [Display(Name = "Team")]
    public int TeamId { get; set; }        

    [Display(Name = "First name")]
    public string FirstName { get; set; }

    [Display(Name = "Last name")]
    public string LastName { get; set; }

    public virtual Team Team { get; set; }        
}
 public class Team
{
    [Key]
    [HiddenInput(DisplayValue = false)]
    public int TeamId { get; set; }

    [Display(Name = "Full Name:")]
    public string Name { get; set; }        

    public virtual ICollection<Player> Players { get; set; }
}
Team.cs

public class Player
{
    [HiddenInput(DisplayValue = false)]
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }        

    [Required]
    [ForeignKey("Team")]
    [Display(Name = "Team")]
    public int TeamId { get; set; }        

    [Display(Name = "First name")]
    public string FirstName { get; set; }

    [Display(Name = "Last name")]
    public string LastName { get; set; }

    public virtual Team Team { get; set; }        
}
 public class Team
{
    [Key]
    [HiddenInput(DisplayValue = false)]
    public int TeamId { get; set; }

    [Display(Name = "Full Name:")]
    public string Name { get; set; }        

    public virtual ICollection<Player> Players { get; set; }
}
最后,您为玩家创建的视图如下所示:

model.Products = new SelectList(MyEnumerableofProducts, "ValueField", "TextField", {value of the selected item in the dropdown, as a string)
@{
ViewBag.Title = "Create";
 }

<h2>Create</h2>

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)

<fieldset>
    <legend>Player</legend>

    <div class="editor-label">
            @Html.LabelFor(model => model.TeamId, "Pick Your Team")
    </div>
    <div class="editor-field">
        @Html.DropDownList("TeamId", String.Empty)
        @Html.ValidationMessageFor(model => model.TeamId)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.FirstName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.FirstName)
        @Html.ValidationMessageFor(model => model.FirstName)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.LastName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.LastName)
        @Html.ValidationMessageFor(model => model.LastName)
    </div>        

    <p>
        <input type="submit" value="Create" />
    </p>
  </fieldset>
 }

 <div>
     @Html.ActionLink("Back to List", "Index")
 </div>

 @section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
@{
ViewBag.Title=“创建”;
}
创造
@使用(Html.BeginForm()){
@Html.ValidationSummary(true)
玩家
@LabelFor(model=>model.TeamId,“选择您的团队”)
@Html.DropDownList(“TeamId”,String.Empty)
@Html.ValidationMessageFor(model=>model.TeamId)
@LabelFor(model=>model.FirstName)
@EditorFor(model=>model.FirstName)
@Html.ValidationMessageFor(model=>model.FirstName)
@LabelFor(model=>model.LastName)
@EditorFor(model=>model.LastName)
@Html.ValidationMessageFor(model=>model.LastName)

} @ActionLink(“返回列表”、“索引”) @节脚本{ @Scripts.Render(“~/bundles/jqueryval”) }
如果您使用的是VB.Net-MVC4-Razor 这个答案几乎和Frigik一样(谢谢Frigik)

在您的模型中,创建两个字段,一个是保存SelectedItem的字段(这里是字符串的TagType),另一个是下拉值(TagTypeList)

模型类-Tag.vb 在CommonUtility类中-CommonUtility.vb 此处第三个参数(可选)为空,将在下拉列表中插入第一个项目为空。第一个参数是selectedItem,它有助于在DB表中已经存在该值时进行填充


希望这对使用VB.Net的用户有所帮助除了上述方法外,您还可以使用方法在查找表中使用多外键。就我所见,它非常清晰有效。

@Html.DropDownList(“ManagerId”,(SelectList)ViewBag.Managers,string.Empty)或使用model和lambda表达式@Html.DropDownListFor(x=>ManagerId,model.Managers)如何选择正确的“Manager”?我不明白