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);
Related
I'm trying to pass a list of values from the controller to the view, but apparently I got this issue where the list cannot be passed. I already tried passing one value and it has no problem. But when I try to pass list, it show the following error -
The model item passed into the dictionary is of type
'System.Collections.Generic.List`1[vidly.Models.pelanggan]', but this
dictionary requires a model item of type 'vidly.models.pelanggan'.
I have this model -
public class pelanggan
{
public string Nama { get; set; }
}
The controller code -
// GET: Pelanggan
public ActionResult Index()
{
var name = new List<pelanggan> {
new pelanggan {Nama = "Paidi" },
new pelanggan {Nama = "Budi" }
};
return View(name);
}
This is my view file
#model vidly.Models.pelanggan
#{
ViewBag.Title = "index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Customer</h2>
#foreach(var Nama in Model.pelanggan)
{
<li>#Nama.Nama</li>
}
I already tried to create a ViewModel but it also showing same error. Can you point where the error is?
You are returning a List<T> from the controller. So, the model declared in your view should be able to receive a list and iterate over it.
Replace the model declaration in the view file with following -
#using vidly.Models;
#model IEnumerable<pelanggan>
Then you can iterate/loop over the model like -
#foreach(var p in Model) // p represents a "pelanggan" object in the list
{
<li>#p.Nama</li>
}
The problem is the missmatch of types between what you return return View(name); and what te view expects #model vidly.Models.pelanggan You could change to #model List<vidly.Models.pelanggan> but instead I'd say:
public class pelanggan
{
public List<string> Namas { get; set; }
}
Then
public ActionResult Index()
{
var model = new pelanggan {
name = new List<string> {
"Paidi",
"Budi"
}
};
return View(model);
}
And finally in your view
#foreach(var Nama in Model.Namas)
{
<li>#Nama</li>
}
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);
}
}
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 />
}
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 using MVC5, Razor, Entity Framework, C#. I am trying to pass a value of a dorpdown list using a link.
my model is
public class TestVM
{
public string TheID { get; set; }
}
I am loading an enum into a IEnumerable<SelectListItem>.
My enum is
public enum DiscountENUM
{
SaleCustomer,
SaleCustomerCategory,
SaleProduct,
SaleProductCategory,
SaleCustomerAndProduct,
SaleCustomerAndProductCategory,
SaleCustomerCategoryAndProductCategory,
PurchaseVendor,
PurchaseVendorAndProduct,
PurchaseVendorAndProductCategory,
PurchaseProduct,
PurchaseProductCategory,
Unknown
}
I am using the index method of the home controller
public ActionResult Index()
{
ViewBag.ListOfDiscounts = SelectListDiscountENUM();
TestVM d = new TestVM();
return View(d);
}
Where I load the ListOfDiscounts using:
private IEnumerable<SelectListItem> SelectListDiscountENUM()
{
List<SelectListItem> selectList = new List<SelectListItem>();
var listOfEnumValues = Enum.GetValues(typeof(DiscountENUM));
if (listOfEnumValues != null)
if (listOfEnumValues.Length > 0)
{
foreach (var item in listOfEnumValues)
{
SelectListItem sVM = new SelectListItem();
sVM.Value = item.ToString();
sVM.Text = Enum.GetName(typeof(DiscountENUM), item).ToString();
selectList.Add(sVM);
}
}
return selectList.OrderBy(x => x.Text).AsEnumerable();
}
My create method which is called from the view is
public ActionResult Create(TestVM d, string TheID)
{
return View();
}
My Index view is
#model ModelsClassLibrary.Models.DiscountNS.TestVM
<div>#Html.ActionLink("Create New", "Create", new { TheID = Model.TheID})</div>
<div>
#Html.DropDownListFor(x => x.TheID, #ViewBag.ListOfDiscounts as IEnumerable<SelectListItem>, "--- Select Discount Type ---", new { #class = "form-control" })
</div>
The problem is in the following line in the View
<div>#Html.ActionLink("Create New", "Create", new { TheID = Model.TheID })</div>
I have tried adding a model with the name of the field as "TheID"... no luck. Also, added a string field in the parameter, no luck. I looked at the FormControl object, and there was nothing in it either! I suspect something has to be added at the Route level in the helper, but I don't know what.
Model.TheID is always null. Even when I select an item in the DropDownListFor.
Does anyone have an idea how I can capture the select value of the DropDownListFor and send it into the Html.ActionLink TheID?