2009-07-29 83 views
0

我目前使用EntityFramework绑定我的ASP.NET MVC项目到MySQL数据库,我的一个实体Product有一个包含ProductImages集合的Images属性。我构建了一个表单,允许用户修改给定的产品,并且此表单还包含用于编辑与该产品关联的所有图像的字段。对此事阅读Phil Haack'sDan Miser's的帖子后,我有什么需要做一个体面的想法,但我似乎无法使它出于某种原因...ASP.NET MVC - EntityFramework和绑定到列表

这里是我的产品形式:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<KryptonCMS.Models.Product>" %> 
<%@ Import Namespace="KryptonCMS.Core" %> 
<%@ Import Namespace="KryptonCMS.Models.ViewModels" %> 

<% using (Html.BeginForm()) 
    {%> 

     <ul class="gallery"> 
      <% 
       var index = 0; 
       foreach (var image in Model.ImageList.OrderBy(p => p.Order)) 
       { 
      %> 
      <li> 
       <% Html.RenderPartial("ProductImageForm", image, new ViewDataDictionary(ViewData) { { "index", index } }); %> 
      </li> 
      <% 
       index++; 
       } 
      %> 
     </ul> 

    <p> 
     <input type="submit" name="btnSave" value="Save" /> 
     <input type="submit" name="btnCancel" value="Cancel" /> 
    </p> 
<% } %> 

这里是ProductImageForm定义:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<KryptonCMS.Models.ProductImage>" %> 
<%@ Import Namespace="KryptonCMS.Core" %> 
<div> 
    <% 
     var fieldPrefix = string.Format("images[{0}]", ViewData["index"]); %> 
    <%=Html.Hidden(fieldPrefix + "ID", Model.ID) %> 
    <img src="<%=UtilityManager.GetProductImagePath(Model.Product.ID, Model.FileName, true) %>" 
     alt="" /><br /> 
    <label for="Description"> 
     Description:</label> 
    <%=Html.TextBox(fieldPrefix + "Description", Model.Description) %><br /> 
    <label for="Order"> 
     Order:</label> 
    <%=Html.TextBox(fieldPrefix + "Order", Model.Order)%><br /> 
</div> 

最后我的ProductsController操作:

public ActionResult Edit(int id) 
    { 
     var product = productsRepository.GetProduct(id); 

     if (product == null) 
      return View("NotFound", new MasterViewModel()); 

     // else 
     return View(ContentViewModel.Create(product)); 
    } 

    [AcceptVerbs(HttpVerbs.Post), ValidateInput(false)] 
    public ActionResult Edit(int id, FormCollection formCollection) 
    { 
     var product = productsRepository.GetProduct(id); 

     if (formCollection["btnSave"] != null) 
     { 
      if (TryUpdateModel(product) && TryUpdateModel(product.Images, "images")) 
      { 
       productsRepository.Save(); 

       return RedirectToAction("Details", new { id = product.ID }); 
      } 
      return View(ContentViewModel.Create(product)); 
     } 

     // else 
     return RedirectToAction("Details", new { id = product.ID }); 
    } 

HTML输出单个ProductImageForm看起来是这样的:

<div> 
    <input id="images[0]ID" name="images[0]ID" type="hidden" value="1" /> 
    <img src="/Content/ProductGallery/3/thumbs/car1.jpg" 
     alt="" /><br /> 
    <label for="Description"> 
     Description:</label> 
    <input id="images[0]Description" name="images[0]Description" type="text" value="FAST CAR" /><br /> 
    <label for="Order"> 

     Order:</label> 
    <input id="images[0]Order" name="images[0]Order" type="text" value="1" /><br /> 
</div> 

我已经试过各种方法来重新组织自己的状态,包括采取图像采集出来的产品形态,并把它放在自己的方法(这我真的不想这样做),但没有任何工作。我的方法在这里有明显的错误吗?

回答