c# - Razor. @foreach statement cannot operate because does not contain public definition for GetEnumerator -
c# - Razor. @foreach statement cannot operate because does not contain public definition for GetEnumerator -
as know iqueryable same ienumerator both contains getenumerator method, when i'm passing partial view iqueryable object, razor said there no definition getenumerator. here interface definition:
public interface icomments : idisposable { iqueryable<comments> getpostcomments(int postid); //void add(comments comment); //void update(comments comment); //void remove(int id); }
here repository:
public class commentsrepository: icomments, idisposable { private blogaspnetentities db; public commentsrepository(blogaspnetentities db) { this.db = db; } public iqueryable<comments> getpostcomments(int postid) { var = in db.comments orderby a.posted descending a.postid == postid select a; homecoming all; } private bool disposed = false; protected virtual void dispose(bool disposing) { if (!this.disposed) { if (disposing) { db.dispose(); } } this.disposed = true; } public void dispose() { dispose(true); gc.suppressfinalize(this); } }
and partial view:
@model blogaspnetmvc.models.comments <div class="row"> @foreach (var item in model) { <div class="col-md-12"> <div class="panel panel-default"> <div class="panel-heading"> <h3 class="panel-title">@item.author " сказал " @item.posted ":"</h3> </div> <div class="panel-body"> @html.raw(httputility.htmldecode(@item.comment)) </div> </div> </div> } </div>
i'm can't what's wrong because can see of iquerybale. p.s. , here model:
public partial class comments { public int id { get; set; } public string comment { get; set; } public nullable<int> author { get; set; } public nullable<system.datetime> posted { get; set; } public nullable<int> postid { get; set; } public virtual blog_post blog_post { get; set; } }
p.p.s. parent viewmodel:
<div class="col-md-12"> @html.partial("~/views/comments/details.cshtml", new blogaspnetmvc.models.comments()) </div>
you seem missed add together comments class model definition.
try this...
@model ienumerable<blogaspnetmvc.models.comments>
when model needs iteration, comments class should implement ienumerable/iqueryable interface, in case not implemented. or partial view should ienumerable/iqueryable object model.
you have pass list partial view rendering (model binding) solve problem.
this tried , worked me.
the controller should pass iqueriable object view
iqueryable<comments> commentslist = commentsrepository.getpostcomments(1); homecoming view(commentslist);
in parent view (which renders partial view) should utilize @model directive as,
@model iqueryable<webapplication1.models.comments>
i modified @model directive in partial view as,
@model iqueryable<webapplication1.models.comments>
try 1 time , should work.
c# asp.net-mvc asp.net-mvc-4 razor
Comments
Post a Comment