开发者

Html.DropDownList automatically adding prefix

I have the following partial view BuyProduct.ascx

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

If I use it in a View the dropdownlist name remains unmodified but if I use it inside an EditorTemplate for example :

<%@ 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" 开发者_C百科/></a></p>
    <% Html.RenderPartial("BuyProduct", Model); %>
</li>

My Controller code :

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

A prefix with the name of the model class is being added to the DropDownList name ( Product.Quantity inset of Quantity) and my action no longer gets the correct value, Any idea on how to solve it for both cases?


In the DropDownList extensions the SelectInternal method contains the construct below for generating a unique name.

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

I guess it is due to the fact that your are using templates, the containing model name "Product" is rendered along with the member name "Quantity". Probably to avoid name clashes when using multiple templates.

If you would really want to "override" this behaviour, a custom implementation of a DropDownList extension would probably be your best bet.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜