2014-01-30 2 views
0

MVC를 사용하고 ViewData를 사용하여 값을 조작하고 있습니다. 나는MVC에서 선택한 값에 대한 드롭 다운의 다중 옵션 저장

내가 드롭 다운에서 사용자의 선택에 따라 다른 값을 저장할 방법 적 목록 항목의 텍스트와 가치 즉, 가격과 크기 옵션과 함께
IList<SelectListItem> sizeList = new List<SelectListItem>(); 
foreach (var item in sizeInfo) 
    { 
    SelectListItem listItem = new SelectListItem(); 
    listItem.Value = item._price.ToString(); 
    listItem.Text = item._sizeOption; 
    sizeList.Add(listItem); 
    } 
    ViewData["SizeList"] = sizeList; 
    productInstance.SizeCount = true; 

같은 컨트롤러의 코드가 있습니다.

내 드롭 다운

@Html.Raw(@Html.DropDownList("SizeList", ViewData["SizeList"] as SelectList, new { @id = "ddsize1" })) 

내가 어떻게 다운 특정 선택 드롭의 사용자 선택에 따라 텍스트와 값보다 다른 하나 개 이상의 속성을 추가 할 수있는이을 ViewData를 가져옵니다.?

답변

0

컨트롤러 : 당신이 한 번 더 목록을 취할 수

, 그것은 가격과 원하는 속성이 포함되어 있습니다. 해당 목록을 숨겨진 필드에 배치하십시오. 보기에서

IList<SelectListItem> CustomList = new List<SelectListItem>(); 
foreach (var item in sizeInfo) 
{ 
SelectListItem listItem = new SelectListItem(); 
listItem.Value = item.yourattribue.ToString(); 
listItem.Text = item._sizeOption; 
sizeList.Add(listItem); 
} 
ViewData["CustomList"] = CustomList; 

:

 @using (Html.BeginForm()) 
      { 
     <div id="divPrice1"style="display:none"> 
     <table> 
     <tr> 
     <td class="divSizehPrice" style="text-align:right">@Html.Raw(@Model.dtcurrentCurrency.Rows[0]["HTMLENTITY"])</td> 
     <td class="divSizehPrice" style="width:24%; text-align:left; color:Red;"><div id="PriceDiv1"></div></td> 
     <td class="divSearchRRPPrice" style="text-align:right"><div id="divOffer">RRP&nbsp;@Html.Raw(@Model.dtcurrentCurrency.Rows[0]["HTMLENTITY"])</div></td> 
     <td class="divSearchRRPPrice" style="width:70%; text-align:left"><div id="PriceDiv2"></div></td> 
     </tr> 
     </table>      
    </div> 
    <table style="width:100%"> 
    <tr>   
     @if (@Model.SizeCount == true) 
     { 
      <td><label>Size:&nbsp;</label></td> 
      <td> 
      <span style="display:none;"> 
      @Html.DropDownList("CustomList", ViewData["customList"] as SelectList, new { @id = "ddPrice" }) 
      </span> 
      @Html.Raw(@Html.DropDownList("SizeList", ViewData["SizeList"] as SelectList, new { @id = "ddsize1" })) 
      </td> 
     } 
     else 
     { 
      if (@Model.dtProduct.Rows[0]["Size"] != null) 
      { 
       if (@Model.dtProduct.Rows[0]["Size"].ToString().Length > 0) 
       { 
        <td colspan=2><p><label>Size:</label>@Model.dtProduct.Rows[0]     ["Size"]</p></td> 
       } 
      } 
     }   
    </tr>      
    </table> 
    <br /> 
    <a href="@Url.Action("Currency", "Product")">Change currency</a> 
    <br /> 
    @Html.Hidden("ProductId", @Model.dtProduct.Rows[0]["ProductId"]) 
    @Html.Hidden("ProductPriceId", @Model.dtProduct.Rows[0]["ProductPriceId"]) 
    @Html.Hidden("ProductPrice", @APrice) 
    @Html.Hidden("tempPrice", null, new { @id = "tempPrice" }) 
    @Html.Hidden("ddlSize",null, new { @id="ddlSizeId" }) 

    <input type="submit" value="Add to Shopping Cart" data-theme="f" /> 
    } 

자바 스크립트/JQuery와 :

<script type="text/javascript"> 
$('#ddsize1').change(function() { 
    var selectedValue = $('option:selected', $(this)).val(); 
    var selectedText = $('#ddsize1 option:selected').text(); 
    document.getElementById('ddlSizeId').value = selectedText; 
    var offerPrice = @Model.offerPrice; 
    if(offerPrice == 0) 
    { 
     var price = selectedValue * @Model.dtcurrentCurrency.Rows[0]["EXCHANGERATE"]; 
     $("#priceTable1").hide(); 
     document.getElementById('tempPrice').value=price.toFixed(2); 
     document.getElementById('PriceDiv1').innerHTML = price.toFixed(2); 
     $("#divPrice1").show(); 
     $("#divOffer").hide(); 
     // return false; 
    } 
    else{ 
     var el = document.getElementById("ddPrice"); 
     for(var i=0; i<el.options.length; i++) 
     { 
      var txtPrice = el.options[i].text; 
      var priceMatch = selectedValue * 1; 
      var MatchPrice = priceMatch.toFixed(2); 
      if (txtPrice == MatchPrice) { 
      $("#priceTable1").hide(); 
      var txtOfferPrice = el.options[i].value * @Model.dtcurrentCurrency.Rows[0]["EXCHANGERATE"]; 
      var txtOriginalPrice = el.options[i].text * @Model.dtcurrentCurrency.Rows[0]["EXCHANGERATE"]; 
      document.getElementById('tempPrice').value = txtOfferPrice.toFixed(2); 
      document.getElementById('PriceDiv1').innerHTML = txtOfferPrice.toFixed(2); 
      document.getElementById('PriceDiv2').innerHTML = txtOriginalPrice.toFixed(2); 
      $("#divPrice1").show(); 
      } 
     } 
    } 
}); 
</script> 

모델 :

public string tempPrice {get;set;} 

이제 컨트롤러에있는 당신의 속성에 액세스 할 수 있습니다. 나는 그것이 당신의 문제에 충분히 도움이되기를 바랍니다.

관련 문제