لماذا لا يمكنني foreach من خلال بلدي ViewData أو نموذج؟

StackOverflow https://stackoverflow.com/questions/1441215

  •  10-07-2019
  •  | 
  •  

سؤال

وأظل الحصول على أخطاء تحاول تكرار خلال بلدي ViewData في رأي هناك ... حتى أنني حاولت كتابة بقوة بغية IEnumerable (App.Models.Namespace) وباستخدام نموذج، ولكن دون جدوى. إما أحصل على خطأ لعدم وجود طريقة 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>)

إذا كنت تحصل على قائمة من أكثر من نموذج وتريد أن تظهر اثنين من نموذج في قائمة واحدة من يجب استخدام مثل هذا .. إنشاء نموذج اثنين أولا هو الطالب والثاني هو المعلم.

// 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>
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top