以下のコード例では、ソースパラメーターの前にテキストを追加します。このテキストは、タイプであるか、 MoreLinq拡張機能を介してMVCドロップダウンリストで使用できます。この関数は、JSONとしてシリアル化されたリストを返すコントローラーアクションで使用されるため、ドロップダウンカスケードを使用して動的にロードできます。そうでない場合は、ビューモデルで個別にこれを実行します。ISite
IFactory
具体的なListItemクラスを作成せずに、以下のようなことを実行できる方法があるかどうか疑問に思いました。私の例は、私がどのように使用しているかを示していますas IListItem
が、これはコンパイルされないことを認識しています。
現時点では、私のコントローラーにはモデルの具体的なクラスの概念がなく、それを維持したいと思っていました。これを機能させるためにListItemのインスタンスを作成する必要があるかどうか、または誰かが持っているかどうかはわかりません。別の提案?
ここで重要な場合に備えて、ジェネリック医薬品の共変性と反変性についての私の知識は限られていますか?ありがとう
public interface IListItem
{
int Id { get; set; }
string Name { get; set; }
}
public interface ISite : IListItem
{
int CountryId { get; set; }
}
public interface IFactory : IListItem
{
int SiteId { get; set; }
}
public interface IResource
{
int Id { get; set; }
string Name { get; set; }
int ContentID { get; set; }
string Text { get; set; }
int LanguageID { get; set; }
string LanguageCode { get; set; }
int Priority { get; set; }
}
private IEnumerable<IListItem> PrependSelectionResource(IEnumerable<IListItem> source, string languageCode)
{
if(source == null || source.Count() == 1)
return source; // don't bother prepending the relevant resource in these cases
try
{
// will throw an exception if languageCode is null or blank
var resource = _resourceRepository.GetByNameAndLanguageCode(
"Prompt_PleaseSelect",
languageCode);
if(resource == null)
return source;
// prepend the "Please Select" resource to the beginning of the source
// using MoreLinq extension
return source.Prepend(new {
Id = 0,
Name = resource.Text ?? ""
} as IListItem);
}
catch {
return source;
}
}