我从数据库中提取行以查找特定列。现在它拉了5行,我需要为模型中的每一行引用正确的列。这是我的控制器:
public ActionResult Index()
{
//Use mimetype as key to find correct rows
var mimetype = "mcv";
dynamic LookupData = GetVideoUrls(mimetype);
return View(LookupData);
}
private dynamic GetVideoUrls(string mimetype)
{
var VideoService = new AppServices.Video.GetAllVideoByMimeType();
dynamic videoFiles = VideoService.Execute(mimetype);//Execute grabs the rows from the database
return (videoFiles);
}
在我看来,我有:
@model IEnumerable<dynamic>
VideoFiles返回包含13列的五行。我需要为视图中的五行中的每一行访问2列。我该怎么做?
更新 这是我的模特:
public class LoginVideoModel
{
public LoginVideoModel(string englishurl, string spanishurl)
{
EnglishVideoUrl = englishurl;
SpanishVideoUrl = spanishurl;
}
public string EnglishVideoUrl
{
get;
set;
}
public string SpanishVideoUrl
{
get;
set;
}
}
public class WelcomeVideoModel
{
public WelcomeVideoModel(string englishurl, string spanishurl)
{
EnglishVideoUrl = englishurl;
SpanishVideoUrl = spanishurl;
}
public string EnglishVideoUrl
{
get;
set;
}
public string SpanishVideoUrl
{
get;
set;
}
}
public class BenefitVideoModel
{
public BenefitVideoModel(string englishurl, string spanishurl)
{
EnglishVideoUrl = englishurl;
SpanishVideoUrl = spanishurl;
}
public string EnglishVideoUrl
{
get;
set;
}
public string SpanishVideoUrl
{
get;
set;
}
}
public class MyEnrollmentVideoModel
{
public MyEnrollmentVideoModel(string englishurl, string spanishurl)
{
EnglishVideoUrl = englishurl;
SpanishVideoUrl = spanishurl;
}
public string EnglishVideoUrl
{
get;
set;
}
public string SpanishVideoUrl
{
get;
set;
}
}
public class AdminSideVideoModel
{
public AdminSideVideoModel(string englishurl, string spanishurl)
{
EnglishVideoUrl = englishurl;
SpanishVideoUrl = spanishurl;
}
public string EnglishVideoUrl
{
get;
set;
}
public string SpanishVideoUrl
{
get;
set;
}
}
希望这也有帮助。
更新 这是videoservice.execute代码:
public class GetAllVideoByMimeType
{
public dynamic Execute(string mimetype)
{
return DBRepository.GetAllByCritera<DataContainers.File>("WHERE MimeType = @0", mimetype);
}
}
更新3 这是存储库:
/// <summary>
/// Obtain one or more items from db based on Where Clase
/// </summary>
/// <typeparam name="T">Table Type</typeparam>
/// <param name="whereClause">Standard WHERE clause with paramterized statements
/// Example: WHERE someID=@0",args: 4
/// </param>
/// <param name="arguments"></param>
/// <returns></returns>
public static IEnumerable<dynamic> GetAllByCritera<T>(string whereClause, params object[] arguments)
{
var model = new DBTableModel<T>();
return model.All(where: whereClause, args: arguments);
}
如果我尝试了这个怎么办:
public ActionResult Index()
{
//Use mimetype as key to find correct rows
var mimetype = "mcv";
dynamic LookupData = GetVideoUrls(mimetype);
return View(LookupData);
}
private dynamic GetVideoUrls(string mimetype)
{
var VideoService = new AppServices.Video.GetAllVideoByMimeType();
List<dynamic> videoFiles = VideoService.Execute(mimetype);
ApplicationVideoModel appvids = new ApplicationVideoModel();
dynamic LoginFiles = videoFiles.Where(v => v.OriginalFileNameWithoutExtension == "LoginController").FirstOrDefault();
dynamic WelcomeFiles = videoFiles.Where(v => v.OriginalFileNameWithoutExtension == "WelcomeController").FirstOrDefault();
dynamic BenefitFiles = videoFiles.Where(v => v.OriginalFileNameWithoutExtension == "BenefitController").FirstOrDefault();
dynamic MyEnrollmentFiles = videoFiles.Where(v => v.OriginalFileNameWithoutExtension == "MyEnrollmentController").FirstOrDefault();
dynamic AdminFiles = videoFiles.Where(v => v.OriginalFileNameWithoutExtension == "AdminSideController").FirstOrDefault();
return LoginFiles;
}
每个动态都返回我需要的行,但是如何将每个动态发送到视图?
答案 0 :(得分:0)
根据您提供的示例模型,您的常见模型对象可能如下所示
public class VideoUrlModel {
public string EnglishVideoUrl { get; set; }
public string SpanishVideoUrl { get; set; }
}
然后可以按如下方式更新GetVideoUrls
private IEnumrable<VideoUrlModel> GetVideoUrls(string mimetype) {
var VideoService = new AppServices.Video.GetAllVideoByMimeType();
var videoFiles = VideoService.Execute(mimetype);
var models = videoFiles.Select(row =>
new VideoUrlModel {
EnglishVideoUrl = row.englishvideourl,
SpanishVideoUrl = row.spanishvideourl
});
return models;
}
由于VideoService.Execute
返回IEnumerable<dynamic>
,您可以使用 LINQ Select
扩展名迭代行,并根据需要创建模型项。
所以现在在你的控制器中可以将集合作为模型传递给视图
public ActionResult Index() {
//Use mimetype as key to find correct rows
var mimetype = "mcv";
var LookupData = GetVideoUrls(mimetype);
return View(LookupData);
}
您的视图会将模型视为,
@model IEnumerable<VideoUrlModel>
并且可以访问作为视图模型传递的集合
答案 1 :(得分:0)
这对我的申请有用:
public ActionResult Index()
{
//Use mimetype as key to find correct rows
var mimetype = "mcv";
var VideoService = new AppServices.Video.GetAllVideoByMimeType();
List<dynamic> videoFiles = VideoService.Execute(mimetype);
dynamic mymodel = new System.Dynamic.ExpandoObject();
mymodel.LoginFiles = videoFiles.Where(v => v.OriginalFileNameWithoutExtension == "LoginController").FirstOrDefault();
mymodel.BenefitFiles = videoFiles.Where(v => v.OriginalFileNameWithoutExtension == "BenefitController").FirstOrDefault();
mymodel.WelcomeFiles = videoFiles.Where(v => v.OriginalFileNameWithoutExtension == "WelcomeController").FirstOrDefault();
mymodel.MyEnrollmentFiles = videoFiles.Where(v => v.OriginalFileNameWithoutExtension == "MyEnrollmentController").FirstOrDefault();
mymodel.AdminSideFiles = videoFiles.Where(v => v.OriginalFileNameWithoutExtension == "AdminSideController").FirstOrDefault();
return View(mymodel);
}
正如您所见,我使用mymodel.
构建模型,我可以使用以下方式在视图中访问它:
@Model.LoginFiles.EnglishVideoURL
感谢您的帮助!