ASP.NET 预选 MVC Html.DropDownListFor

ASP.NET MVC pre-selected Html.DropDownListFor

我对 Html.DropDownListFor 有疑问。我已经阅读了很多类似的 questions 但我仍然不明白我必须做什么。我不知道如何将下拉列表中的 Meal "bb" 作为预选值。

编辑:函数代码:

ViewModel

public class OrderForm
{
    public int Id { get; set; }
    public string MealName { get; set; }
    public int OrderCount { get; set; }
    public List<SelectListItem> MealOptions { get; set; }
}

控制器

public IActionResult OrderForm()
{
    List<SelectListItem> mealOptions = new List<SelectListItem>();

    mealOptions.Add(new SelectListItem("aa", "1"));
    mealOptions.Add(new SelectListItem("bb", "2"));
    mealOptions.Add(new SelectListItem("cc", "3"));

    OrderForm viewModel = new OrderForm
    {
         MealName = "someMeal",
         Id = 2,
         OrderCount = 5,
         MealOptions = mealOptions
    };

    return View(viewModel);
}

查看

@model OrderForm
@Html.LabelFor(x => x.MealName)
@Html.TextBoxFor(x => x.MealName)
@Html.DropDownListFor(x => x.Id, Model.MealOptions)

HTML 结果

<div>
    <label for="MealName">MealName</label>
    <input id="MealName" name="MealName" type="text" value="someMeal" />

    <select data-val="true" data-val-required="The Id field is required." id="Id" name="Id">
          <option value="1">aa</option>
          <option selected="selected" value="2">bb</option>
          <option value="3">cc</option>
    </select>
</div>

这是我的结果。

工作样本:https://dotnetfiddle.net/MHLuvc

型号

using System.Web.Mvc;
using System.Collections.Generic;

public class OrderForm {
    public int Id { get; set; }
    public string MealName {get; set;}
    public int OrderCount { get; set; }
    public int MealItemId {get;set;}
    public List<SelectListItem> MealOptions { get; set; }
}

public class MealItem {
    public int AltId {get;set;}
    public string Name {get;set;}
}

控制器

using System.Linq;

public IActionResult OrderForm(int id)
{
    var mealItems = new List<MealItem>() {
         new MealItem(){AltId = 0,Name = "aa"},
         new MealItem(){AltId = 1,Name = "bb"},
         new MealItem(){AltId = 2,Name = "cc"}
    };
        
    var mealOptions = mealItems.Select(prop => new SelectListItem() { Text = prop.Name, Value = prop.AltId.ToString() }).ToList();

    OrderForm viewModel = new OrderForm
    {
        MealName = "someMeal",
        Id = 4,
        OrderCount = 5,
        MealItemId = 2, // Set Default MealOption Id
        MealOptions = mealOptions
    };

    return View(viewModel);
}

查看

@model OrderForm

@{
    @Html.LabelFor(x => x.MealName)
    @Html.TextBoxFor(x => x.MealName)
    @Html.DropDownListFor(x => x.MealItemId, Model.MealOptions)
}

DropDownListFor 要求 View 有一个 Model(强类型),并使用 Model 中的值填充相关字段。以下是您应该瞄准的目标。

/* create a new viewmodal */
public class OrderVm
{
    [DisplayName("Meal Name")]
    public string MealName { get; set; }
    public List<SelectListItem> MealOptions { get; set; } = new List<SelectListItem>();
}

/* updated controller */
public IActionResult OrderForm(int id)
{
   List<MealItem> listMeal = mealService.GetMeals();

   var orderVm = new OrderVm()
   {
       MealName = "bb",
   };

   foreach (MealItem m in listMeal)
   {
       orderVm.MealOptions.Add(new SelectListItem(m.mealName, m.altId.ToString()));
   }

   return View(orderVm);
} 


/* view */
@model OrderVm
@Html.LabelFor(x => x.MealName)
@Html.DropDownListFor(x => x.MealName, Model.MealOptions)