查看不显示模型传递的数据

时间:2014-01-15 16:19:45

标签: asp.net-mvc asp.net-mvc-4 view ef-code-first

我有MVC 4项目应该为提案创建一组记录,然后在保存后应该重定向到某个页面,我将显示所有记录。

这是我保存后的问题,它会重定向,但从模型传递到视图的数据不会更新/不会显示在视图上。

以下是我获取数据的控制器操作:

    private CoverageViewModel coveragedata = new CoverageViewModel();
    private NewProposalDBContext db = new NewProposalDBContext();
    //
    // GET: /Coverage/
    public ActionResult Index()
    {
        CoverageViewModel coverage = new CoverageViewModel();
        List<Institution> institutions = db.Institutions.ToList();

        if (coveragedata.Proposal != null)
        {
            coverage.Institution = institutions;
            coverage.Proposal = (List<NewProposal>)Session["coveragedata"];
        }
        else
        {
            List<NewProposal> listProposal = new List<NewProposal>();
            listProposal = db.NewProposal.ToList();
            List<LeadsManagement> listLeadsMgt = new List<LeadsManagement>();
            listLeadsMgt = db.LeadsManagements.ToList();
            List<LeadEmployees> listLeadsEmployees = new List<LeadEmployees>();
            listLeadsEmployees = db.LeadEmployees.ToList();
            List<VacsInAction> listVacsInAction = new List<VacsInAction>();
            listVacsInAction = db.VacsInActions.ToList();
            List<NewProposal> listofImplementedProposal = new List<NewProposal>();
            listofImplementedProposal = 
                       db.NewProposal.Where(x => x.Status.StatusID == 3).ToList();

            coverage.Institution = institutions;
            coverage.Proposal = listProposal;
            coverage.LeadsManagement = listLeadsMgt;
            coverage.LeadsEmployee = listLeadsEmployees;
            coverage.VacsInAction = listVacsInAction;
    }

    return View(coverage);
}

这是我的观点 我使用session来获取控制器的最新数据并尝试覆盖模型的值。

@{
ViewBag.Title = "Index";

if(Session["coveragedata"] != null)
{
    Model.Proposal = (List<ULIV.ViewModels.NewProposal>)Session["coveragedata"];
}

if(Session["leadsMgt"] != null)
{
    Model.LeadsManagement = (List<ULIV.Models.LeadsManagement>)Session["leadsMgt"];
}

if(Session["leadsEmp"] != null)
{
    Model.LeadsEmployee = (List<ULIV.Models.LeadEmployees>)Session["leadsEmp"];
}

以下是数据的实际显示:

@foreach (var proposal in Model.Proposal)
{
      if (proposal.Status.StatusID == 1)
      {
           <tr>
           <td align="center">
                    @Html.DisplayFor(modelItem => proposal.ProposalCode)
           </td>
           <td align="center">
                  @Html.DisplayFor(modelItem => proposal.VaccineType.VaccineTypeName)
           </td>
           <td align="center">
                  @Html.DisplayFor(modelItem => proposal.Product.ProductName) 
           </td>
           <td align="center">
                  @Html.DisplayFor(modelItem => proposal.ProposedUnitPrice)
           </td>
           <td align="center">
                  @Html.DisplayFor(modelItem => proposal.ProposedVolume)
           </td>
           <td align="center">
                  @Html.DisplayFor(modelItem => proposal.Status.StatusName)
           </td>
           </tr>
       }
}

2 个答案:

答案 0 :(得分:0)

我认为您的观点不是强类型视图。您应该在页面顶部添加一行 喜欢

@model CoverageViewModel。

答案 1 :(得分:0)

我认为这可行:

您已经在循环模型列表,因此您可以使用它:

@Html.Display(proposal.ProposalCode)

而不是:

 @Html.DisplayFor(modelItem => proposal.ProposalCode)

由于