2011-08-11 4 views
0

를 추가 내가보기에서 사용하는 경우 나는 다음과 같은 부분보기 내가 예를 들어 EditorTemplate 내부에 그것을 사용하는 경우 드롭 다운리스트 이름은 수정되지 않은 남아 있지만Html.DropDownList가 자동으로 접두사

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MvcMusicStore.Models.Product>" %> 

<span class="price">Precio: <em><strong><%: Html.Price(Model) %></strong></em></span> 

<div class="wrapper"> 
<% if (Model.Active) { %> 
    <% using (Html.BeginForm("AddToCart", "ShoppingCart", new { id = Model.ProductId }, FormMethod.Post, new { id = "addtocart-form" })) {%> 

      <% if (Model.MetricType == (int)ProductMetricType.Units) { %> 
       <%: Html.DropDownList("Quantity" , new SelectList(DropDownLists.Units , "Value", "Key" , Model.MetricType)) %> 
      <% } else { %> 
       <%: Html.DropDownList("Quantity", new SelectList(DropDownLists.Kilograms, "Value", "Key", Model.MetricType)) %> 
      <% } %> 

     <input type="submit" value="Comprar" /> 
    <% } %> 
<% } else { %> 
    <span class ="buttonBig">Sin existencias!</span> 
<% } %> 
</div> 

을 BuyProduct.ascx 한 :

<%@ Import Namespace="MvcMusicStore"%> 
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MvcMusicStore.Models.Product>" %> 
<li> 
    <h3><%: Html.Truncate(Model.Name, 25) %></h3> 
    <p><a href="<%: Url.Action("Details", "Store", new { id = Model.ProductId }) %>"><img alt="<%: Model.Name %>" src="<%: Model.ProductArtUrl %>" width="180" height="165" /></a></p> 
    <% Html.RenderPartial("BuyProduct", Model); %> 
</li> 

내 컨트롤러 코드 :

public ActionResult AddToCart(Guid id, int quantity = 1) { 

미주리의 이름의 접두사 del 클래스가 DropDownList 이름 (Product.Quantity 삽입 수량)에 추가되고 더 이상 올바른 값을 얻지 못합니다. 두 가지 경우 모두 해결하는 방법에 대한 아이디어가 있습니까?

답변

1

DropDownList 확장 프로그램에서 SelectInternal 메서드에는 고유 한 이름을 생성하기위한 아래 구문이 포함되어 있습니다.

string fullName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name); 

템플릿을 사용하고 있기 때문에 포함 된 모델 이름 "제품"이 회원 이름 "수량"과 함께 렌더링됩니다. 아마도 여러 개의 템플릿을 사용할 때 이름 충돌을 피할 수 있습니다.

이 동작을 "오버라이드"하려면 DropDownList 확장의 사용자 지정 구현이 가장 좋은 방법 일 것입니다.