2

I am working on Visual C# MVC project. I am using EF databse first approach in my model.My project is about developing an appmarket where users can buy apps. I used partial view in home page which will contain details of different apps in different partial views.Like App1 in 1st partial view, App2 in 2nd partial view, so on. For this I used for each loop.

In each partial view there is link called MoreInfo, so when user clicks on that they will go inside MoreInfo page. In database I have fields such as app info, app cost, description etc which will be displayed inside MoreInfo page. All these fields are in one table called Apps table.

When i follow Enumerable.FirstOrDefault approach I am able to retrieve only first record from database. But now my problem is I need to retrieve first record in my first MoreInfo view, second record in second MoreInfo view and so on.

My code for controller is :

public ActionResult MoreInfo()
{    
    var e = Enumerable.FirstOrDefault(db.AppmarketApps);

    return View(e);
}

and in view i used :

@model Market.Models.AppmarketApp

<h3><span class="grey"><a href="#">MARKETPLACE</a> &#8250;</span> @Model.Description</h3>

So here I am getting first record description for all MoreInfo views which I don't want. Thanks in advance.

4

3 回答 3

3

扩展我的评论,您应该使用应用程序的 ID,然后将其传递给MoreInfo这样的东西:

public ActionResult MoreInfo(int id)
{    
    var e = db.AppmarketApps.Where(x => x.ID == id).FirstOrDefault();
    return View(e);
}
于 2013-08-07T10:44:16.590 回答
2

将感兴趣的应用程序的 ID 传递到 MoreInfo 的链接中,并根据该 ID 查询您的数据。

于 2013-08-07T10:41:55.603 回答
0

最后在玩了我的代码之后,我知道了解决方案。

之前我有两个控制器,分别称为 HomeController 和 MoreInfoController。但由于 MoreInfo 链接位于 Homepage 本身中,因此我删除了 MoreInfo 控制器并将代码添加到 HomeController 本身。这是我添加的代码:

public ActionResult MoreInfo(short id)
    {
        var e = db.AppmarketApps.Where(x => x.ID == id).FirstOrDefault();
        return View(e);
    }

在这里,我将 id 作为参数传递,大写的“ID”是我的主键。因为我想每个视图只显示一条记录,所以我使用了 FirstOrDefault 方法。

在我看来,在此之后,我更改了这样的代码:

@Html.ActionLink("MoreInfo", "MoreInfo", new { id = item.ID }, new { @class = "greyButton" })

这里第一个参数“MoreInfo”是链接名称,第二个参数“MoreInfo”是链接。正如您所看到的,当用户单击 MoreInfo 链接时,id 将通过使用传递给该链接

new { id = item.ID }. 

类用于样式目的。要清楚上述代码的作用是,它将 id 传递给 MoreInfo 链接,并将显示 AppmarketApps 表中存在的数据。

最后,如果我想显示“名称”,我在视图中使用了@Model.Name,如果我想显示“描述”,我使用了@Model.Description,依此类推。

感谢所有在这方面帮助我的人。

于 2013-08-07T17:04:52.327 回答