控制器中的Linq查询,用于选择数据库中的字段 [英] Linq query in controller for selecting fields in database

查看:40
本文介绍了控制器中的Linq查询,用于选择数据库中的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我的查看页面上有一个下拉框.我想从控制器发送此下拉列表的多个值.我的数据库中有ID和名称,每个ID包含五个不同的名称.是否可以根据控制器的ID将这些多个值发送到视图.请让我知道是否有任何想法.

谢谢

解决方案

是的,有可能:

控制器:

 List< MyType>数据=  GetStuffFromLinqHere ();
viewBag.DropDownSelectList =
     SelectList(
        数据
        " " ,
        data.FirstOrDefaut()/*  默认情况下选择第一个项目(可选)*/); 



查看您想要的列表::

 @ Html.DropDownListFor(model => ViewBag. ValueYouWantTheSelectedValueToBeIn ,ViewBag. DropDownSelectList )




显然,我无法给出有意义的名称,因为我不知道您在做什么. IDPropertyName是要在列表项的ID中使用的属性的名称,类似地DisplayPropertyName是要在DDL中显示的属性的名称. ValueYouWantTheSelectedValueToBeIn是您希望在提交表单时放置所选值的位置.

另外,您可能有一个视图模型,在这种情况下,请不要使用ViewBag,您应该在GetStuffFromLinqHere() 方法调用中或之后对列表进行排序.如果您具有复杂的ID或描述,则应该创建一个视图模型来处理它,或者将属性添加到列表中Linq包含的类型中.


感谢答复


谢谢和问候,
Bhavani.


hi
I have a drop down box in my view page. I want to send multiple values for this drop down from controller. I have id and name in my data base each id contains five different names. Is it possible to send these multiple values to the view depending on id to controller. Plz let me know if any one have any idea.

Thanks

解决方案

Yes, it is possible:

Controller:

List<MyType> data= GetStuffFromLinqHere();
viewBag.DropDownSelectList = 
    new SelectList( 
        data      
        "IDPropertyName", 
        "DisplayPropertyName", 
        data.FirstOrDefaut()/*Select the first Item by default (optional)*/    ); 



View, where you want the list::

@Html.DropDownListFor( model => ViewBag.ValueYouWantTheSelectedValueToBeIn, ViewBag.DropDownSelectList)




Obviously, I can''t give meaningful names as I don''t know what your are doing exactly. IDPropertyName is the name of the property you want in the Id of the list item, similarly DisplayPropertyName is the name of the property you want to display in the DDL. ValueYouWantTheSelectedValueToBeIn is where you want the selected value to be placed when the form is submitted.

Additionally, you may have a view model, in which case don''t used the ViewBag any you should sort the List either in or after the GetStuffFromLinqHere() method call. If you have a complex id or description, you should either create a view model to handle it, or add a property to the type the Linq contains in the list.


Thanks for the reply


Thanks and regards,
Bhavani.


这篇关于控制器中的Linq查询,用于选择数据库中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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