2015-07-22 74 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

還強烈建議您使用樣式表,而不是內嵌樣式(看我上面鏈接的小提琴 –