使用viewModel的详细视图控制器方法

本文关键字:视图 控制器 方法 viewModel 使用 | 更新日期: 2023-09-27 18:01:24

我仍然在asp.net MVC中找到我的脚。

我想从viewModel中生成一个细节视图,但是我没有得到正确的控制器,所以它说我传递了错误的模型类型给视图。

控制器当前为:

    public ActionResult ClubDetails(int clubId)
    {
        //Populate the view
        var clubs = from s in db.Clubs
                    orderby s.Name
                    select s;
        var viewModel = clubs.Select(t => new ClubDetailsViewModel
        {
            Name = t.Name,
            ShortName = t.ShortName,
            Founded = t.Founded,
            ContactName = t.FirstName + " " + t.LastName,
            Address1 = t.Address1,
            Address2 = t.Address2,
            City = t.City,
            County = t.County,
            Postcode = t.Postcode,
            Telephone = t.Telephone,
            Email = t.Email,
            Bio = t.Bio,
            Website = t.Website,
        });
        return View(viewModel);
    }

viewModel是:

    public class ClubDetailsViewModel
{
    [Display(Name = "Club Name")]
    public string Name { get; set; }
    [Display(Name = "Abbreviation")]
    public string ShortName { get; set; }
    [Display(Name = "Founded")]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
    public DateTime? Founded { get; set; }
    [Display(Name = "Contact Name")]
    public string ContactName { get; set; }
    [Display(Name = "Address 1")]
    public string Address1 { get; set; }
    [Display(Name = "Address 2")]
    public string Address2 { get; set; }
    [Display(Name = "City")]
    public string City { get; set; }
    [Display(Name = "County")]
    public string County { get; set; }
    [Display(Name = "PostCode")]
    public string Postcode { get; set; }
    [Display(Name = "Telephone")]
    public string Telephone { get; set; }
    [EmailAddress]
    [Display(Name = "Email")]
    public string Email { get; set; }
    [Display(Name = "Website")]
    [Url]
    public string Website { get; set; }
    [Display(Name = "Bio")]
    public String Bio { get; set; }
}

视图为:

@model GRCWebApp.ViewModels.ClubDetailsViewModel
@{
ViewBag.Title = "ClubDetails";
}
<h2>ClubDetails</h2>
<div>
<h4>ClubDetailsViewModel</h4>
<hr />
<dl class="dl-horizontal">
    <dt>
        @Html.DisplayNameFor(model => model.Name)
    </dt>
    <dd>
        @Html.DisplayFor(model => model.Name)
    </dd>
    <dt>
        @Html.DisplayNameFor(model => model.ShortName)
    </dt>
    <dd>
        @Html.DisplayFor(model => model.ShortName)
    </dd>
    <dt>
        @Html.DisplayNameFor(model => model.Founded)
    </dt>
    <dd>
        @Html.DisplayFor(model => model.Founded)
    </dd>
    <dt>
        @Html.DisplayNameFor(model => model.ContactName)
    </dt>
    <dd>
        @Html.DisplayFor(model => model.ContactName)
    </dd>
    <dt>
        @Html.DisplayNameFor(model => model.Address1)
    </dt>
    <dd>
        @Html.DisplayFor(model => model.Address1)
    </dd>
    <dt>
        @Html.DisplayNameFor(model => model.Address2)
    </dt>
    <dd>
        @Html.DisplayFor(model => model.Address2)
    </dd>
    <dt>
        @Html.DisplayNameFor(model => model.City)
    </dt>
    <dd>
        @Html.DisplayFor(model => model.City)
    </dd>
    <dt>
        @Html.DisplayNameFor(model => model.County)
    </dt>
    <dd>
        @Html.DisplayFor(model => model.County)
    </dd>
    <dt>
        @Html.DisplayNameFor(model => model.Postcode)
    </dt>
    <dd>
        @Html.DisplayFor(model => model.Postcode)
    </dd>
    <dt>
        @Html.DisplayNameFor(model => model.Telephone)
    </dt>
    <dd>
        @Html.DisplayFor(model => model.Telephone)
    </dd>
    <dt>
        @Html.DisplayNameFor(model => model.Email)
    </dt>
    <dd>
        @Html.DisplayFor(model => model.Email)
    </dd>
    <dt>
        @Html.DisplayNameFor(model => model.Website)
    </dt>
    <dd>
        @Html.DisplayFor(model => model.Website)
    </dd>
    <dt>
        @Html.DisplayNameFor(model => model.Bio)
    </dt>
    <dd>
        @Html.DisplayFor(model => model.Bio)
    </dd>
</dl>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { /* id = Model.PrimaryKey */ }) |
@Html.ActionLink("Back to List", "Index")
</p>

使用viewModel的详细视图控制器方法

你通过的是IEnumerable<ClubDetailsViewModel>,不是ClubDetailsViewModel。你可能想要过滤你的俱乐部并选择一个,而不是对所有的俱乐部都这样做。像这样:

var viewModel = db.Clubs
    .Where(t => t.ClubID == clubID)
    .Select(t => new ClubDetailsViewModel
    { .. })
    .FirstOrDefault();
return View(viewModel);

或者add

 @Model List<ClubDetailsViewModel>

到你的视图并循环通过clubs