2015-07-22 75 views
0

我工作的人在网站上和我有一个小问题产品画廊。我承认我对MVC和EF有点新,所以我有这个问题。 “我的索引”视图显示了我的产品,但它显示在一个垂直向上和向下的列表中,就像我创建它应该做的表一样。创建与EF 6和MVC 5

我想要做的是创建一个画廊,他们并排连续说4,然后移动到下一行等等(上帝,我知道这是有道理的)。这是我的看法

@model IEnumerable<AccessorizeForLess.ViewModels.DisplayProductsViewModel> 

@{ 
    ViewBag.Title = "Index"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

<h2>Index</h2> 

<p> 
    @Html.ActionLink("Create New", "Create") 
</p> 
<table class="table"> 
    @foreach (var item in Model) 
    { 
     <tr> 
      <td> 
       @Html.DisplayFor(modelItem => item.Name)<br /> 
       <img src="@item.Image.ImagePath"/> 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.Price) 
      </td> 
      <td> 
       @Html.ActionLink("Edit", "Edit", new { id = item.Id }) | 
       @Html.ActionLink("Details", "Details", new { id = item.Id }) | 
       @Html.ActionLink("Delete", "Delete", new { id = item.Id }) 
      </td> 
     </tr> 
    } 

</table> 

当然,它正在做它应该做的事情。有人可以帮我解决这个问题吗?

+1

考虑使用div元素与'float:left;'和'width:25%' –

+0

@StephenMuecke我想感谢您在过去几天的所有帮助。当我学习MVC – PsychoCoder

+0

快乐的来龙去脉时,它意味着很多。我假设你已经完成了它,但[这里是一个简化的小提琴](http://jsfiddle.net/7vkc8myy/),显示你可以做什么。 –

回答

0

如果任何人有任何接近这个问题,这是我想出了和它的作品只是我一直在寻找的方式:

@model IEnumerable<AccessorizeForLess.ViewModels.DisplayProductsViewModel> 

@{ 
    ViewBag.Title = "Index"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

<h2>Products</h2> 

<p> 
    @Html.ActionLink("Create New", "Create") 
</p> 
    @foreach (var item in Model) 
    { 
     <div style="float:left; width:25%"> 
      @Html.DisplayFor(modelItem => item.Name)<br /> 
      <input id="Hidden1" type="hidden" value="@item.Id" /> 
      <div>[email protected](modelItem => item.Price)</div> 
      <div><img src="@item.Image.ImagePath" /></div> 
      <div>&nbsp;</div> 
      <div>Quantity: <input type="text" id="quantity" style="width:50px;" /></div> 
      <div>@Html.ActionLink("Details", "Details", new { id = item.Id })</div> 
     </div> 
    } 

,不过我有一个问题,在我的控制器如何我可以格式化价格作为货币,我想这一点:

IEnumerable<DisplayProductsViewModel> model = products.Select(p => new DisplayProductsViewModel() 
{ 
    Id = p.ProductId, 
    Name = p.ProductName, 
    Image = p.ProductImage, 
    Price = string.Format("{0:C}",p.ProductPrice) 
}).ToList(); 

并且得到错误:

LINQ to Entities does not recognize the method 'System.String Format(System.String, System.Object)' method, and this method cannot be translated into a store expression

+1

您不应该在查询中格式化值,而是将[[DisplayFormat(DataFormatString =“{0:C}”)]'添加到属性中。 '@ Html.DisplayFor()'帮助程序将正确渲染它(删除视图代码中的前导'$') –

+0

请注意''和'

Quantity:
'正在生成无效的html(重复的'id'属性)。在任何情况下,它目前还不清楚为什么你有这些,因为你没有出现有一个表格(如果你这样做,当你发布反正这绝不会绑定到你的模型) –

+0

还强烈建议您使用样式表,而不是内嵌样式(看我上面链接的小提琴 –