Mvc4中的选择项目 [英] Selection item in Mvc4

查看:45
本文介绍了Mvc4中的选择项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的模型中我有

public Nullable int leave_status {get;组; }





i希望为leave_status提供如下所示的选择列表

selectList(

new []

{

new SelectListItem {Value =0,Text =a},

new SelectListItem {Value =1,Text =b},

new SelectListItem {Value =2,Text =c},

},

值,文字

);

in my model i have
public Nullable int leave_status { get; set; }


i want to have a selection list like below for the leave_status
selectList(
new[]
{
new SelectListItem { Value = "0", Text = "a" },
new SelectListItem { Value = "1", Text = "b" },
new SelectListItem { Value = "2", Text = "c" },
},
"Value", "Text"
);

推荐答案

创建如下所示的类下拉列表



试试这样

Create a class Dropdownlist like below

Try like this
using System.Collections.Generic;using System.Web.Mvc;
 
namespace EDaaS.Domain.Utilities
{
    public static class DropDownList<t>
    {
        public static SelectList LoadItems(IList<t> collection, string value, string text)
        {
            return new SelectList(collection, value, text);
        }
    }
 
}</t></t>



您的观点


Your View

@Html.DropDownListFor(model => model.CityID, (IEnumerable<SelectListItem>)ViewBag.EmployeeList, "--Select--", new { @class = "select" })





再创建一个课程



Create one more class

public class Leave
{
public int Value{get;set;}
public string Text{get;set;}
}



你的ActionResult


Your ActionResult

var list=new List<leave>{new Leave{Value=1,Text="a"},new Leave{Value=2,Text="b"}};

ViewBag.EmployeeList =
                Domain.Utilities.DropDownList<leave>.LoadItems(list, "Value", "Text");</leave></leave>



希望这有助于


Hope this helps


这篇关于Mvc4中的选择项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆