ViewDataまたはModelでforeachできないのはなぜですか?
-
10-07-2019 - |
質問
ビュー内のViewDataを反復処理しようとするとエラーが発生し続けます...ビューをIEnumerable(App.Models.Namespace)に強く入力し、Modelを使用してみても無駄になりました。 GetEnumerableメソッドがないか、無効な型キャストのためにエラーが発生しました...どのようにこれを行うのか考えていますか?
モデル...
public IQueryable<Product> getAllProducts()
{
return (from p in db.Products select p);
}
コントローラー...
public ActionResult Pricing()
{
IQueryable<Product> products = orderRepository.getAllProducts();
ViewData["products"] = products.ToList();
return View();
}
表示...
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Pricing
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Pricing</h2>
<div>
<select class="product">
<%foreach(var prod in ViewData["products"]){%>
<option><%=prod.Title %></option>
<%} %>
</select><select></select>
</div>
</asp:Content>
解決
キャストでこれを試してください:
foreach(var prod in (List<Product>)ViewData["products"])
他のヒント
foreach (var prod in (ViewData["products"] as IEnumerable<Product>))
同様の状況に陥りましたが、これでうまくいきました。
とにかくこんな風にやってるの?しない:
Inherits="System.Web.Mvc.ViewPage<IEnumerable<Product>>
ビューで。次に、コントローラーアクションで以下を実行します。
public ActionResult Pricing()
{
IQueryable<Product> products = orderRepository.getAllProducts();
return View(products.ToList(););
}
その後、 ViewData
を使用する必要はまったくありません。
<%foreach(Product prod in ViewData["products"]){%>
foreach(var prod in ViewData["products"] as IQueryable<Product>)
3つ以上のモデルからリストを取得していて、1つのリストに2つのモデルを表示する場合は、次のように使用する必要があります。 2つのモデルを作成するには、最初に学生、2番目に教師を作成します。
// Create Models
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public List<int> Marks { get; set; }
}
public class Teacher
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
//コントローラーの作成
public ActionResult Index()
{
List<Student> students = new List<Student>()
{
new Student {Id = 1, Name = "Vikas", Address = "Mohali", Marks = new List<int> {90, 23, 46,56} },
new Student {Id = 2, Name = "Rajeev", Address = "Mohali", Marks = new List<int> { 56, 78, 34, 67 }},
new Student {Id = 3, Name = "Ajay", Address = "Delhi", Marks = new List<int> {56, 78, 34, 56}}
};
List<Teacher> teachers = new List<Teacher>()
{
new Teacher {Id = 1, Name = "Arun Nagar", Address = "Delhi"},
new Teacher {Id = 2, Name = "Manish Kumar", Address = "Mohali"}
};
var Querylist = (from student in students
where student.Address == "Mohali"
select student.Name)
.Concat(from teacher in teachers
where teacher.Address == "Mohali"
select teacher.Name);
//get list in ViewBag
ViewBag.DataLIst = Querylist;
//get list in View Data
ViewData["DataLIst1"] = Querylist.ToList();
return View(Querylist.AsEnumerable());
}
//create View "Index.cshtml"
@foreach (var h in @ViewBag.DataLIst)
{
<h3>@h</h3>
}
@foreach (var s in @ViewData["DataLIst1"] as List< string>)
{
<h1>@s</h1>
}
所属していません StackOverflow