如何通过在MVC中登录用户来过滤DropDown列表

时间:2017-03-25 13:46:35

标签: c# asp.net asp.net-mvc entity-framework asp.net-mvc-4

如何过滤下拉列表,我需要记录登录用户名。

控制器中的

我有这个代码:

    var agentBR = (Session["aID"]);
    ViewBag.AgentID = new SelectList(db.tbl_agenti.Where(x => x.aID.Equals("agentBR")), "aID", "agent_ime");

但没有工作,会话ID正常,在浏览器中我有这个错误:

DbComparisonExpression需要具有可比较类型的参数。

描述:在执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息: System.ArgumentException:DbComparisonExpression需要具有可比类型的参数。

来源错误:

  

@ Html.LabelFor(model => model.AgentID,“AgentID”,htmlAttributes:new {@class =“control-label col-md-2”})

     

@ Html.DropDownList(“AgentID”,null,htmlAttributes:new {@class =“form-control”})

     

@ Html.ValidationMessageFor(model => model.AgentID,“”,new {@class =“text-danger”})

1 个答案:

答案 0 :(得分:0)

Where方法

中代码的这一部分
x.aID.Equals("agentBR")

尝试过滤aID属性值与Equals方法中传递的值匹配的记录。您提到aID是int类型,但是您将字符串"agentBR"传递给Equals方法进行比较,从而导致类型不匹配错误。

你应该尝试传递相同的类型。

假设Session["aID"]中存储的值是有效的int32值,您可以将其读取到int变量并在where子句中使用它来与aID属性进行比较int属性int agentId=0; if(Session["aID"]!=null) { agentId = Convert.ToInt32(Session["aID"]); } var agents = new SelectList(db.tbl_agenti .Where(x => x.aID.Equals(agentId)), "aID", "agent_ime"); ViewBag.AgentID = agents; 1}}类型。

Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this); 
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);

//setup initial adapters etc.

 public void onItemSelected(AdapterView<?> parent, View view,
            int pos, long id) {
       //based on the selected item update the second spinner:
 ArrayAdapter<CharSequence> adapter ArrayAdapter.createFromResource(this,
        R.array.planets_array, android.R.layout.simple_spinner_item);

// Apply the adapter to the spinner
spinner2.setAdapter(adapter);
    }  
相关问题