MVC DropDownListFor [英] MVC DropDownListFor

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

问题描述

我是使用MVC方法的新手.如何在MVC中使用DropDownListfor?我需要从主表中获取列表,并将该选项显示在Business Profile对象中的Business Premise中.这是我的ViewModel

I am new using MVC approach. How to using the DropDownListfor in the MVC? I need get the list from my master table and show the option into the Business Premise in the Business Profile object. This is my ViewModel

 public class SMEAppViewModel 

{        
    public WP2PBusinessProfile BuisinessProfile  { get; set; }        
    public IEnumerable<WP2PMasterDropDownList> MasterList { get; set; }       

}

在我的控制器中,我已经将MasterList初始化为列表

In my controller, I already initialize the MasterList to the List

 var _MasterList = _context.WP2PMasterDropDownList.ToList();

在我看来,我可以使用以下代码在列表中显示所有选项

And in my view, I able to display all the option in the List by using the following code

 @foreach (var karim in Model.MasterList.Where(c => c.Variable == "BusinessPremise"))
                    {

                        @Html.DisplayFor(modelitem => karim.Value) <br>                            

                    }

但是,我在视图中使用了以下dropdownlistfor,但是我的DropDownlistfor无法在视图中显示我的下拉选项.

However, I using the following dropdownlistfor in my view, but my DropDownlistfor is not able to show my drop down option in my view.

 @Html.DropDownListFor(m => m.BuisinessProfile.BusinessPremise, new SelectList(Model.MasterList, "Id", "Value"), "Select value")
                                    @Html.LabelFor(m => m.BuisinessProfile.BusinessPremise, new { @class = "form-control" })

有人会告诉我我的代码有什么错误吗?

would anyone tell me any wrong in my code?

谢谢

推荐答案

以您的观点尝试

@Html.DropDownListFor(m => m.Entity, new Project.Models.My_Class().My_Method, new { @class = "form-control" })

您无法从DropDownListFor获取数据.

You cant get data from your DropDownListFor.

在My_Method中,您可以使用SelectList

In the My_Method , You can use SelectList

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

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