简体   繁体   中英

MVC3 Razor View Engine

I just started to learn MVC3. Have been coding in traditional ASP.NET For a while and now would like to move to MVC.

Some things that i don't understand (probably just been used differently then in traditional ASP.NET)

I'm trying to write a simple news module that will display news and allow to insert comment for them.

So first step is, i created a tables on my SQL server: TblNews TblCategories TblComments

Created Linq2SQL data Class in Models folder and named it News.dbml Created Controller HomeController.cs and a method called Index() in it. Look like this:

public ActionResult Index()
        {
            Models.NewsDataContext db = new Models.NewsDataContext();
            var Model = (from n in db.TblNews
                         select new
                         {
                             ID = n.ID,
                             Title = n.Title,
                             Description = n.Description,
                             Category = n.TblCategory.CategoryName
                         });

            return View(Model);
        }

As you see I'm trying to select all the news and their category names (TblNews and TblCategories do have relationship between them)

After that I'm returning the data that i got from the query.

In the View i have:

@{
    ViewBag.Title = "News Index Page";
}

<table>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                ID: @item.ID<br />
                Title: @item.Title<br />
                Description: @item.Description<br />
                Category: @item.Category
            </td>
        </tr>
    }
</table>

Which should return something like:

ID: 4
Title: asd
Description: asd
Category: 2

That's my sample data from tables.

When i run the page it gives me error message:

'object' does not contain a definition for 'ID'

but when i focus my mouse on "item" variable it actually contain:

{ ID = 4, Title = asd, Description = asd, Category = Test2 }

I also tried to return Linq Query as list by adding .ToList() to the end of it.

Can anybody give me any hints and get me into the right direction?

Sorry if i explained something incorrectly. English is not my primary language. Please ask if you need any more information.

Thank you very much.

PS I'm using Visual Studio 2012

You're missing your model declaration at the top of your view so will default to type object . When you do item.ID you're doing object.ID , which is why it complains that object does not contain a definition for ID

Add the following:

@model List<Models.NewsDataContext>

Also, make sure you evaluate the LINQ query by calling .ToList() before your return Model, that way the query is already executed against the database before it hits the view:

return View(Model.ToList());

Edit: Actually, in your LINQ query you are selecting into an anonymous type, you must use a concrete class in order to use this in your view . The below assumes your TblNews corresponds to a class called News :

var Model = (from n in db.TblNews
                     select new News //class name here
                     {
                         ID = n.ID,
                         Title = n.Title,
                         Description = n.Description,
                         Category = n.TblCategory.CategoryName
                     });

        return View(Model.ToList());

Then change your model to:

@model List<News>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM