如何在ASP.NET MVC4中为DropdownListFor创建控制器,从MSSQL中填充数据。 [英] How do I create controller for DropDownListFor in ASP.NET MVC4 populating data from MSSQL.

查看:70
本文介绍了如何在ASP.NET MVC4中为DropdownListFor创建控制器,从MSSQL中填充数据。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型代码:

  public   class  PostLeadModel 
{

[显示(名称= 项目输入 0 ]
public string ItemType { get ; set ; }
[显示(名称= 项目类别)]
public string ItemCategory { get ; set ; }
[显示(名称= 项目子类别)]
}

public int SelectedItemCategoryId {获得; set ; }
public IList< itemcategory> _itemCategories;
public IEnumerable< SelectListItem> ItemCats
{
get
{
var allItemCategories = _itemCategories.Select(f = > new SelectListItem
{
Value = f.ID.ToString(),
Text = f.Name
});
return DefaultItemCats.Concat(allItemCategories);
}
}

public IEnumerable< SelectListItem> DefaultItemCats
{
get
{
return 可枚举.Repeat( new SelectListItem
{
Value = - 1
Text = 选择一个项目类别
},count: 1 );
}
}
// private List< itemsubcategory> _itemSubCategories;

[显示(名称= Item SubCategories)]
public int SelectedItemSubCategoryId { get ; set ; }

public IList< itemsubcategory> _itemSubCategories;
public IEnumerable< SelectListItem> ItemSubCats
{
get
{
var allItemSubCategories = _itemSubCategories.Select(f = > new SelectListItem
{
Value = f.ID.ToString(),
Text = f.Name
});
return DefaultItemSubCats.Concat(allItemSubCategories);
}
}

public IEnumerable< SelectListItem> DefaultItemSubCats
{
get
{
return 可枚举.Repeat( new SelectListItem
{
Value = - 1
Text = 选择一个Item SubCategory
},count: 1 );
}
}



我的视图代码:

 @ Html.LabelFor(m => m.SelectedItemCategoryId)
@ Html.DropDownListFor(m => m.SelectedItemCategoryId,Model.ItemCats)
@ Html.ValidationMessageFor(m => ; m.SelectedItemCategoryId)
@ Html.LabelFor(m => m.SelectedItemSubCategoryId)
@ Html.DropDownListFor(m = > m .SelectedItemSubCategoryId,Model.ItemSubCats)
@ Html.ValidationMessageFor(m => m.SelectedItemSubCategoryId);

解决方案

你我没有在这里更新完整的代码。但我们可以根据需要指导你

查看你的模型类

右键单击模型文件夹 - 添加类 - 名称PostLeadModel

命名空间MyMVC.Models 
{
public class PostLeadModel
{

[Display(Name =Item)键入)]
公共字符串ItemType {get;组; }
[显示(名称=项目类别)]
公共字符串ItemCategory {get;组; }
[Display(Name =Item Sub-Category)]
public string itemsubcategory {get;组; }
}

}



转到Controller Folder -Right单击 - 添加新控制器 - 带名称

然后添加你剩下的代码

//把你剩下的代码

去任何一个Controller类的方法>右键单击>添加视图>提供你的名字>根据需要放置代码

 @ Html.LabelFor(m => m.SelectedItemCategoryId)
@ Html.DropDownListFor(m => m.SelectedItemCategoryId, Model.ItemCats)
@ Html.ValidationMessageFor(m => m.SelectedItemCategoryId)
@ Html.LabelFor(m => m.SelectedItemSubCategoryId)
@ Html.DropDownListFor(m => ; m.SelectedItemSubCategoryId,Model.ItemSubCats)
@ Html.ValidationMessageFor(m => m.SelectedItemSubCategoryId);


My code for the model:

       public class PostLeadModel
    {

        [Display(Name = "Item Type")0]
        public string ItemType { get; set; }
        [Display(Name = "Item Category")]
        public string ItemCategory { get; set; }
        [Display(Name = "Item Sub-Category")]
     }

public int SelectedItemCategoryId { get; set; }
        public  IList<itemcategory> _itemCategories;
        public IEnumerable<SelectListItem> ItemCats
            {
                get 
                    {
                        var allItemCategories = _itemCategories.Select(f => new SelectListItem
                                               {
                                                   Value = f.ID.ToString(),
                                                   Text = f.Name          
                                              });
                        return DefaultItemCats.Concat(allItemCategories);               
    }
}
 
public IEnumerable<SelectListItem> DefaultItemCats
{
    get
    {
        return Enumerable.Repeat(new SelectListItem
        {
               Value = "-1", 
               Text = "Select an Item Category"
         }, count: 1); 
    }
 }
     //private List<itemsubcategory> _itemSubCategories;

[Display(Name = " Item SubCategories")]
public int SelectedItemSubCategoryId { get; set; }

public  IList<itemsubcategory> _itemSubCategories;
            public IEnumerable<SelectListItem> ItemSubCats
            {
                get 
                    {
                        var allItemSubCategories = _itemSubCategories.Select(f => new SelectListItem
                                               {
                                                   Value = f.ID.ToString(),
                                                   Text = f.Name          
                                               });
                        return DefaultItemSubCats.Concat(allItemSubCategories);
                    }
            }

            public IEnumerable<SelectListItem> DefaultItemSubCats
            {
                get
                {
                    return Enumerable.Repeat(new SelectListItem
                    {
                        Value = "-1",
                        Text = "Select an Item SubCategory"
                    }, count: 1);
                }
            }


My code for the View:

          @Html.LabelFor(m=>m.SelectedItemCategoryId)
@Html.DropDownListFor(m =>m.SelectedItemCategoryId, Model.ItemCats)
@Html.ValidationMessageFor(m=>m.SelectedItemCategoryId)
                @Html.LabelFor(m=>m.SelectedItemSubCategoryId)
@Html.DropDownListFor(m => m.SelectedItemSubCategoryId, Model.ItemSubCats)
@Html.ValidationMessageFor(m=>m.SelectedItemSubCategoryId);

解决方案

You have not update full of code here.But we can guid you as per required
See Your model class
Right Click on Model Folder - Add Class-With Name "PostLeadModel"

namespace MyMVC.Models
{
  public class PostLeadModel
    {
 
        [Display(Name = "Item Type")]
        public string ItemType { get; set; }
        [Display(Name = "Item Category")]
        public string ItemCategory { get; set; }
        [Display(Name = "Item Sub-Category")]
        public string itemsubcategory { get; set; }
     }
 
}


Go to Controller Folder -Right Click - Add New Controller- With Name
Then add your rest of code
//Put your rest of code
Go any one of the method of Controller class > Right Click > Add View > Give your name > Put your code as per required

 @Html.LabelFor(m=>m.SelectedItemCategoryId)
@Html.DropDownListFor(m =>m.SelectedItemCategoryId, Model.ItemCats)
@Html.ValidationMessageFor(m=>m.SelectedItemCategoryId)
                @Html.LabelFor(m=>m.SelectedItemSubCategoryId)
@Html.DropDownListFor(m => m.SelectedItemSubCategoryId, Model.ItemSubCats)
@Html.ValidationMessageFor(m=>m.SelectedItemSubCategoryId); 


这篇关于如何在ASP.NET MVC4中为DropdownListFor创建控制器,从MSSQL中填充数据。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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