Mvc4中的选择项目

Mvc4中的选择项目

本文介绍了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"
);

推荐答案

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中的选择项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 18:54