enter image description hereI have a controller that gets its data from a user defined function using Entity framwework.I am trying to just display my data in the view and populate my table.
My Controller looks like this:
public ActionResult Index()
{
var description = "Toyota";
QuotingEngineEntities1 vehicles = new QuotingEngineEntities1();
List<object> list = new List<object>();
using (var context = new QuotingEngineEntities1())
{
var vehicle = from s in context.fn_GetVehicle(description)
select new
{
s.MAKE,
s.MODEL,
s.PRICE,
s.POWER,
s.Transmission
};
list.Add(vehicle.FirstOrDefault());
}
ViewBag.list = list;
return View(ViewBag.list);
}
AND MY View looks like this
#foreach (var v in ViewBag.list)
{
<li>#v.MODEL</li> //i get an error
<br />
}
I finally got it work.i had to loop through the data before adding it to the list.
public ActionResult Index()
{
var description = "Toyota";
List<fn_GetVehicle_Result> list = new List<fn_GetVehicle_Result>();
using (var context = new QuotingEngineEntities1())
{
var query = context.fn_GetVehicle(description);
foreach (var v in query)
{
list.Add(v);
}
ViewBag.list = list;
}
return View("Index",ViewBag.list);
}
enter code here
You are trying use Viewbag with your list data but it is not advisable way to do this and you dont have to add Viewbag into View() method. It is sent automatically into view by controller.
I would suggest to use ViewModel or ExpandoObject to send your collection into view. You can implement like following
//controller
public ActionResult Index()
{
using (var context = new QuotingEngineEntities1())
{
var vehicle = from s in context.fn_GetVehicle(description)
select new
{
s.MAKE,
s.MODEL,
s.PRICE,
s.POWER,
s.Transmission
};
dynamic yourmodel = new ExpandoObject();
yourmodel.Vehicles = vehicle.ToList();
}
return View(yourmodel);
}
// view
#using YourProject; // Your Project Name
#model dynamic
#foreach (var v in model.Vehicles)
{
<li>#v.MODEL</li>
<br />
}
Related
I'm getting System.NullReferenceException: 'Object reference not set to an instance of an object.' error in Index2.cshtml file after deserializing my TempData object. How can I show my object without getting this error? ( This is my first time asking in here sorry if I made mistakes.)
var products = new List<Products>
{
new Products {Id=56, ProductName="x",Quantity=45},
new Products {Id=55, ProductName="y",Quantity=5},
new Products {Id=54, ProductName="z",Quantity=4},
new Products {Id=53, ProductName="t",Quantity=8},
new Products {Id=52, ProductName="k",Quantity=12}
};
string data = JsonSerializer.Serialize(products);
TempData["products"] = data;
return RedirectToAction("Index2");
public IActionResult Index2()
{
var data = TempData["products"].ToString();
List<Products> products = JsonSerializer.Deserialize<List<Products>>(data);
return View();
}
**Index2.cshtml**
<ul>
#foreach(var item in TempData["products"] as List<Products>)
{
<li>#item.ProductName</li>
}
</ul>
Change your code from:
List<Products> products = JsonSerializer.Deserialize<List<Products>>(data);
To:
TempData["products"] = JsonSerializer.Deserialize<List<Products>>(data);
Or you can use return View(products) to use the data:
public IActionResult Index2()
{
var data = TempData["products"].ToString();
List<Products> products = JsonSerializer.Deserialize<List<Products>>(data);
return View(products);
}
View:
#model List<Products>
<ul>
#foreach (var item in Model)
{
<li>#item.ProductName</li>
}
</ul>
I have this in my View:
#{
var categories = (List<C_Category>)Model.c;
}
#foreach (C_Category cat in categories)
{
<option value="#cat.C_Id">#cat.C_Name</option>
}
And this in my Controller:
[HttpGet]
public ActionResult Admin()
{
using (var context = new sopingadbEntities())
{
List<P_Product> p = context.P_Product.OrderByDescending(x => x.P_Id).ToList();
List<C_Category> c = context.C_Category.ToList();
var ao = new AdminObj()
{
p = p,
c = c
};
return View("Admin", new { c, p });
}
}
But in my view I get an error:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'object' does not contain a definition for 'c'
I'm sure I've been doing this way all the time, am I missing something?
Here is the addwatch:
Error:
if u declare AdminObj as model in view than you have to pass var ao in return perameter
or
currently u are returning anonymous object as model in return view which is not work in view as you casted
mention what you added as #model in view
Answer extended:
Had to add this in the view as mentioned in this answer.
#model ProjectWeb.Controllers.HomeController.AdminObj
And in Controller:
[HttpGet]
public ActionResult Admin()
{
using (var context = new sopingadbEntities())
{
List<P_Product> p = context.P_Product.OrderByDescending(x => x.P_Id).ToList();
List<C_Category> c = context.C_Category.ToList();
var ao = new AdminObj()
{
p = p,
c = c
};
return View(ao);
}
}
public ActionResult Index()
{
ESSEntities DB = new ESSEntities();
List<EmployeeMdl> EmpList = DB.Employees.ToList();
return View(EmpList);
}
How do I pass this list to view because I got error
Cannot implicitly convert type '' to 'System.Collections.Generic.List'
You could do something like this.
public ActionResult Index()
{
ESSEntities DB = new ESSEntities();
List<Employees> lstDBEmployees = DB.Employees.ToList();
List<EmployeeMdl> EmpList = new List<EmployeeMdl>();
foreach(var thisEmployee in lstDBEmployees )
{
EmpList.Add(new EmployeeMdl(){
prop1 = thisEmployee.prop1,
prop2 = thisEmployee.prop2
});
}
return View(EmpList);
}
I think your view is not receiving List please use this.
View:
#model List<EmployeeMdl>
#foreach(var item in Model)
{
//code here
}
Controller:
public ActionResult Index()
{
ESSEntities DB = new ESSEntities();
List<EmployeeMdl> EmpList = DB.Employees.ToList();
return View(EmpList);
}
var EmpList =
DB.Employees.Select(c => new{ /*your model properties somthing like c.EmployeeId, c.EmployeeName*/}).ToList();
Try this.
I am trying to populate a listbox from a model which is my Entity framework edmx model I seems to be populating but each character is appearing on a new line so for instance
H
A
L
O
instead of
HALO
this is my controller:
private dataEntities dbGame = new dataEntities();
[HttpGet]
public ActionResult Index()
{
dbdata dbdata = new dbdata();
List<SelectListItem> listSelectItems = new List<SelectListItem>();
foreach (dbdata dbdata in dbGame.dbdata)
{
SelectListItem selectList = new SelectListItem();
{
selectList.Text = dbdata.GameTitle;
selectList.Value = dbdata.GameId;
};
dbdata.GameTitle = selectList.Text;
dbdata.Value = selectList.Value;
listSelectItems.Add(selectList);
}
return View(dbdata);
}
razor:
#using (Html.BeginForm())
{
#Html.ListBox(Model.GameTitle, new SelectList(Model.GameTitle));
}
any suggestions as to why it's doing this?
it's because GameTitle property is type of string(a sequence of characters).
so, try like this:
ViewBag.items = listSelectItems;
return View(dbdata);
in your View:
#Html.ListBox(Model.GameTitle,(IEnumerable<SelectListItems>)ViewBag.items)
you don't need:
dbdata.GameTitle = selectList.Text;
dbdata.Value = selectList.Value;
Or you can put a property of type List<SelectListItem> in your dbdata model, then assign listSelectItems to it then return View(dbdata);.
I am new in ASP.NET MVC.
I have a problem like below.
In Controller i have a code like this.
var students= db.Sagirdler.Where(x => x.SinifID == sinif.SinifID).
Select(m => new {m.Name, m.Surname}).ToList();
TempData["Students"] = students;
return RedirectToAction("Index", "MyPage");
This is my Index Action in MyPageController where I redirect and i call View.
public ActionResult Index()
{
ViewBag.Students = TempData["Students"];
return View();
}
And in View I use this code.
#{
ViewBag.Title = "Index";
var students = ViewBag.Students;
}
#foreach (var std in students)
{
#std.Name
<br/>
}
It says:
'object' does not contain a definition for 'Name'
What is the problem? How can I solve it?
You want to use
ViewBag.Students = students;
instead of TempData.
What I think you're trying to achieve would be better implemented like so:
Create a class
public class StudentViewModel
{
public string Name { get;set;}
public string Surname {get;set;}
}
then in your view using
#model IEnumerable<StudentViewModel>
#foreach (var student in Model)
{
...
}
And in your controller
var students = db.Sagirdler.Where(x => x.SinifID == sinif.SinifID)
.Select(m => new StudentViewModel { Name = m.Name, Surname = m.Surname} )
.ToList();
return View(students);