详细信息使用viewModel查看Controller方法

时间:2015-07-07 20:16:03

标签: c# asp.net-mvc

我仍然使用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>

2 个答案:

答案 0 :(得分:2)

您传递的是IEnumerable<ClubDetailsViewModel>,而不是ClubDetailsViewModel。你可能想要过滤你的球杆并选择一个,而不是对所有球杆进行这样做。像这样:

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

答案 1 :(得分:1)

或添加

 @Model List<ClubDetailsViewModel>

到你的观点并循环俱乐部