简体   繁体   English

ASP.NET MVC 预选 Html.DropDownListFor

[英]ASP.NET MVC pre-selected Html.DropDownListFor

I have problem with Html.DropDownListFor.我对 Html.DropDownListFor 有疑问。 I have read a lot of similar questions but I still dont understand what I have to do.我已经阅读了很多类似的问题,但我仍然不明白我必须做什么。 I dont know how to make Meal "bb" in dropdown list as pre-selected value.我不知道如何将下拉列表中的膳食“bb”作为预选值。

EDIT: function code:编辑:功能代码:

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; }
}

Controller控制器

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);
}

View看法

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

HTML Result 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>

This is my result.这是我的结果。 在此处输入图像描述

Working Sample: https://dotnetfiddle.net/MHLuvc工作示例: https ://dotnetfiddle.net/MHLuvc

Model模型

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;}
}

Controller控制器

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);
}

View看法

@model OrderForm

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

DropDownListFor requires the View to have a Model (strongly typed), and uses the values in the Model to populate the relevant fields. DropDownListFor 要求视图具有模型(强类型),并使用模型中的值来填充相关字段。 Below is what you should be aiming at.以下是您应该瞄准的目标。

/* 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有日期时间的ASP.NET MVC 5 Html.DropDownListFor不起作用选择了SelectList - ASP.NET MVC 5 Html.DropDownListFor with datetime doesn't work SelectList selected asp.net mvc Html.DropDownListFor:如何处理选定的ID - asp.net mvc Html.DropDownListFor: how to handle selected id ASP.NET @Html.DropDownListFor() 选择值设置为 true 但提交到 controller 时返回 false - ASP.NET @Html.DropDownListFor() selected value is set to true but returns false when submitted to the controller 将C#ASP.NET MVC 3 SQL数据库转换为html.dropdownlistfor和AutoPostBack to Browse操作 - C# ASP.NET MVC 3 SQL database to html.dropdownlistfor and AutoPostBack to Browse action on selecting and item JQuery 从@Html.DropDownListFor asp.net mvc multiselect dropdownlistbox 获取最后更改的值 - JQuery to get the last changed value from @Html.DropDownListFor asp.net mvc multiselect dropdownlistbox 从Html.DropDownListFor()ASP.NET调用操作 - Calling an action from Html.DropDownListFor() ASP.NET 使用C#asp.net MVC代码优先方法在Razor中验证@ Html.DropDownList和@ Html.DropDownListFor - Validation for @Html.DropDownList and @Html.DropDownListFor in Razor using C# asp.net mvc code first approach 使用viewmodel在asp.net-MVC中使用html.dropdownlistFor绑定数据 - Use viewmodel to bind data with html.dropdownlistFor in asp.net-MVC 如何使用 ASP.Net Core @Html.DropDownListFor 指定默认选项? - How do I specify default option with an ASP.Net Core @Html.DropDownListFor? ASP.Net Core 3:如何使 @Html.DropDownListFor 具有默认值的“只读”? - ASP.Net Core 3: How do I make an @Html.DropDownListFor “read-only” with a default value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM