列表上的C#条件语句
条件:如果假设我的列表包含列表上的C#条件语句,c#,asp.net,asp.net-mvc-3,list,C#,Asp.net,Asp.net Mvc 3,List,条件:如果假设我的列表包含n计数。然后,如果任何索引的任何“本机元素”持有某些值,则显示其他字段,反之亦然 i、 e。 像这样的 public ActionResult actionName() { AbcViewModel viewModel=new AbcViewModel(); viewModel=model.getAbcList(); return View(viewModel); } @if (Model.languageList.Any(x => x
n
计数。然后,如果任何索引的任何“本机元素”持有某些值,则显示其他字段,反之亦然
i、 e。
像这样的
public ActionResult actionName()
{
AbcViewModel viewModel=new AbcViewModel();
viewModel=model.getAbcList();
return View(viewModel);
}
@if (Model.languageList.Any(x => x.nativeLanguage.IsNotEmpty() == false)
{
@LabelFor(x=>x.other)
}
else
{
@LabelFor(x=>x.native)
}
@if (Model.languageList.Any(x => string.IsNullOrEmpty(x.nativeLanguage))
{
@LabelFor(x => x.other)
}
else
{
@LabelFor(x => x.native)
}
我认为这不是正确的语法(IsNotEmpty不是有效的方法)
告诉我正确的方法。你可以这样做
public ActionResult actionName()
{
AbcViewModel viewModel=new AbcViewModel();
viewModel=model.getAbcList();
return View(viewModel);
}
@if (Model.languageList.Any(x => x.nativeLanguage.IsNotEmpty() == false)
{
@LabelFor(x=>x.other)
}
else
{
@LabelFor(x=>x.native)
}
@if (Model.languageList.Any(x => string.IsNullOrEmpty(x.nativeLanguage))
{
@LabelFor(x => x.other)
}
else
{
@LabelFor(x => x.native)
}
用于检查指定的字符串是空字符串还是空字符串
试试这个:
@if (Model.languageList.Any(x=> !string.IsNullOrEmpty(x.nativeLanguage))==false){ @LabelFor(x=>x.other) }
else{@LabelFor(x=>x.native)}
试试这个:
@if (!Model.languageList.Any(x=> string.IsNullOrEmpty(x.nativeLanguage))
{
@LabelFor(x=>x.other)
}
else
{
@LabelFor(x=>x.native)
}
如果列表中充满了字符串,那么应该能够调用如下方法
public ActionResult actionName()
{
AbcViewModel viewModel=new AbcViewModel();
viewModel=model.getAbcList();
return View(viewModel);
}
@if (Model.languageList.Any(x => x.nativeLanguage.IsNotEmpty() == false)
{
@LabelFor(x=>x.other)
}
else
{
@LabelFor(x=>x.native)
}
@if (Model.languageList.Any(x => string.IsNullOrEmpty(x.nativeLanguage))
{
@LabelFor(x => x.other)
}
else
{
@LabelFor(x => x.native)
}
如果您还有其他数据类型,您仍然可以为自己编写一个方法来完成相同的工作