ASP.NET MVC 3 中的下拉列表

发布于 2024-11-11 17:04:09 字数 361 浏览 1 评论 0原文

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

    @Html.EditorFor(model => model.Category)
    @Html.ValidationMessageFor(model => model.Category)
</div>

这给了我一个标签和一个文本框。如何获得带有静态选择项的下拉列表来代替文本框。请帮忙。我是 ASP.NET MVC 新手。我需要 Razor 语法的解决方案/建议。

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

    @Html.EditorFor(model => model.Category)
    @Html.ValidationMessageFor(model => model.Category)
</div>

This gives me a label and a text box. How can I get a drop down list with static select items in place of the text box. Please help. I am new to ASP.NET MVC. I need solution / advice in Razor syntax.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

删除→记忆 2024-11-18 17:04:09
@Html.DropDownListFor( model => model.Category, new SelectList(new [] {"cat1", "cat2", "cat3"}) );
@Html.DropDownListFor( model => model.Category, new SelectList(new [] {"cat1", "cat2", "cat3"}) );
爱给你人给你 2024-11-18 17:04:09

以下是如何使用模型、视图和控制器填充 DropDownList。

首先是视图

@using Website.Models
@model PageWithSelectList
@{
    ViewBag.Title = "Index";
}
@Html.DropDownList("DayOfWeek", Model.DaysOfWeek)

,然后是控制器和操作方法

using System.Web.Mvc;
using Website.Models;

namespace Website.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new PageWithSelectList();
            model.DayOfWeek = 3;
            return View(model);
        }
    }
}

以及 HTML 输出,

<select id="DayOfWeek" name="DayOfWeek">
<option value="1">Sunday</option>
<option value="2">Monday</option>
<option selected="selected" value="3">Tuesday</option>
<option value="4">Wednesday</option>
<option value="5">Thursday</option>
<option value="6">Friday</option>
<option value="7">Saturday</option>
</select>

我希望这会有所帮助。

Here is how you would populate a DropDownList using Model, View and Controller.

First the view

@using Website.Models
@model PageWithSelectList
@{
    ViewBag.Title = "Index";
}
@Html.DropDownList("DayOfWeek", Model.DaysOfWeek)

Then the Controller and Action method

using System.Web.Mvc;
using Website.Models;

namespace Website.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new PageWithSelectList();
            model.DayOfWeek = 3;
            return View(model);
        }
    }
}

and the HTML output

<select id="DayOfWeek" name="DayOfWeek">
<option value="1">Sunday</option>
<option value="2">Monday</option>
<option selected="selected" value="3">Tuesday</option>
<option value="4">Wednesday</option>
<option value="5">Thursday</option>
<option value="6">Friday</option>
<option value="7">Saturday</option>
</select>

I hope this helps.

九厘米的零° 2024-11-18 17:04:09

填充下拉列表的另一种方法是使用 viewbag。

控制器:

public Action Create()
{
   FillViewBag();
   Return View();
}

private void FillViewBag()
    {
        List<SelectListItem> selectOptions = new List<SelectListItem>();            
        for(int i = 1; i <= 3; i++)
        {
            SelectListItem sli = new SelectListItem();
            sli.Text = "Option " + i.ToString();
            sli.Value = i.ToString();
            selectOptions.Add(sli)
        }
        ViewBag.SelectOptions = selectOptions;
     }

在您看来:

 @Html.DropDownListFor(model => model.yourProp, ViewBag.SelectOptions as IEnumerable<SelectListItem>)

希望这对您有帮助!

Another way to fill your dropdownlist is by using the viewbag.

Controller:

public Action Create()
{
   FillViewBag();
   Return View();
}

private void FillViewBag()
    {
        List<SelectListItem> selectOptions = new List<SelectListItem>();            
        for(int i = 1; i <= 3; i++)
        {
            SelectListItem sli = new SelectListItem();
            sli.Text = "Option " + i.ToString();
            sli.Value = i.ToString();
            selectOptions.Add(sli)
        }
        ViewBag.SelectOptions = selectOptions;
     }

In your View:

 @Html.DropDownListFor(model => model.yourProp, ViewBag.SelectOptions as IEnumerable<SelectListItem>)

Hope this helps you!

或十年 2024-11-18 17:04:09

您可以使用下面的代码

Controler

var list = new SelectList(new[]
                                          {
                                              new {ID="1",Name="Employee"},
                                              new{ID="2",Name="Debtor"},
                                              new{ID="3",Name="Supplier"},
                                               new{ID="4",Name="Patient"},
                                          },
                           "ID", "Name", 1);
                ViewData["list"] = list;

                return View();

View

<div class="editor-field">

@Html.DropDownListFor(model => model.type,ViewData["list"] as SelectList,
 new { style = "width: 100px;"})

</div>

You can use the foolowing code

Controler

var list = new SelectList(new[]
                                          {
                                              new {ID="1",Name="Employee"},
                                              new{ID="2",Name="Debtor"},
                                              new{ID="3",Name="Supplier"},
                                               new{ID="4",Name="Patient"},
                                          },
                           "ID", "Name", 1);
                ViewData["list"] = list;

                return View();

View

<div class="editor-field">

@Html.DropDownListFor(model => model.type,ViewData["list"] as SelectList,
 new { style = "width: 100px;"})

</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文