문제

나는보기에서 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