2013-06-27 29 views
0

選擇單行我有這個功能,顯示所有行的表,我可以用一些額外的細節MVC:從表

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); 
} 

表現出來鑑於現在我想選擇一個基於行在作爲參數傳遞標識

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觀點 這是我在查看所做

@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") 

回答

2

嘗試改變這一行

return View(moduleV); 

對此

return View(moduleV.FirstOrDefault());/