C#-在ASP中使用多个数据调用.NET MVC 3视图

本文关键字:调用 数据 NET MVC 视图 ASP C#- | 更新日期: 2023-09-27 17:59:27

我正在努力学习ASP。NET MVC 3与剃须刀,并且到目前为止已经掌握了它的窍门。

我一直在努力让多个数据调用工作。

我正在做的事情的背景。我有一个页面,将显示有关一个项目的信息(我正在工作),但我也想显示该项目的进度(这是另一个数据调用)。我无法显示进度数据。我试着使用viewbag,但得到了:

系统。数据对象。ObjectResult`1[System.String]

我将存储过程与实体框架一起使用。

我很感激你们的帮助。

编辑:

以下是我的问题的一个粗略示例:

private SDMPREntities _db = new SDMPREntities();
public ActionResult Details(int id) { 
ViewData.Model = _db.ViewProjectDetails(id).ToList();
ViewBag.Progress = _db.ProjectProgress();
return View();
} 

第2版:

@詹姆斯·德安杰洛,是的。这是剃须刀的代码:

@model IEnumerable<DeveloperManagerSharp.Models.Projects>
@{
ViewBag.Title = "Project Details";
Layout = "~/Views/Shared/project-ui.cshtml";
}
<div class="post">
<h3 class="title">
    Project Details</h3>
<div class="entry">
    <h4>
        Select a Project</h4>
    <p>
        Select a project below to see their progress or to update any of their properties.</p>
</div>
</div>
@foreach (var item in ViewData.Model)
{    
<div class="post">
    <h3 class="title">@item.project_name (@item.project_version) @ViewBag.Progress%</h3>
    <div class="meta">
        <div class="posted">
            @Html.ActionLink("Edit", "Edit", new { id = item.PID }) |
            @Html.ActionLink("Back to Projects", "Index")
        </div>
        @if (@item.project_completed == "1") {
            <div class="date">
                Project is Completed.
            </div>
            } else {
            <div class="date">
                Project is Under Work.
            </div>       
            }
    </div>
    <div class="entry">
        @if(@item.project_status == "0")
            {
            <span>Status: Planning</span><br />
            }
            else if(@item.project_status == "1")
            {
            <span>Status: Alpha</span><br />
            }
            else if(@item.project_status == "2")
            {
            <span>Status: Beta</span><br />
            }
            else if(@item.project_status == "3")
            {
            <span>Status: Release Candiate</span><br />
            }
            else if(@item.project_status == "4")
            {
            <span>Status: Active</span><br />
            }
            else if(@item.project_status == "5")
            {
            <span>Status: Maure</span><br />
            }
            else if(@item.project_status == "6")
            {
            <span>Status: Discontinued</span><br />
            }
        Starts: @item.project_sdate<br />
        Ends: @item.project_edate<br />
        <em>@item.project_desc</em>
    </div>
</div>
}

C#-在ASP中使用多个数据调用.NET MVC 3视图

调用

_db.ProjectProgress();

很可能返回一个ObjectResult,它表示一个项的集合。

所以当你试图用在视图中显示它时

@ViewBag.Progress

正在调用ToString(),它只是打印出类型。

为了解决这个问题,您需要从集合中获取一个特定的结果。在不知道你的数据结构的情况下,我只能冒险猜测,例如

_db.ProjectProgress().Single(p=>p.ProjectId == id)

请尝试以下操作。

数据层代码

 public class ProjectDetailManager 
 {   
   public static List<ViewProjectDetail> GetProjectDetailById(int id)    
   {
       using(var dbContext = new SDMPREntities())
       {
          return dbContext.ViewProjectDetails(id).ToList();
       }    
    }
    public static ProjectProgress GetProjectProgress(int id)    
    {
       using(var dbContext = new SDMPREntities())
       {
          return dbContext.ProjectProgress().Where(x=>x.ProjectId ==id).FirstOrDefault();
       }    
    }
 }

控制器代码:

public ActionResult Details(int id) 
{
  ViewData.Model = ProjectDetailManager.GetProjectDetailById(id); 
   ViewBag.Progress = ProjectDetailManager.GetProjectProgress(id);
   return View();    
}

查看代码

     @model IEnumerable<ViewProjectDetail> 
    @{
        ViewBag.Title = "Project Details";
        Layout = "~/Views/Shared/_SiteContent.cshtml";
     }
@// not sure what your object look like but here an example 
    <h1>Project Status: @(ViewBag.Progress != null ? ViewBag.Progress.Status : string.Empty)</h1>
       @foreach(var item in Model) 
       {     
          //format display whatever 
       }