locked
getting problem with Notsupportedexception with foreach RRS feed

  • Question

  • Unable to create a constant value of type 'System.Object'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.

    Here is code,

    @foreach (var item in Model)
        {  
        <article class="blog">
     
            <a href="@Url.Action("Details", "Blog", new { id = item.ArticleId })" class="post-photo">
                <img src="@item.ImagePath" width="200" height="150" alt="@item.Title" />
            </a>
     
            <header>
                <h1>@Html.ActionLink(item.Title, "Details", "Blog", new { id = item.ArticleId }, null)</h1>
                <p><time pubdate="pubdate">@Html.DisplayFor(a => item.Published)</time></p>
            </header>
         
            <div class="post">
                @(new HtmlString(item.ArticleIntro))
                <a href="@Url.Action("Details", "Blog", new { id = item.ArticleId })">read more</a>
            </div>
     
        </article>
        <div class="clear"></div>
        }

    error shows on foreach loop. and i don't get it.
    • Edited by MSabbir Thursday, September 8, 2011 9:03 AM
    • Moved by Jackie-Sun Thursday, September 29, 2011 6:41 AM ASP.NET related (From:ADO.NET Entity Framework and LINQ to Entities)
    Thursday, September 8, 2011 8:18 AM

Answers

All replies

  • In Asp.net MVC, you cannot loop through your model, instead you can loop through any IEnumaerable object of model.

    Foreach loop works only on objects which implements IEnumerable interface.

    I hope you get it clear.


    Pravin Arote, MCTS - Web Technologies
    Thursday, September 8, 2011 10:52 AM
  • this is my home controller:

    public class HomeController : Controller
        {
            private MyWebSiteEntities _db = new MyWebSiteEntities();

            public ActionResult Index()
            {           
                var list = (from a in _db.Articles
                            where a.Status.Equals(1)
                            orderby a.Published descending
                            select a).Take(2);
                return View(list);
            }       
        }

    and this is my view/home index.cshtml

    @model IEnumerable<MyWebSite.Models.Article>
     
    @{
        ViewBag.Title = "Welcome to my web site";
    }
     
    <h1>@ViewBag.Title</h1>
    <img class="photo" src="/Content/images/pula.jpg" width="618" height="227" alt="Roman Amphitheater in the city of Pula" />

        @foreach (var item in Model)
        {  
        <article class="blog">
     
            <a href="@Url.Action("Details", "Blog", new { id = item.ArticleId })" class="post-photo">
                <img src="@item.ImagePath" width="200" height="150" alt="@item.Title" />
            </a>
     
            <header>
                <h1>@Html.ActionLink(item.Title, "Details", "Blog", new { id = item.ArticleId }, null)</h1>
                <p><time pubdate="pubdate">@Html.DisplayFor(a => item.Published)</time></p>
            </header>
         
            <div class="post">
                @(new HtmlString(item.ArticleIntro))
                <a href="@Url.Action("Details", "Blog", new { id = item.ArticleId })">read more</a>
            </div>
     
        </article>
        <div class="clear"></div>
        }
    as a beginner i don't understand problem... plz help me ...

    Thursday, September 8, 2011 11:05 AM
  • Hello MSabbir,

    Since you are using ASP.NET MVC, I recommend you to post in the following forum: http://forums.asp.net/1227.aspx/1?ADO+NET+Entity+Framework+LINQ+to+SQL+NHibernate

    I hope you can get the solution soon there.

     

    have a nice day,

     


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, September 13, 2011 8:13 AM