MVC:从表中选择一行

时间:2013-06-27 08:56:02

标签: asp.net-mvc asp.net-mvc-4 linq-to-sql

我有这个功能显示表格中的所有行,我可以在视图中显示一些额外的细节

public ActionResult ModuleList()
{
    var modulesList = new IndexModule();

    var modules = _db.Modules
                    .OrderBy(module => module.DateEntered)
                    .Select(module => new ModuleVM
                    {
                        Id = module.Id,
                        ModuleId = module.ModuleId,
                        TypeName = module.ModuleType.TypeName,
                        DateEntered = module.DateEntered
                    });
    modulesList.Modules = modules.ToList();

    return View(modulesList);
}

现在我想根据作为参数传递的Id

选择一行
public ActionResult Details(long id)
{
    var moduleV = _db.Modules
                .Select(module => new ModuleVM
                {
                    Id = id,
                    ModuleId = module.ModuleId,
                    TypeName = module.ModuleType.TypeName,
                    KindName = module.ModuleType.ModuleKind.KindName,
                    DateEntered = module.DateEntered
                });

    return View(moduleV);//error
}

错误说:Argument type 'System.Linq.Iqueryable<AdminPortal.Areas.Hardware.Models.ModuleVM>' isnot assignable to model type 'AdminPortal.Areas.Hardware.Models.ModuleVM' 我尝试在上面做,但它不起作用。 我的select语句是否仅选择id行? 我想选择行并向其添加一些细节并显示在Details视图中 这就是我在View中所做的事情

@using BootstrapSupport
@model AdminPortal.Areas.Hardware.Models.ModuleVM
@{
    ViewBag.Title = "Details";
    Layout = "~/Views/shared/_BootstrapLayout.basic.cshtml";
}

<fieldset>
    <legend>Module <small>Details</small></legend>
    <dl> <!-- use this class on the dl if you want horizontal styling http://twitter.github.com/bootstrap/base-css.html#typography  class="dl-horizontal"-->     

            <dt>
                @Model.Id
            </dt>
        <dd>
            @Html.Display(Model.Id.ToString())                
        </dd>
        <dt>@Model.ModuleId</dt>
        <dd>@Html.Display(Model.ModuleId)</dd>

        <dt>@Model.TypeName</dt>
        <dd>@Html.Display(Model.TypeName)</dd>

        <dt>@Model.KindName</dt>
        <dd>@Html.Display(Model.KindName)</dd>

        <dt>@Model.DateEntered</dt>
        <dd>@Html.Display(Model.DateEntered.ToString())</dd>

    </dl>
</fieldset>
<p>
    @Html.ActionLink("Edit", "Edit", Model.GetIdValue()) |
    @Html.ActionLink("Back to List", "ModuleList")

1 个答案:

答案 0 :(得分:2)

尝试更改此行

return View(moduleV);

到此

return View(moduleV.FirstOrDefault());/