如何在mvc的下拉列表中为不同的用户显示不同的项目? [英] How can I display different items in a dropdownlist in mvc for different users?

查看:80
本文介绍了如何在mvc的下拉列表中为不同的用户显示不同的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次选择某个ID时,如何更改下拉列表中的某些字段?例如,我有这个应用程序将映射特定用户ID的某些字段,例如用户:1将有字段a + b。这将仅用于显示字段,但下拉列表仍将显示所有字段,例如user:1将显示字段a + b + c但仅需要查看a + b。如何让下拉列表显示正确的字段。以下是我到目前为止所获得的内容:

How can I change certain fields in a dropdownlist for every time a certain ID is chosen? For example I have this application that will map certain fields for a certain userID e.g user: 1 will have fields a + b. This will work for just displaying the fields but the dropdownlist will still display all fields e.g user: 1 will see fields a + b + c but is only required to see a + b. how can I make the dropdownlist display the correct fields. Here is what I have got so far:

'In the controller
Dim userModel = New IssueTracker.ClientUserProjectIssue()

userModel.cTable = dbServer.ClientTables.Where(Function(x) x.userID = getuserID).ToList()

userModel.proTable = dbServer.ProjectTypes.Where(Function(x) x.ProjectTypeID = 1).ToList()

Return View(userModel)

'In the view
'Here I want to display a dropdownlist for each chosen ID

@For Each test In Model.cTable
          @Html.DropDownList("ProjectTypeID", New SelectList(Model.proTable.Where(Function(x) x.ProjectTypeID = test.ProjectTypeID), "ProjectTypeID", "ProjectTypeName"), "Please Select")
Next        



这几乎正常工作,但由于下拉列表,它不是在一个下拉列表中显示项目,而是在一个下拉列表中显示每个项目。例如user1:会看到一个下拉列表,其中包含字段a,另一个下拉列表中包含字段b。



如何解决这个问题,以便将其全部放在一个下拉列表中?


This is nearly working but instead of displaying the items in one dropdownlist it is displaying each item in one dropdownlist because of the dropdownlist. e.g user1: will see a dropdownlist that has field a, and another dropdownlist that has field b.

How can I fix this so it is all in the one dropdownlist?

推荐答案

原因是因为您拥有的标记为Model.cTable项目中的每个项目呈现一个下拉列表。

1应该做的是坚持实现模型类的基本MVC方法,该模型类具有扁平结构并包含一个列表集合。

例如

公共类MyDropDownListItems

{

public List< string>项目{get; set;}

public MyDropDownListItems()

{

Items = new List< string>();

}

}

2.然后,在控制器中,将列表集合模型对象与两个表中的数据一起填充到一个列表中:

var dropDownItems = new MyDropDownListItems();

// - >在你的问题中使用你的linq代码添加项目

3.然后你必须修改标记以绑定到模型类对象类型而不是数据库linq项目。

@Html .DropDownList(ProjectTypeID,New SelectList(Model.Items,Please Select))
The reason is because the markup you have is rendering one drop down list for each item in the Model.cTable items.
1. What should do is adhere to basic MVC approach of implementing a model class which is of a 'flat' structure and contains ONE list collection.
e.g.
Public Class MyDropDownListItems
{
public List<string> Items {get;set;}
public MyDropDownListItems()
{
Items = new List<string>();
}
}
2. Then, in the controller, populate that list collection model object with the data from both tables into one list:
var dropDownItems = new MyDropDownListItems();
//--> Add items here using your linq code in your question
3. Then you must modify the markup to bind to the model class object type rather than database linq items.
@Html.DropDownList("ProjectTypeID", New SelectList(Model.Items, "Please Select"))


这篇关于如何在mvc的下拉列表中为不同的用户显示不同的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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