C# 选择2不在局部视图内渲染的字段
在一个主页上,例如Edit.cshtml,我这里有一段代码,它呈现一个带有国旗的select 2,它工作正常,但是当我试图将它移动到局部视图时,它不工作 PleasureCraftEditViewPartial.cshtml中的My Html字段C# 选择2不在局部视图内渲染的字段,c#,asp.net-mvc,C#,Asp.net Mvc,在一个主页上,例如Edit.cshtml,我这里有一段代码,它呈现一个带有国旗的select 2,它工作正常,但是当我试图将它移动到局部视图时,它不工作 PleasureCraftEditViewPartial.cshtml中的My Html字段 <div class="form-group"> @Html.DisplayNameFor(model => model.MooringCountry) <select asp-for=&qu
<div class="form-group">
@Html.DisplayNameFor(model => model.MooringCountry)
<select asp-for="MooringCountry" class="form-control select2" style="height:35px" id="MooringCountry" name="MooringCountry">
<option>Please select Mooring Country</option>
</select>
<span asp-validation-for="MooringCountry" class="text-danger"></span>
</div>
@if (@Model.VesselCat == (int)VesselCat.VesselCatType.Commercial) {
@await Html.PartialAsync("ComercialEditViewPartial")
}
@if (@Model.VesselCat == (int)VesselCat.VesselCatType.MotorBoats ||
@Model.VesselCat == (int)VesselCat.VesselCatType.SailingBoats ||
@Model.VesselCat == (int)VesselCat.VesselCatType.SmallBoats) {
@await Html.PartialAsync("PleasureCraftEditViewPartial")
}
以下内容在my Edit.cshtml中
<script>
$(function () {
//Initialize Select2 Elements
$('.select2').select2();
var isoCountries = [
{ id: 1, flag: 'gbp', text: 'United Kingdom' },
{ id: 2, flag: 'usa', text: 'United States' }
];
function formatCountry(country) {
if (!country.id) { return country.text; }
var $country = $('<span class="flag-icon flag-icon-' + country.flag + ' flag-icon-squared"></span>' + '<span class="flag-text">' + country.text + "</span>");
return $country;
};
//Assuming you have a select element with name country
// e.g. <select name="name"></select>
$("[id='Flag']").select2({
placeholder: "Please Select a Flag",
templateResult: formatCountry,
data: isoCountries
});
$("[id='MooringCountry']").select2({
placeholder: "Please Select a Mooring Country",
templateResult: formatCountry,
data: isoCountries
});
$('#Flag').trigger('change');
});
</script>
$(函数(){
//初始化Select2元素
$('.select2').select2();
var等国家=[
{id:1,标志:“gbp”,文字:“联合王国”},
{id:2,国旗:“美国”,文字:“美国”}
];
国家职能(国家){
如果(!country.id){return country.text;}
变量$country=$(“”+“”+country.text+“”);
返回$country;
};
//假设您有一个名为country的select元素
//例如。
$(“[id='Flag']”)。选择2({
占位符:“请选择一个标志”,
templateResult:formatCountry,
数据:等国家
});
$(“[id='MooringCountry']”)。选择2({
占位符:“请选择一个系泊国家”,
templateResult:formatCountry,
数据:等国家
});
$('#Flag')。触发器('change');
});
我的控制台中没有错误
只是一个空的下拉列表
您是否检查了控制台中的javascript错误?这里有些可疑之处,我没有看到
isoCountries
…@asawyer的右括号对不起,这是我的缩进just@asawyer修复了上面的问题,但是没有控制台错误。在这一点上,我在$('.select2').select2()处设置了一个断点代码>行,并确保控件按预期存在。我还想确保局部视图实际上是渲染的,razor部分中的断点有助于确保它的渲染,因为如果不渲染,它将不可见