将数据从控制器传递到视图

本文关键字:视图 控制器 数据 | 更新日期: 2023-09-27 18:35:49

在过去的一天半里,我一直试图找到这个答案,有时看起来我是如此接近,但到目前为止没有运气。我有一个控制器,我在其中定义了一个 LINQ 查询,并试图弄清楚如何将结果传递到列表视图。 以下是控制器代码:

namespace CMS.Controllers
{
    public class SurveyController : Controller
    {
        private SupportEntities supdb = new SupportEntities();
        private BuisnessEntities bsdb = new BuisnessEntities();
        //
        // GET: /Survey/BizSurveyC
        public ViewResult BizSurveyC(string nipaKey, string bOrg)
        {
            // use the next two lines for testing and then either delete or comment them out.
            nipaKey = "22";
            bOrg = "MPC";
            var Cquery = from Mstr in bsdb.BizOrgInsts
                                     join Dat in bsdb.BizSurveyQ on Mstr.ID equals Dat.MASTERID
                                     where Mstr.NIPAKEY == nipaKey & Mstr.FULCIRCORG == bOrg
                                     orderby Mstr.STREETSUFX, Dat.ADDRESS, Mstr.NUMBER
                                     select new { MasterId = Mstr.ID, Name = Mstr.OLDNAME, Mstr.ADDRESS, Mstr.NIPAKEY, Dat.SURVEYDATE, SurveyId = Dat.ID, Dat.RESURVEYOF, Dat.STAMP };
            //ViewBag.BizQuery = Cquery;
            ViewData["BizQuery"] = new SelectList(Cquery);
            return View();
        }
    }
}

正如你通过查看可以看出的那样,我已经尝试了ViewData和Viewbag,但到目前为止没有运气


以下是现在出现的方式:

视图模型类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace CMS.Models
{
    public class BizSurveyCVM
    {
        public long? MasterId { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public string NipaKey { get; set; }
        public string Date { get; set; }
        public long? SurveyId { get; set; }
        public long? Resurvey { get; set; }
        public string DateStamp { get; set; }
    }
}

修改后的操作

        var Cquery = (from Mstr in bsdb.BizOrgInsts
                                    join Dat in bsdb.BizSurveyQ on Mstr.ID equals Dat.MASTERID
                                    where Mstr.NIPAKEY == nipaKey & Mstr.FULCIRCORG == bOrg
                                    orderby Mstr.STREETSUFX, Dat.ADDRESS, Mstr.NUMBER
                                    select new BizSurveyCVM
                                    {
                                        MasterId = Mstr.ID,
                                        Name = Mstr.OLDNAME,
                                        Address = Mstr.ADDRESS,
                                        NipaKey = Mstr.NIPAKEY,
                                        Date = Dat.SURVEYDATE,
                                        SurveyId = Dat.ID,
                                        Resurvey = Dat.RESURVEYOF,
                                        DateStamp = Dat.STAMP
                                    }).ToList();
        return View(Cquery);
    }

BizSurveyC View

@model List<CMS.Models.BizSurveyCVM>
<table>
    <tr>
        <th>
            MasterId
        </th>
        <th>
            Name
        </th>
        <th>
            Address
        </th>
        <th>
            NipaKey
        </th>
        <th>
            Date
        </th>
        <th>
            SurveyId
        </th>
        <th>
            Resurvey
        </th>
        <th>
            DateStamp
        </th>
        <th></th>
    </tr>
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.MasterId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Address)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.NipaKey)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Date)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.SurveyId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Resurvey)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DateStamp)
        </td>
</table>

下面是结果视图:

抱歉,还不允许保存图像,但在视图中我有标题但没有数据。

我显然在视图或查询中有一些工作要做,但事情看起来好多了,谢谢大家的帮助。 谢谢

将数据从控制器传递到视图

您可以创建一个 ViewModel 类来保存查询结果,然后为新的 ViewModel 类强键入视图:

视图模型类

public class BizSurveyCVM 
{
    public long MasterId { get; set; }
    public string Name { get; set; }
    ...
}

修改后的操作

var Cquery = (from Mstr in bsdb.BizOrgInsts
              join Dat in bsdb.BizSurveyQ on Mstr.ID equals Dat.MASTERID
              where Mstr.NIPAKEY == nipaKey & Mstr.FULCIRCORG == bOrg
              orderby Mstr.STREETSUFX, Dat.ADDRESS, Mstr.NUMBER
              select new BizSurveyCVM  { MasterId = Mstr.ID, Name = Mstr.OLDNAME, ...}
             ).ToList();
return View(Cquery);

BizSurveyC View

@model List<{namespace}.BizSurveyCVM>
@foreach(var item in Model) {
     {HTML Mark-up}
}

编辑:这是基于更新问题的视图的更新示例:

@model List<{namespace}.BizSurveyCVM>
@foreach(var item in Model) {
    <tr>
        <td>
            @item.MasterId
        </td>
        <td>
            @item.Name
        </td>
        <td>
            @item.Address
        </td>
        ...
    </tr>
}
return View(Cquery);

return View("ViewName",Cquery);

然后在您的视图中,模型类型应与 Cquery 的类型匹配。但我发现它更容易(假设您使用的是 VS),只需右键单击方法主体中的某处并单击"添加视图",然后从模型类型列表中选择模型。

你确定要做吗?

视图中的集合更有用,因此您可以构建所需的任何内容,例如选择列表。

不要忘记在选择列表中定义谁是你的数据值和显示值,比如..

ViewData["BizQuery"] = new SelectList(Cquery, null, "MasterId", "Name");
好的,您的

代码对我有用,因此请检查您的视图,您必须在视图数据中对对象进行正确的转换

你需要有这样的东西

@foreach (SelectListItem item in (SelectList)ViewData["BizQuery"])
{
    <p>@item.Text</p>
}