从SQL数据库中检索值 - EF

时间:2018-06-14 17:01:44

标签: sql asp.net-mvc entity-framework razor

我正在试图弄清楚如何从SQL数据库中提取值并在剃刀视图中显示它。

我有以下类使用Entity Framework(我相信)

public class EventLog
{
    [Key]
    public int Id { get; set; }
    public int EventId { get; set; }
    public int MaxDelegates { get; set; }
    public string Code { get; set; }
    public DateTime End { get; set; }
    public string Title { get; set; }  
}

我想在以下模型中将标题映射到DBTitle

public class CourseDetailVM : CourseDetailSummaryVM
{
    public EventLog DBTitle { get; set; }
}

然后我想在以下视图中看到这一点:

@using TSW.Web.Helpers
@model TSW.Web.ViewModels.CourseDetailVM

@{
    Layout = "~/Views/_Master.cshtml";
}

@Model.DBTitle.Title;

我已经安装了以下控制器(对不起,我打算减少它的长度):

public class CourseDetailController : BaseRenderController<CourseDetailPageDT>
{
    private readonly ISitePageFactory _pageFactory = null;
    private readonly IEventService _eventService = null;

    public CourseDetailController(IEventService eventService, ISitePageFactory pageFactory)
    {
        _pageFactory = pageFactory;
        _eventService = eventService;
    }

    public async Task<ActionResult> CourseDetail()
    {
        var homepage = _pageFactory.GetCurrentHomepage();

        var model = Mapper.Map<CourseDetailVM>(CurrentContent);

        model.Email = homepage.ContactEmail;
        model.PhoneNumber = homepage.HeaderPhoneNumber;

        model.InnerPageHeader.ShowHeading = true;
        model.InnerPageHeader.Title = model.PageTitle;

        if (model.Categories.Count == 1)
        {
            var categoryTagId = model.Categories.First().Id;

            var contentTypeAlias = DocumentTypeHelper.GetDocumentTypeAlias<CourseListingPageDT>();
            var courseCategoryPage = Umbraco.TypedContentAtXPath($"//{contentTypeAlias}")
                .FirstOrDefault(x => x.GetPropertyValue<int>(Constants.DocumentTypes.CourseListingPage.Category) == categoryTagId);

            if (courseCategoryPage != null)
            {
                model.InnerPageHeader.BackLink = Mapper.Map<LinkItem>(courseCategoryPage.Id);
            }
        }

        try
        {
            model.Events = await _eventService.GetEventsForCourse(CurrentContent.AdministrateId);
        }
        catch (Exception ex)
        {
            model.Events = new StaticPagedList<Event>(Enumerable.Empty<Event>(), 1, 1, 0);
            Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
        }

        if (CurrentContent.Graphic != 0)
        {
            model.InnerPageHeader.Graphic = Mapper.Map<CtaItem>(CurrentContent.Graphic);
        }

        return View(model);
    }
}

我已经尝试了每个建议,我可以谷歌在控件中添加映射,但无法理解这个将SQL数据库中的值拉入剃刀视图的简单功能。

有人可以帮帮我吗?

0 个答案:

没有答案