asp.net mvc 3 EF HTML MultiSelect - 如何使用 LINQ 检索多个值
我有一个高级搜索表单,它包含输入框、下拉列表等。现在我想将下拉列表更改为多选列表。
我可以设置视图,如下所示:
@Html.ListBoxFor(model => model.IdState, null, new { size = "7" })
但我不知道如何将控制器从: 更改
public ActionResult Index(FormData model)
{
IQueryable<mytable> results = from s in db.mytable.Include("State").where(s=> (model.IdState != null ? s.IdState == model.IdState : true)) select s;
return View(result);
}
为多个 IdState 结果???
早些时候,IdState 是:
int IdState;
现在是:
IEnumerable<int> IdState;
感谢您抽出时间!
我仍然无法让它发挥作用。在 SQL 中将是:
SELECT * FROM MyTable WHERE IdState IN (1,2,5,7,10)
有什么想法吗? http://msdn.microsoft.com/en-us/vcsharp/aa336746。 aspx 不要显示类似的情况
解决方法
我在此讨论中找到:http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/095745fe-dcf0-4142-b684-b7e4a1ab59f0 / 这段代码片段:
IQueryable<Foo> foos = context.Foo.Where("it.Id in {1, 2, 3, ...}");
我改为
string sState = "";
foreach (int a in model.IdState ) sState += (a.ToString() + ",");
sState = sState .Substring(0, sState .Length - 1);
IQueryable<mytable> results = from s in db.mytable.Where("it.IdState in {" + sState + "}");
And It Works Great!!!
希望能找到最终的解决方案
I have an Advanced Search form, It contains, input boxes, dropdown lists, etc. And now I want to change a dropdown list to a multiselect list.
I can set the view, like this:
@Html.ListBoxFor(model => model.IdState, null, new { size = "7" })
But I don't know how to change the controller from:
public ActionResult Index(FormData model)
{
IQueryable<mytable> results = from s in db.mytable.Include("State").where(s=> (model.IdState != null ? s.IdState == model.IdState : true)) select s;
return View(result);
}
To a multiple IdState Result???
Earlier the IdState was:
int IdState;
Now is:
IEnumerable<int> IdState;
Thanks for your time!
I still can't make it work. In SQL will be:
SELECT * FROM MyTable WHERE IdState IN (1,2,5,7,10)
Any Idea? The http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx Don't show similar situation
Workaround
I found on this discussion: http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/095745fe-dcf0-4142-b684-b7e4a1ab59f0/ this code snippet:
IQueryable<Foo> foos = context.Foo.Where("it.Id in {1, 2, 3, ...}");
I changed to
string sState = "";
foreach (int a in model.IdState ) sState += (a.ToString() + ",");
sState = sState .Substring(0, sState .Length - 1);
IQueryable<mytable> results = from s in db.mytable.Where("it.IdState in {" + sState + "}");
And It works great!!!
I hope can find the final solution
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
而不是
尝试使用
Instead of
try using