Pass List To View C# Linq - c#

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.

Related

How to show deserialized TempData object in view?

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>

Display a list from controller in the view MVC

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 />
}

passing data between controller methods

I'm creating asp.net mvc 5 application. in this application I have faced to problem passing data between controller methods.
Here the scenario step by step
I'm getting IEnumerable dataset to Create_Brochure method like this
public ActionResult Create_Brochure(IEnumerable<ProductsPropertiesVM> model)
{
IEnumerable<BrochureTemplateProperties> sample = model.Where....
return View(sample);
}
Then I need to save that IEnumerable<ProductsPropertiesVM> model to another IEnumerable object and use that in Create_Brochure_PDF() method
public ActionResult Create_Brochure_PDF()
{
IEnumerable<BrochureTemplateProperties> samplePDF = modelPDF....
return View(samplePDF);
}
for that I'did bit R&D part and came up solution with Sessions , Here the tutorial I followed
So I changed my code like this
but seems I'm having compile time errors though I followed exact as tutorial
1st controller method
[HttpPost]
[ValidateInput(false)]
public ActionResult Create_Brochure(IEnumerable<ProductsPropertiesVM> model)
{
IEnumerable<ProductsPropertiesVM> modelPDF = new IEnumerable<ProductsPropertiesVM>();
modelPDF = model;
IEnumerable<BrochureTemplateProperties> sample = model.Where(y => y.IsChecked)
.Select(y => new BrochureTemplateProperties
{
Property_ID = y.Property_ID,
IsChecked = y.IsChecked,
Property_Title = y.Property_Title,
Property_Value = y.Property_Value
});
TempData["TemplateData"] = modelPDF;
return View(sample);
}
2nd controller method
public ActionResult Create_Brochure_PDF()
{
IEnumerable<ProductsPropertiesVM> modelPDF = TempData["TemplateData"] as IEnumerable<ProductsPropertiesVM>;
IEnumerable<BrochureTemplateProperties> samplePDF = modelPDF.Where(y => y.IsChecked)
.Select(y => new BrochureTemplateProperties
{
Property_ID = y.Property_ID,
IsChecked = y.IsChecked,
Property_Title = y.Property_Title,
Property_Value = y.Property_Value
});
return View(samplePDF);
}
You can not instantiate interface..!
Replace
IEnumerable<ProductsPropertiesVM> modelPDF = new IEnumerable<ProductsPropertiesVM>();
modelPDF = model;
With
IEnumerable<ProductsPropertiesVM> modelPDF = model;
inside your Create_Brochure method.

why does ListBox populate individual characters from (.edmx) model on new line

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

'object' does not contain a definition error in Asp.Net MVC

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

Categories

Resources