0
我想呈現省份的選擇列表,以便用戶可以選擇省份。但是返回到服務器的選定結果始終是選擇列表(安大略省)的第一項。爲Model.Province(字符串)屬性渲染省份的選擇列表
型號(代碼第一)
public class Address
{
[Key]
public int ID { get; set; }
[ForeignKey("Customer")]
public string CustomerID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Label { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string City { get; set; }
public string Province { get; set; }
public string Country { get; set; }
public string PostalCode { get; set; }
public string PhoneNumber { get; set; }
public virtual ApplicationUser Customer { get; set; }
//[InverseProperty("BillingAddress")]
public virtual ICollection<Invoice> Billings { get; set; }
//[InverseProperty("ShippingAddress")]
public virtual ICollection<Invoice> Shippings { get; set; }
}
控制器
我的省份被裝載在ViewData的是這樣的:
string[] provinces = ConfigurationManager.AppSettings["CanadianProvinces"].Split(',');
ViewData["Provinces"] = new SelectList(provinces);
查看
在我看來,我像這樣呈現我的列表,它確實呈現一個列表,但綁定似乎失敗...因爲即使選擇改變,第一個項目總是返回。
<div class="form-group required">
<label for="Province">Provinces <sup>*</sup> </label>
@Html.ValidationMessageFor(x => x.Province, "", new { @class = "text-danger" })
@Html.DropDownListFor(x=>x.Province, (SelectList)ViewData["Provinces"],new {@class="form-control" })
</div>
生成的HTML
剃刀生成以下HTML:
<div class="form-group required">
<label for="Province">Provinces <sup>*</sup> </label>
<span class="field-validation-valid text-danger" data-valmsg-for="Province" data-valmsg-replace="true"></span>
<select class="form-control" id="Province" name="Province" style="display: none;">
<option selected="selected">Ontario</option>
<option> Quebec</option>
<option> Nova Scotia</option>
<option> New Brunswick</option>
<option> Manitoba</option>
<option> British Columbia</option>
<option> Prince Edward Island</option>
<option> Saskatchewan</option>
<option> Alberta</option>
<option> Newfoundland and Labrador</option>
</select><div class="minict_wrapper active">
<input type="text" value="Ontario" placeholder=" Nova Scotia">
<ul style="display: block;">
<li data-value="Ontario" class="minict_first">Ontario</li>
<li data-value="Quebec" class=""> Quebec</li>
<li data-value="Nova Scotia" class="selected"> Nova Scotia</li>
<li data-value="New Brunswick" class=""> New Brunswick</li>
<li data-value="Manitoba" class=""> Manitoba</li>
<li data-value="British Columbia" class=""> British Columbia</li>
<li data-value="Prince Edward Island" class=""> Prince Edward Island</li>
<li data-value="Saskatchewan" class=""> Saskatchewan</li>
<li data-value="Alberta" class=""> Alberta</li>
<li data-value="Newfoundland and Labrador" class="minict_last"> Newfoundland and Labrador</li>
<li class="minict_empty" style="display: none;">No results match your keyword.</li>
</ul>
</div>
我試圖找出爲什麼2名列表(1名選擇列表和1個< ul>列表)使用輸入類型文本呈現。任何解決方案或幫助找到解決方案的線索都是受歡迎的,並且非常感謝。
你有任何插件?或者你重寫DropDownListFor實現? – Marko
是啊Jqueryminimalect是原因。我剛剛回答了這個問題,謝謝你的回答! – MaxVerro