문제

현재 EntityFramework를 사용하여 ASP.NET MVC 프로젝트를 MySQL 데이터베이스에 바인딩하고 있으며 내 엔티티 중 하나 인 제품에는 제품 모음이 포함 된 이미지 속성이 있습니다. 사용자가 주어진 제품을 수정할 수있는 양식을 작성 했으며이 양식에는 해당 제품과 관련된 모든 이미지를 편집하기위한 필드가 포함되어 있습니다. 읽고 나서 Phil Haack 's 그리고 Dan Miser 's 문제에 대한 게시물 나는 어떤 일이 일어나야하는지에 대한 괜찮은 아이디어를 가지고 있지만 어떤 이유로 든 효과가없는 것 같습니다 ...

내 제품 양식은 다음과 같습니다.

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

그리고 마지막으로 내 제품 콘트롤러 작업 :

    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>

나는 이미지 컬렉션을 제품 양식에서 꺼내고 자체적으로 배치하는 등 양식을 재구성하는 모든 종류의 방법을 시도했지만 (실제로하고 싶지 않은) 아무것도 작동하지 않습니다. 여기에 내 접근 방식에 뻔뻔스럽게 문제가 있습니까?

도움이 되었습니까?

해결책

입력 이름에 점이 누락되었습니다.

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