为Model呈现省份的选择列表.省(字符串)属性
本文关键字:字符串 属性 列表 选择 Model 现省 | 更新日期: 2023-09-27 18:14:59
我想呈现一个省份的选择列表,以便用户可以选择一个省份。但是返回给服务器的选择结果总是选择列表(Ontario)的第一项。
Model (Code first)
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);
<<p> 视图/strong> 在我看来,我渲染我的列表这样,这确实渲染列表正确,但绑定似乎失败…因为即使选择改变了,也总是返回第一项。
<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
Razor生成以下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
- list)以输入类型text呈现。我们欢迎任何解决方案或帮助找到解决方案的线索。
我刚刚找到了原因,我正在使用一个恰好使用jquery minimalect插件的网站模板。脚本在替换呈现的选择列表时破坏了属性绑定。我删除了脚本参考,现在它工作得很好…