Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 设置页面加载时多个下拉列表的默认值_Asp.net Mvc 3_Razor - Fatal编程技术网

Asp.net mvc 3 设置页面加载时多个下拉列表的默认值

Asp.net mvc 3 设置页面加载时多个下拉列表的默认值,asp.net-mvc-3,razor,Asp.net Mvc 3,Razor,我有一个模型 public class Customer { public string Name { get; set;} public string CountryCode { get; set;} } 在控制器中 var model = new List<Customer> { new Customer { Name = "foo", CountryCode = "US"}, new Customer { Name = "bar", CountryCo

我有一个模型

public class Customer
{
 public string Name { get; set;}
 public string CountryCode { get; set;}
}
在控制器中

var model = new List<Customer> 
{ 
    new Customer { Name = "foo", CountryCode = "US"},
    new Customer { Name = "bar", CountryCode = "UK",
};          
return PartialView("_Edit", model);
var模型=新列表
{ 
新客户{Name=“foo”,CountryCode=“US”},
新客户{Name=“bar”,CountryCode=“UK”,
};          
返回PartialView(“编辑”,模型);
显示所有国家/地区的扩展方法:-

public class CountryList
{
public static IEnumerable<SelectListItem> CountrySelectList
    {
        get
        {
            var list = new List<SelectListItem>()
            {
                new SelectListItem { Value = "US", Text="US" },
                new SelectListItem { Value = "UK", Text="UK" },
            };

            return list;
        }
    }
}
公共类国家列表
{
公共静态IEnumerable CountrySelectList
{
得到
{
var list=新列表()
{
新建SelectListItem{Value=“US”,Text=“US”},
新建SelectListItem{Value=“UK”,Text=“UK”},
};
退货清单;
}
}
}
在局部视图中

@model List<Customer>

@Html.DropDownListFor(model => model[i].CountryCode, CountryList.CountrySelectList, "Select Country Type")
@型号列表
@Html.DropDownListFor(model=>model[i].CountryCode,CountryList.CountrySelectList,“选择国家类型”)
但是下拉列表没有选择每个客户的国家代码?有什么想法吗

PS:它使用的模型[i]=>是Customer类型,为了简单起见,我在呈现html标记之前删除了forloop

@using(Html.BeginForm())
{
   for(int i = 0; i < Model.Count(); i++)
   {
      @Html.TextBoxFor(model => model[i].Name)
      @Html.DropDownListFor..........
   }
}
@使用(Html.BeginForm())
{
对于(int i=0;imodel[i].Name)
@Html.DropDownListFor。。。。。。。。。。
}
}

因为您的CoutryList帮助程序会返回一个SelectListItems列表,其中所有SelectListItems的SelectProperty设置为False(默认设置)

我将重写您的帮助器方法,如下所示:

public static IEnumerable<SelectListItem> CountrySelectList(string selectedCountryCode)
{
    get
    {
        var list = new List<SelectListItem>()
        {
            new SelectListItem { Value = "US", Text="US" },
            new SelectListItem { Value = "UK", Text="UK" },
        };

        var selectedListItem = list.FirstOrDefault(t=>t.Value== selectedCountryCode);

        if(selectedListItem!=null)
          selectedListItem.Selected=true;


        return list;
    }
}

让我更正一下,您想让我将属性更改为方法吗?我认为您无法访问Func表达式之外的模型[I]。请告诉我是否有任何输入错误?是的,这是一个输入错误,您必须更改
@Html.DropDownListFor(model=>model[I]CountryCode,CountryList.CountrySelectList(model[I].CountryCode),“选择国家/地区类型”)
CountrySelectList(型号[i]仔细一看,您首先会理解我的问题。型号[i]确实意味着razor视图使用的是IEnumerable或IList,我的第一次编辑是我的错误,第二次/第三次编辑是为了让它更清晰。
@Html.DropDownListFor(model => model[i].Customer, CountryList.CountrySelectList(model[i].Customer.CountryCode), "Select Country Type")