我目前正在使用EntityFramework将我的ASP.NET MVC项目绑定到MySQL数据库,我的一个实体Product有一个包含ProductImages集合的Images属性。我已经构建了一个表单以允许用户修改给定的产品,此表单还包括用于编辑与该产品关联的所有图像的字段。阅读 Phil Haack's 和< a href =“http://www.distribucon.com/blog/ASPNETMVCRC1BindingAList.aspx”rel =“nofollow noreferrer”> Dan Miser的关于此事的帖子我对需要发生的事情有了一个很好的了解,但是我似乎无法让它出于某种原因......

这是我的产品表格:

<%@ 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 });
    }

单个ProductImageForm的HTML输出如下所示:

<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>

我尝试了各种重组表单的方法,包括将Image集合从Product表单中取出并放在自己的表单中(我真的不想这样做),但没有任何工作。我的方法在这里有什么严重错误吗?

有帮助吗?

解决方案

您输入的名称中缺少点:

<%= Html.Hidden(fieldPrefix + ".ID", Model.ID) %>
<%= Html.TextBox(fieldPrefix + ".Description", Model.Description) %>
<%= Html.TextBox(fieldPrefix + ".Order", Model.Order) %>

查看此博文: http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries。 ASPX

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top