simple dropdown list - asp.net-mvc-3

i did not found simple dropdown list .
i'm new in mvc3
this part create and fill : ListMonitoringLicenseModelList = new List();
i fill this objectsList in another part and use it to fill MonitoringLicenseModelList
objectsList = new List(...fill objectsList...);
List<MonitoringLicenseModel>MonitoringLicenseModelList = new List<MonitoringLicenseModel>();
foreach (object o in objectsList)
{
string[] tmpString = o.ToString().Split('#');
int i = 0;
monitoringLicenseModel = new MonitoringLicenseModel();
monitoringLicenseModel.MonitoringID =Convert.ToInt32(tmpString[i++]);
monitoringLicenseModel.LicenseUI = tmpString[i++];
MonitoringLicenseModelList.Add(monitoringLicenseModel);
}
public ActionResult SchedulesIndex()
{
return View(MonitoringLicenseModelList);
}
how can i write dropdownlist to show this
name value
MonitoringID = LicenseUI

#model List<MonitoringLicenseModel>
...
#Html.DropDownList(
"SelectedMotoringID",
new SelectList(Model, "MonitoringID", "LicenseUI")
)

Related

Bind List to GridviewComboboxcolumn in telerik radgrindview (winform)

I have a generic List like
List<return> returnlist
class return
{
public string returnid {get; set;
...
public List<string> Vouchernumbers
}
I bind the returnlist to the telerik radgridview.
How can i bind the voucherlist to the GridviewComboboxcolumn for each row ?
I have bind the voucherlist to the combobox after radgridview_complete_binding.
You need to create the grid with columns and data
You need to add the combobox column, initialize it.. Please check you need to have a dataeditor in here
Assign the string to datasource
comboColumn.DataSource = new String[] { "Test1", "Test2"};
You can bind the collections too:
Binding list BindingList<ComboBoxDataSourceObject> list = new BindingList<ComboBoxDataSourceObject>();
ComboBoxDataSourceObject object1 = new ComboBoxDataSourceObject();
object1.Id = 1;
object1.MyString = "Test 1";
list.Add(object1);
ComboBoxDataSourceObject object2 = new ComboBoxDataSourceObject();
object2.Id = 2;
object2.MyString = "Test 2";
list.Add(object2);
colboCol2.DataSource = list;
radGridView1.Columns.Add(colboCol2);
create radcombobox and set datasource and add it to rad grid
eg :
GridViewComboBoxColumn col = new GridViewComboBoxColumn();
col.DataSource = DAL.ActiveDb.GetList<SalesRep>().ToList().OrderBy(x => x.RepName).Select(x => new { Id = x.Id, x.RepName });
col.DropDownStyle = RadDropDownStyle.DropDown;
col.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
col.DisplayMember = "RepName";
col.ValueMember = "Id";
col.FieldName = "RepId";
col.HeaderText = "Rep Name";
col.Width = 200;
//var t = gridColInfo.Where(x => x.ColumnName.ToLower() == "repid").FirstOrDefault();
//if (t != null)
//{
// col.Width = t.ColumnWidth;
//}
this.radGridBillwiseOpening.Columns.Add(col);

Why does my selected value not get added to my SelectListItem as the “Text” and “Value” properties do?

I am trying to set a default value of a list in a partial view. The partial view is called using this…
#Html.Action("Acton", "Controller", new { department = item.Data.departmentNumber, defaultValue="someValue" })
Then in the controller I have…
[ChildActionOnly]
public ActionResult Categories(int? id, int? department, string defaultValue)
{
var typeList = from e in db.Rubrics where e.DepartmentID == department select e;
var selectedRubrics = typeList.Select(r => r.Category);
List<String> rubricsList = selectedRubrics.ToList();
var categories = new List<SelectListItem>();
for (int i = 0; i < rubricsList.Count(); i++)
{
categories.Add(new SelectListItem
{
Text = rubricsList[i],
Value = rubricsList[i],
Selected = (defaultValue == "defaultValueGetsSentToView")
});
}
var ViewModel = new RubricsViewModel
{
Category = "Select a Category",
Categories = categories
};
return View(ViewModel);
}
Why does my selected value not get added to my SelectListItem as the “Text” and “Value” properties are? Thanks for any help!
Assuming the values in the code are the literal values you are using, "defaultValue" is "someValue" and you are setting selected with the comparison defalutValue == "defaultValueGetsSentToView".
"someValue" == "defaultValueGetsSentToView" evaluates to false.

Create a store from Code Behind and filter its content and bind it to Combobox Ext.Net 2.2

I am creating combobox in a component Column in a GridPanel from codebehind. There is requirement to create a store and filter its content according to some condition and bind it to the created combobox. Store is getting bind to combo but filter is not working. Please help me to get the proper solution for this. My code snippet is given below.
List<object> storeDataProductClass= new List<object>();
storeDataProductClass.Add(new { text = "Class0", value = "Class0", productIndex = 0});
storeDataProductClass.Add(new { text = "Class1", value = "Class1", productIndex = 1});
storeDataProductClass.Add(new { text = "Class2", value = "Class2", productIndex = 2});
storeDataProductClass.Add(new { text = "Class3", value = "Class3", productIndex = 3});
storeDataProductClass.Add(new { text = "Class4", value = "Class4", productIndex = 4});
Ext.Net.ComboBox cmbClass = new ComboBox();
cmbClass.ID = "cmbClass_" + i;
Model classModel = new Model();
classModel.Fields.Add(new ModelField("text", ModelFieldType.String));
classModel.Fields.Add(new ModelField("value", ModelFieldType.String));
classModel.Fields.Add(new ModelField("productIndex", ModelFieldType.Int));
Ext.Net.Store storeClass = new Ext.Net.Store();
storeClass.ID = "storeClass_" + i;
storeClass.AutoDataBind = true;
storeClass.Model.Add(classModel);
storeClass.DataSource = storeDataProductClass;
storeClass.DataBind();
storeClass.Filter("productIndex", i.ToString());
cmbClass.Store.Add(storeClass);
cmbClass.DisplayField = "text";
cmbClass.ValueField = "value";
compColumn.Component.Add(cmbClass);

Error converting Linq query to list

Here's my code. I want to save the list in a session variable for later authentication (it's a list of objects the user is authorized to access....) I get an error saying it cannot implictly convert System.Collections.Generic.List to 'System.collections.Generic.List. Help?
protected void Session_Start(object sender, EventArgs e)
{
string strUserName = User.Identity.Name;
string strShortUserName = strUserName.Replace("WINNTDOM\\", string.Empty).ToUpper();
System.Web.HttpContext.Current.Session["strShortUserName"] = strShortUserName;
System.Web.HttpContext.Current.Session["strUserName"] = strUserName;
List<string> authorizedObjects = new List<string>();
using (CPASEntities ctx = new CPASEntities())
{
var w = (from t in ctx.tblUsers
where (t.UserPassword == strUserName)
select t).FirstOrDefault();
if (!(w==null))
{
authorizedObjects = (from t in ctx.qryUserObjectAuthorization
where (t.UserPassword == strUserName)
select new { n = t.ObjectName }).ToList();
}
}
}
To generate that as a list of strings use
authorizedObjects = (from t in ctx.qryUserObjectAuthorization
where (t.UserPassword == strUserName)
select t.ObjectName).ToList();
authorizedObjects is List<string> but you're trying to use it as a List of anonymous type:
List<string> authorizedObjects = new List<string>();
(...)
authorizedObjects = (from t in ctx.qryUserObjectAuthorization
where (t.UserPassword == strUserName)
select new { n = t.ObjectName, i = t.ObjectID }).ToList()
Change your query to that:
authorizedObjects = (from t in ctx.qryUserObjectAuthorization
where (t.UserPassword == strUserName)
select t.ObjectName).ToList()
You are initializing a List<string> object but populating different object.
List<string> authorizedObjects = new List<string>();
select new { n = t.ObjectName, i = t.ObjectID } <--construct a class with these properties and initialize `List<WithNewClassName>`

dropDownList doesn't show real data

i have added the model in my view:
#model Tuple<List<EshopTheme.Areas.Administrators.Models.ThemeGroupsModel>,
EshopTheme.Areas.Administrators.Models.AdminModel,
EshopTheme.Areas.Administrators.Models.ThemesModel>
in my Controller i have added this action:
public ActionResult AddTheme()
{
AdminSrv adminService = new AdminSrv();
AdminModel adminModel = new AdminModel();
ThemesModel themeModel = new ThemesModel();
Tuple<List<ThemeGroupsModel>, AdminModel, ThemesModel> tuple =
new Tuple<List<ThemeGroupsModel>, AdminModel, ThemesModel>
(adminService.getAllThemeGroupByAdmin(), adminModel, themeModel);
return View(tuple);
}
i have used this DropDownListFor :
#Html.DropDownListFor(x => x.Item1, new SelectList(Model.Item1))
but, DropDownListFor , shows the list of:
EshopTheme.Areas.Administrators.Models.ThemeGroupsModel
EshopTheme.Areas.Administrators.Models.ThemeGroupsModel
EshopTheme.Areas.Administrators.Models.ThemeGroupsModel
why?????????????
this is my getAllThemeGroupByAdmin() code:
public List<ThemeGroupsModel> getAllThemeGroupByAdmin()
{
List<ThemeGroupsModel> ThemeGr = new List<ThemeGroupsModel>();
ThemeGroupsModel ThemeGrTemp;
using (var context = new EShopThemeDBEntities(idbconnection.ConnStr))
{
var ThemeGroupList = (from o in context.ThemeGroups
select o).ToList();
foreach (var item in ThemeGroupList)
{
ThemeGrTemp = new ThemeGroupsModel();
ThemeGrTemp.ThemeGroupName = item.ThemeGroupName;
ThemeGrTemp.ThemeGroupId = item.ThemeGroupID;
ThemeGr.Add(ThemeGrTemp);
}
}
return ThemeGr;
}
You should use Text and Value property;
SelectList selectList = new SelectList(selectListItems, "Value", "Text");

Resources