c# - remove field name in linq select -
using mvc 5 c#.
i have list object. class object:
public class cameradevice { public string ipaddress { get; set; } public string udn { get; set; } public string documenturl { get; set; } public string friendlyname { get; set; } public bool isusb { get; set; } public string model { get; set; } public string make { get; set; } public string displayname { get; set; } }
a list created object type:
var devices = new list<cameradevice>();
i want populate dropdown 1 of fields list object.
so:
public actionresult connection() { var model = new connectiondata(); model.devices = new selectlist(devices.select(i => new { i.friendlyname }), "device"); return view(model); }
my viewmodel class is:
public class connectiondata { [display(name = "device")] [required(errormessage = "select")] public string selecteddevice { get; set; } public ienumerable<selectlistitem> devices { get; set; } }
my html5 markup is:
@html.dropdownlistfor(m => m.selecteddevice, model.devices, "select device", new { @style = "width: 355px;height:21px; border:none;outline:0px;" });
the problem field name included in dropdown:
how can remove field name?
you need tell property name should used value , text dropdownlist.
use selectlist contructor way:
model.devices = new selectlist(devices .select(i => new { i.friendlyname }), "friendlyname", "friendlyname");
the second parameter dropdownlist value field posted action on form submit , second display text field dropdown list, selecting 1 property have passed friendlyname
property both.
or if want use selectlist constructor takes 1 parameter, in case don't create anonymous type while projecting selecting 1 property, way:
model.devices = new selectlist(devices .select(i => i.friendlyname));
Comments
Post a Comment