How to get viewbag data in Asp.net MVC? - asp.net-mvc-3

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

Related

Master details batch crud with entity framework only saving single detail record

I'm building a master details page with batch editing, that is multiple details records with single master record. but only one detail record is being saved into the data base. I tried to debug & found that detail loop is executing multiple time accurately but not the saving multiple data. Here is my Code for save method:
public ActionResult CMN_VAL_FORM(HRM_CMN_VLU_MST_ViewModel model)
{
//var ctx=new Entities1();
var CMN_VLU_MST_OBJ = new HRM_CMN_VLU_MST();
var CMN_VLU_DTL_OBJ = new HRM_CMN_VLU_DTL();
//using (TransactionScope transaction = new TransactionScope())
//{
using (var ctx = new Entities1())
{
var type_code = ctx.ExecuteStoreQuery<string>("select get_pk_code('hrm_cmn_vlu_mst','CMN_VLU_TYPE_CODE') from dual").SingleOrDefault(); //A scalar function to generate the code in the format yymmdd0001
var value_code = ctx.ExecuteStoreQuery<string>("select get_pk_code('hrm_cmn_vlu_dtl','CMN_VLU_CODE') from dual").SingleOrDefault();
CMN_VLU_MST_OBJ.CMN_VLU_TYPE_CODE = type_code;
CMN_VLU_MST_OBJ.CMN_VLU_REM = model.CMN_VLU_REM;
CMN_VLU_MST_OBJ.CMN_VLU_TYPE_FOR = model.CMN_VLU_TYPE_FOR;
CMN_VLU_MST_OBJ.CMN_VLU_TYPE_SRTNM = model.CMN_VLU_TYPE_SRTNM;
CMN_VLU_MST_OBJ.ENTRY_DATE = DateTime.Now;
CMN_VLU_MST_OBJ.CMN_VLU_TYPE_NAME = model.CMN_VLU_TYPE_NAME;
foreach (var item in model.HRM_CMN_VLU_DTL)
{
CMN_VLU_DTL_OBJ.CMN_VLU_LEVL = item.CMN_VAL_LEVL;
CMN_VLU_DTL_OBJ.CMN_VLU_REM = item.CMN_VLU_REM;
CMN_VLU_DTL_OBJ.CMN_VLU_SLNO = item.CMN_VLU_SLNO;
CMN_VLU_DTL_OBJ.CMN_VLU_TITL = item.CMN_VAL_TITL;
CMN_VLU_DTL_OBJ.CMN_VLU_CNTN = item.CMN_VAL_CNTN;
CMN_VLU_DTL_OBJ.CMN_VLU_CODE = value_code;
CMN_VLU_DTL_OBJ.CMN_VLU_TYPE_CODE = type_code;
CMN_VLU_DTL_OBJ.ENTRY_DATE = DateTime.Now;
CMN_VLU_DTL_OBJ.MAIL_ADDR_INT = item.MAIL_ADDR_INT;
CMN_VLU_DTL_OBJ.MAIL_ADDR_EXT = item.MAIL_ADDR_EXT;
CMN_VLU_DTL_OBJ.MAIL_AUTO_SEND_INT = item.MAIL_AUTO_SEND_INT;
CMN_VLU_DTL_OBJ.MAIL_AUTO_SEND_EXT = item.MAIL_AUTO_SEND_EXT;
CMN_VLU_DTL_OBJ.ACTIVE_STATUS = item.ACTIVE_STATUS;
CMN_VLU_MST_OBJ.HRM_CMN_VLU_DTL.Add(CMN_VLU_DTL_OBJ);
ctx.SaveChanges();
var temp_value_code = Int32.Parse(value_code);
temp_value_code++;
value_code = temp_value_code.ToString();
ctx.HRM_CMN_VLU_MST.AddObject(CMN_VLU_MST_OBJ);
}
ctx.SaveChanges();
// transaction.Complete();
//}
}
return View();
}
No Error message for the code, but not saving multiple detail records. What I'm doing wrong?
What I was doing wrong, I created the object just once & updated that same object every time while executing the foreach loop! I just moved the object declaration into the foreach loop & it works like a charm!

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

How to display array in MVC3?

Hello friends I get datas from Web services so I can't use Html.DisplayFor(model=>model.item)and how can I display datas in array. Here is my code:
#using icerik.TahakkukServices
#{
ViewBag.Title = "Deneme";
Layout = "~/Views/Shared/_Layout5.cshtml";
}
#{
TahakkukServicesClient client = new TahakkukServicesClient();
client.ClientCredentials.UserName.UserName = "service_test";
client.ClientCredentials.UserName.Password = "";
client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
MakbuzList[] liste = client.GetMakbuzListe(2);
}
#foreach (var item in liste)
{
Html.DisplayFor(item.Adi)
}
#model icerik.TahakkukServices. add your table name
#{
ViewBag.Title = "Deneme";
Layout = "~/Views/Shared/_Layout5.cshtml";
}
#{
TahakkukServicesClient client = new TahakkukServicesClient();
client.ClientCredentials.UserName.UserName = "service_test";
client.ClientCredentials.UserName.Password = "";
client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
MakbuzList[] liste = client.GetMakbuzListe(2);
}
#foreach (var item in liste)
{
Html.DisplayFor(model=>model.) add your column name
}
Allright I understand how to do it just giving model name with your Service as you can see at below
#model icerik.TahakkukServices.Borc
so you can use Html.DisplayFor

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

MVCContrib pager with large number of query parameters and URL routing

I have a search controller that has a large number of parameters (the search criteria). I want to page through the results using MVCContrib - IPagination. This is all good the problem is how to generate the Link on the Html.Pager. I've tried this in the view
#Html.Pager(Model.Results).Link(p =>
Url.Action("Search", new {
Model.Criteria } ))
but is doesn't work. Do I need to register a route so that the Url.Action understands how to form the link?
Controller code below....
public ViewResult Search(JobSearch search, int? page, IAuthenticatedUser authenticatedUser)
{
// perform search
var query = _jobRepository.CreateQuery<IPagedJobSearch>();
query.SiteId = authenticatedUser.SiteId;
query.JobId = search.Criteria.JobId;
query.Lot = search.Criteria.LotNumber;
query.Street = search.Criteria.StreetInfo.Name;
query.StreetNumber = search.Criteria.StreetInfo.Number;
query.Suburb = search.Criteria.Suburb;
query.Council = search.Criteria.Council;
query.ClientRef = search.Criteria.ClientOrderNumber;
query.ItemsPerPage = 15;
query.PageNumber = page ?? 1;
var pagedResult = query.Execute(); ......
You don't show all of your markup but you'll need to specify each of your parameters like this:
<%= Html.Pager(Model.AssetsPagedList)
.First("First")
.Last("Last")
.Next("Next")
.Previous("Previous")
.Link(currentPage => Url.Action("Browse", new {
page = currentPage,
searchTerm = Model.SearchModel.SearchTerm,
excludedWords = Model.SearchModel.ExcludedWords,
minPrice = Model.SearchModel.MinPrice,
maxPrice = Model.SearchModel.MaxPrice,
locationId = Model.SearchModel.LocationId,
catalogId = Model.SearchModel.CatalogId
}))
%>
Once you have the correct parameters, you'll need to create a controller that accepts each of them:
public ActionResult Browse(int? id, string searchTerm, int? locationId,
GridSortOptions gridSortOptions, int? page, string excludedWords,
decimal? minPrice, decimal? maxPrice, int? catalogId)
{
}

Resources