2017-04-23 28 views
-1

所以我在我创建一个IEnumerable(中SelectListItem),然后使用这个列表来填充选项的选择,像这样一个观点:MVC jQuery将找不到选项

@ModelType MyModel 
@Code 
    ViewData("Title") = "Edit" 

    Dim TypeList As IEnumerable(Of SelectListItem) = { 
    New SelectListItem() With {.Value = "American", .Text = "American"}, 
    New SelectListItem() With {.Value = "Chinese", .Text = "Chinese"}, 
    New SelectListItem() With {.Value = "Mexican", .Text = "Mexican"}, 
    New SelectListItem() With {.Value = "Italian", .Text = "Italian"}, 
    New SelectListItem() With {.Value = "Japanese", .Text = "Japanese"}, 
    New SelectListItem() With {.Value = "Sandwich", .Text = "Sandwich"}, 
    New SelectListItem() With {.Value = "BBQ", .Text = "BBQ"}, 
    New SelectListItem() With {.Value = "Other", .Text = "Other"} 
} 
End Code 

<h2>Edit MyModel</h2> 

@Using (Html.BeginForm("Edit", "MyModel", FormMethod.Post, New With {.name = "frmEditMyModel", .id = "frmEditMyModel"})) 
    @Html.AntiForgeryToken() 

    @<div class="form-horizontal"> 
     <hr /> 
     <div class="text-danger">@(ViewData("mmError"))</div> 
     @Html.HiddenFor(Function(model) model.ID) 

...... 
     <div class="form-group" id="typecombo"> 
      @Html.LabelFor(Function(model) model.Type, htmlAttributes:=New With {.class = "control-label col-md-2"}) 
      <div class="col-md-10"> 
       <select class="form-control"> 
        @For Each item In TypeList 
         @<option>@item.Text</option> 
        Next 
       </select> 
       @Html.HiddenFor(Function(model) model.Type, New With {.id = "type"}) 
       @Html.ValidationMessageFor(Function(model) model.Type, "", New With {.class = "text-danger"}) 
      </div> 
     </div> 
     <div class="form-group" id="typebox"> 
      <div class="col-md-2 col-xs-2" style="text-align:right"></div> 
      <div class="col-md-10 col-xs-10"> 
       @Html.EditorFor(Function(model) model.Type, New With {.htmlAttributes = New With {.class = "form-control"}}) 
      </div> 
     </div> 
....... 

    </div> End Using 

所以,因为这是一个已经存在的东西的编辑页面,我希望select元素具有与MyModel.Type相同的选项。最重要的是,如果type是“其他”,则显示用户可以在其中输入内容的文本框。所以这里是我有的jquery:

@Section Scripts 
    @Scripts.Render("~/bundles/jqueryval") 

<script> 
    $(function() { 

     // First see if the current 'type' is in the dropdown options or not 
     var exists = false; 
     var curType = "@Model.Type"; 
     $('#typecombo option').each(function() { 
      if (this.Text == curType) { 
       exists = true; 
       $("#typecombo select").val(curType).change(); 
      } 
     }); 
     if (exists == false) { 
      $("#typecombo select").val("Other").change(); 
     } 

     // Toggle show/hide of Type textbox if selection in Type combo is 'Other' 
     var selection = $('#typecombo :selected').text(); 
     selection.trim(); 
     if (selection === 'Other') { 
      $('#typebox').show(); 
     } 
     else { 
      $('#typebox').hide(); 
     } 
     $('#typecombo').change(function() { 
      var selection = $('#typecombo :selected').text(); 
      selection.trim(); 
      if (selection === 'Other') { 
       $('#typebox').show(); 
      } 
      else { 
       $('#typebox').hide(); 
      } 
     }) 
</script> 
End Section 

无论出于何种原因,它没有找到已有的值。我试图在页面打开时放置一个显示@ Model.Type的警报 - 正确显示。但这并不重要 - 每次它最终都会将所选值设置为“其他”并显示文本框,因为它没有在选择中找到它作为选项。为什么??

+0

只需使用'@ Html.DropDownListFor(Function(model)model.Type,TypeList)'并删除@ Html.HiddenFor()和@ Html.EditorFor() 'Type' - 你的脚本不需要选择正确的选项。如果您希望文本框添加其他值,请将其绑定到其他属性 –

回答

1

看起来像你需要这部分

 if (this.Text == curType) { 
      exists = true; 
      $("#typecombo select").val(curType).change(); 
     } 

改变

 if (this.text == curType) { 
      exists = true; 
      $("#typecombo select").val(curType).change(); 
     } 

JavaScript是区分大小写。注意这一点。并且DOM元素属性具有名称text

+0

@charlietfl jQuery适用于DOM元素,而不适用于模型 –