使用dapper

时间:2018-01-03 19:05:03

标签: model-view-controller dapper

我是编程新手。尝试使用dapper和automapper构建一个mvc站点。我无法理解如何使用viewmodel在我的视图中显示我的外键的“名称”。我知道这应该很简单,但我迷失了。

这些是我的模特

 public class ShowsModel
{
    [Key]
    public int ShowID { get; set; }

    public string ShowName { get; set; }

    [ForeignKey("Management")]
    public int ShowManagementID { get; set; }

    [ForeignKey("Facility")] 
    public int FacilityID { get; set; }
}

 public class ShowManagementModel
{
    [Key]
    public int ShowManagementID { get; set; }
    public string ManagementCompany { get; set; }
}

public class ShowFacilityModel
{
    [Key]
    public int FacilityID { get; set; }
    public string FacilityName { get; set; }
}

我的viewmodel

public class ShowVM
{
   public int ShowID { get; set; }
   public string ShowName { get; set; }

   public int ShowManagementID { get; set; }
   public string ManagementCompany { get; set; }

   public int FacilityID { get; set; }
   public string Facility { get; set; } // facility table
}

我的控制器

public ActionResult Shows()
    {
        List<ShowVM> model = new List<ShowVM>();
        var shows = ShowRepo.Shows();
        var listShows = Mapper.Map<List<ShowVM>>(shows);
        return View(listShows);
    }

我的观点

@model IEnumerable<DarkHorseWebApp.ViewModels.ShowVM>
<p>
@Html.ActionLink("Create New", "Create")

<table class="table">
<tr>
    <th>
        @Html.DisplayNameFor(model => model.ShowID)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.ShowName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.ShowManagementID)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.ManagementCompany)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.FacilityID)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Facility)
    </th>
    <th></th>
</tr>
@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.ShowID)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.ShowName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.ShowManagementID)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.ManagementCompany)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.FacilityID)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Facility)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
        @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
        @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
    </td>
</tr>

}

我可以让视图显示...但是只显示管理和设施的ID号而不是实际名称。

任何帮助和指示都将不胜感激。我看过很多关于这个的帖子和文章,我觉得我的大脑正在旋转。

在我的添加显示视图中,我能够在我的控制器中使用以下代码获取管理和工具的字段以填充下拉列表

        public ActionResult AddShow()
    {
        ShowAddVM vm = new ShowAddVM
        {
            Managements = new SelectList(ManagementRepo.ManagementCos(), "ShowManagementID", "ManagementCompany"),
            Facilities = new SelectList(FacilityRepo.Facilities(), "FacilityID", "FacilityName")
        };
        return View(vm);
    }

和我的观点

 <div class="form-group">
        @Html.LabelFor(model => model.ShowManagementID, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(model => model.ShowManagementID, Model.Managements, "Select Management")
            @Html.ValidationMessageFor(model => model.ShowManagementID, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.FacilityID, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
           @Html.DropDownListFor(model => model.FacilityID, Model.Facilities, "Select Facility")
            @Html.ValidationMessageFor(model => model.FacilityID, "", new { @class = "text-danger" })
        </div>
    </div>

但是我无法在编辑视图中使用以下代码

执行相同的操作
       public ActionResult UpdateShow(int id)
    {
        var show = ShowRepo.Shows().Find(Show => Show.ShowID == id);
        if (show == null) return new HttpNotFoundResult();
        var updateshow = Mapper.Map<ShowUpdateVM>(show);
        return View(updateshow);
    }

回购

 public List<ShowsModel> Shows()
    {
        try
        {
            Connection();
            con.Open();
            IList<ShowsModel> ShowList = SqlMapper.Query<ShowsModel>(
                con, "dbo.spShows_GetShow").ToList();
            con.Close();
            return ShowList.ToList();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

存储过程

作为

开始

上设置nocount

选择ShowID

作为

ShowID,

ShowName,

ShowManagementID,

FacilityID,

ShowWebsite,

DateStart,

DateEnd,

RatingSpecialClasses,

ShowActive

来自dbo.Shows

结束

我应该使用加入show,facility和management表的存储过程吗?

0 个答案:

没有答案