视图未映射模型。获取模型项传递错误

时间:2017-01-03 08:34:22

标签: c# asp.net-mvc

传入字典的模型项的类型为' Vidly.Models.Customer',但此字典需要类型为' Vidly.ViewModels.RandomMovieViewModel'的模型项。

您好,

我试图在点击ActionLink生成的链接时加载另一个视图。

代码段如下所示:

  public class MoviesController : Controller
{


    public ActionResult CustomerDetail(int? id)
    {
        var customerRepo = new CustomerRepo();
        var selectedCustomer = customerRepo.GetCustomer().SingleOrDefault(x => x.Id == id);
        return View(selectedCustomer);
    }
}

现在,动作链接在视图中生成,如下所示:



@model Vidly.ViewModels.RandomMovieViewModel

@{
    ViewBag.Title = "Random";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Customers</h2>
@if (Model.Customers.Count == 0)
{
    <p>No one rented the movie</p>
}

<table class="table table-bordered table-hover">
    <thead>
        <tr>
            <th>
                Customer
            </th>
        </tr>
    </thead>
    <tbody>


        @foreach (var customer in Model.Customers)
        {
            <tr>
                <td>

                    @Html.ActionLink(customer.Name, "CustomerDetail", "Movies", new { id = customer.Id }, null)
                </td>
            </tr>
        }
    </tbody>
</table>
&#13;
&#13;
&#13;

这需要一个CustomerDetail视图,如下所示:

&#13;
&#13;
@model Vidly.Models.Customer
@{
    ViewBag.Title = "CustomerDetail";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>@Model.Name</h2>
&#13;
&#13;
&#13;

我已经创建了一个用于获取数据的存储库类,目前我正在使用硬编码数据。

public class CustomerRepo
{
    public IEnumerable<Customer> GetCustomer()
    {
        return new List<Customer>
        {
            new Customer {Id=1,Name="Rishabh" },
            new Customer {Id=2,Name="Keerti" }
        };
    }
}

ViewModel类:

 public class RandomMovieViewModel
{
    public List<Movie> Movies { get; set; }
    public List<Customer> Customers { get; set; }

}

问题是我遇到了错误。我在视图中传递Customer对象,我还使用Customer模型映射了Details视图,但我不知道它为什么要求我给它ViewModel

enter image description here

0 个答案:

没有答案