dropDownList doesn't show real data - asp.net-mvc-3

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");

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.

How to get viewbag data in Asp.net MVC?

How to get viewbag data, i created one controller in that data is fetch from data base and try to show the data in view but it is not displaying data.
public ActionResult Show()
{
List<Models.Employees> lst = new List<Models.Employees>();
Models.Employees obj = new Models.Employees();
DataSet ds =new Models.BL.InsertBL().GetShow(obj);
lst = (from DataRow drw in ds.Tables[0].Rows
select new Models.Employees
{
Enames = drw["Ename"].ToString(),
DepartId =Convert .ToInt32(drw["Departmentid"]),
EmailIds = drw["Emailid"].ToString(),
Adress = drw["Address"].ToString()
}).ToList();
ViewBag.Sasi = lst;
return View();
}
#{
ViewBag.Title = "Show";
Layout = "~/Views/Shared/VideoLayout.cshtml";
var vv = ViewBag.Sasi;
}
In this scenario I would recommend you strongly typed views:
return View(lst);
and in View:
#model List<Models.Employees>
Your question, try this:
(List<Models.Employees>)ViewBag.Sasi

simple dropdown list

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")
)

How to bind Product and Item in Entity Framework?

I have binding into the class, but not in another. I can`t call Product from Item: Object reference not set to an instance of an object.
Whats wrong?
How optimize this code?
foreach (var xd in excelData)
{
Product p = new Product {
Name = xd.ProductName,
};
ctx.Products.Add(p);
ctx.SaveChanges();
Item t = new Item {
Product=p,
};
t.ProductId = t.Product.ProductId;
ctx.Items.Add(t);
ctx.SaveChanges();
t.Product = ctx.Products.Where(c => c.ProductId == t.ProductId).FirstOrDefault();
ctx.SaveChanges();
}
Assuming your ProducId and ItemIdare auto generated you can write this with a single call toSaveChanges` as follows
foreach (var xd in excelData)
{
Product p = new Product {
Name = xd.ProductName,
};
Item t = new Item {
Product=p,
};
ctx.Items.Add(t);
}
ctx.SaveChanges();
ObjectQuery.Include Method. Specifies the related objects to include in the query results.
ctx.Items.Include("Product").Where(c => c.SupplierId == id).ToList().ForEach(t =>
{
li.Add(new ShortPricesListItem()
{
Name = t.Product.Name,
});
});

Resources