如何在MVC 5中动态分配DropDown值查看

时间:2017-02-08 19:39:29

标签: asp.net-mvc razor

我有一个包含基本地址信息的模型:

public class AddressModel()
{
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
}

当我将它传递到我的视图中的表单时,我将值放入一个文本框中,但State除外,它是一个硬编码的下拉框,用于包含美国的状态:

<form>
   <div class="container-fluid">
         <div class="col-md-3">
              <div class="form-group">
                   <!-- First Name -->
                   <label for="first_name_id" class="control-label">First Name</label>
                   <input type="text" class="form-control" id="first_name_id" name="first_name" placeholder="First Name" value="@Html.ViewData.Model.Patient.FirstName.First().ToString().ToUpper()@Html.ViewData.Model.FirstName.Substring(1).ToLower()">
                   </div>
              </div>
              <div class="col-md-3">
                   <div class="form-group">
                       <!-- Last Name -->
                       <label for="last_name_id" class="control-label">Last Name</label>
                       <input type="text" class="form-control" id="last_name_id" name="last_name" placeholder="Last Name" value="@Html.ViewData.Model.Patient.LastName.First().ToString().ToUpper()@Html.ViewData.Model.LastName.Substring(1).ToLower()">
                        </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <!-- Street 1 -->
                                <label for="street1_id" class="control-label">Street Address 1</label>
                                <input type="text" class="form-control" id="street1_id" name="street1" placeholder="" value="@Html.ViewData.Patient.BillingAddress1">
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <!-- Street 2 -->
                                <label for="street2_id" class="control-label">Street Address 2</label>
                                <input style="margin-left: 0px" type="text" class="form-control" id="street2_id" name="street2" placeholder="Apartment, Suite, etc." value="@Html.ViewData.Patient.BillingAddress2">
                            </div>
                        </div>
                    </div>
                    <div class="container-fluid">
                        <div class="col-md-4">
                            <div class="form-group">
                                <!-- City-->
                                <label for="city_id" class="control-label">City</label>
                                <input type="text" class="form-control" id="city_id" name="city" placeholder="" value="@Html.ViewData.Model.BillingCity">
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <!-- State Dropdown-->
                                <label for="state_id" class="control-label">State</label>
                                <select class="form-control" id="state_id">
                                    <option value="AL">Alabama</option>
                                    <option value="AK">Alaska</option>
                                    <option value="AZ">Arizona</option>
                                    <option value="AR">Arkansas</option>
                                    <option value="CA">California</option>
                                    <option value="CO">Colorado</option>
                                    <option value="CT">Connecticut</option>
                                    <option value="DE">Delaware</option>
                                    <option value="DC">District Of Columbia</option>
                                    <option value="FL">Florida</option>
                                    <option value="GA">Georgia</option>
                                    <option value="HI">Hawaii</option>
                                    <option value="ID">Idaho</option>
                                    <option value="IL">Illinois</option>
                                    <option value="IN">Indiana</option>
                                    <option value="IA">Iowa</option>
                                    <option value="KS">Kansas</option>
                                    <option value="KY">Kentucky</option>
                                    <option value="LA">Louisiana</option>
                                    <option value="ME">Maine</option>
                                    <option value="MD">Maryland</option>
                                    <option value="MA">Massachusetts</option>
                                    <option value="MI">Michigan</option>
                                    <option value="MN">Minnesota</option>
                                    <option value="MS">Mississippi</option>
                                    <option value="MO">Missouri</option>
                                    <option value="MT">Montana</option>
                                    <option value="NE">Nebraska</option>
                                    <option value="NV">Nevada</option>
                                    <option value="NH">New Hampshire</option>
                                    <option value="NJ">New Jersey</option>
                                    <option value="NM">New Mexico</option>
                                    <option value="NY">New York</option>
                                    <option value="NC">North Carolina</option>
                                    <option value="ND">North Dakota</option>
                                    <option value="OH">Ohio</option>
                                    <option value="OK">Oklahoma</option>
                                    <option value="OR">Oregon</option>
                                    <option value="PA">Pennsylvania</option>
                                    <option value="RI">Rhode Island</option>
                                    <option value="SC">South Carolina</option>
                                    <option value="SD">South Dakota</option>
                                    <option value="TN">Tennessee</option>
                                    <option value="TX">Texas</option>
                                    <option value="UT">Utah</option>
                                    <option value="VT">Vermont</option>
                                    <option value="VA">Virginia</option>
                                    <option value="WA">Washington</option>
                                    <option value="WV">West Virginia</option>
                                    <option value="WI">Wisconsin</option>
                                    <option value="WY">Wyoming</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <!-- Zip Code-->
                                <label for="zip_id" class="control-label">Zip Code</label>
                                <input type="text" class="form-control" id="zip_id" name="zip" placeholder="" value="@Html.ViewData.Model.BillingZip">
                            </div>
                        </div>
                    </div>
                </form>

有没有办法以当前格式分配下拉列表的值/选项?我似乎无法找到一种以这种方式设置值的方法。在给定状态列表的情况下,我是否使用HTML帮助程序方法进行下拉菜单?目前,该值始终设置为阿拉巴马州。

1 个答案:

答案 0 :(得分:1)

每个Option元素都有一个“Selected”属性,如

<option value="CA" selected="selected">California</option>

你也可以在javascript中设置

document.getElementById("state_id").value = "CA";

我不确定是什么规定默认值需要的。是否有一个事件,比如更改另一个文本框中的文本,或者一个可用的值,比如CustomerID,它确定哪个Option元素应该默认?让我知道......