Asp.net ModelView方法

时间:2013-07-03 08:31:34

标签: asp.net-mvc

我正在尝试使用asp.net MVC4(razor语法)和databasefirst方法构建一个单页面的应用程序。 您可以看到我的应用程序的简单模型,如下所示。

-OPPORTUNITY<br />
--OPPORTUNITY DETAIL(1)<br />
---Seleted Solution-1<br />
---Seleted Solution-2<br />
---Seleted Solution-3<br />
--OPPORTUNITY DETAIL(2)<br />
---Seleted Solution-1<br />
---Seleted Solution-2<br />
--OPPORTUNITY DETAIL(3)<br />
---Seleted Solution-1<br />
---Seleted Solution-2<br />
---Seleted Solution-3<br />

我在表格中有很多解决方案,并且有很多参数。在一页中显示所有信息的最佳途径是什么?我正在尝试使用创建一个新的viewmodel。但我不确定它的性能,因为它从表中获取所有数据。

这是我的模特课..

OPPORNUTIES CLASS

namespace CRM_V1.Models
{
    using System;
    using System.Collections.Generic;

    public partial class CRM_OPPORTUNITIES
    {
        public CRM_OPPORTUNITIES()
        {
            this.CRM_OP_ATTACHMENTS = new HashSet<CRM_OP_ATTACHMENTS>();
            this.CRM_OPP_DETAILS = new HashSet<CRM_OPP_DETAILS>();
        }

        public decimal ID { get; set; }
        public Nullable<decimal> COMPANYID { get; set; }
        public Nullable<decimal> SEGMENTID { get; set; }
        public Nullable<decimal> SEGMENTDETAILID { get; set; }
        public Nullable<decimal> CUSTOMERID { get; set; }
        public Nullable<decimal> PROJECTID { get; set; }
        public Nullable<decimal> STATUSID { get; set; }
        public Nullable<System.DateTime> CRDATE { get; set; }
        public Nullable<decimal> CRUSER { get; set; }
        public string CODE { get; set; }
        public Nullable<System.DateTime> MDDATE { get; set; }
        public Nullable<decimal> MDUSER { get; set; }
        public Nullable<decimal> IS_VALID { get; set; }
        public Nullable<decimal> OWNERID { get; set; }
        public Nullable<decimal> PRJCTGRPID { get; set; }
        public Nullable<decimal> ORDERSUMMARY { get; set; }

        public string NOTES { get; set; }

        public virtual CRM_COMPANIES CRM_COMPANIES { get; set; }
        public virtual CRM_CUSTOMERS CRM_CUSTOMERS { get; set; }
        public virtual ICollection<CRM_OP_ATTACHMENTS> CRM_OP_ATTACHMENTS { get; set; }
        public virtual CRM_OP_STATUS CRM_OP_STATUS { get; set; }
        public virtual ICollection<CRM_OPP_DETAILS> CRM_OPP_DETAILS { get; set; }
        public virtual CRM_SEGMENTS CRM_SEGMENTS { get; set; }
        public virtual CRM_SEGMENT_DETAILS CRM_SEGMENT_DETAILS { get; set; }
        public virtual CRM_PROJECTS CRM_PROJECTS { get; set; }
        public virtual CRM_PRJCT_GROUPS CRM_PRJCT_GROUPS { get; set; }
    }
}

详细课程

namespace CRM_V1.Models
{
    using System;
    using System.Collections.Generic;

    public partial class CRM_OPP_DETAILS
    {
        public CRM_OPP_DETAILS()
        {
            this.CRM_OPP_DETAILS1 = new HashSet<CRM_OPP_DETAILS>();
            this.CRM_REVENUE_PLAN = new HashSet<CRM_REVENUE_PLAN>();
            this.CRM_SOLUTION_DISTRIBUTION = new HashSet<CRM_SOLUTION_DISTRIBUTION>();
        }

        public decimal ID { get; set; }
        public string CODE { get; set; }
        public Nullable<decimal> OPPID { get; set; }
        public Nullable<decimal> ORDERAMOUNT { get; set; }
        public Nullable<System.DateTime> ORDERDATE { get; set; }
        public string ORDERCURRENCY { get; set; }
        public Nullable<decimal> REVENUEAMOUNT { get; set; }
        public Nullable<System.DateTime> REVENUEDATE { get; set; }
        public Nullable<decimal> REVENUECURRENCY { get; set; }
        public Nullable<short> MARGINRATE { get; set; }
        public Nullable<System.DateTime> TARGETDATE { get; set; }
        public Nullable<short> POSSIBILITY { get; set; }
        public Nullable<decimal> STATUSID { get; set; }
        public string NOTES { get; set; }
        public Nullable<System.DateTime> CRDATE { get; set; }
        public Nullable<decimal> CRUSER { get; set; }
        public Nullable<System.DateTime> MDDATE { get; set; }
        public Nullable<decimal> MDUSER { get; set; }
        public Nullable<decimal> IS_VALID { get; set; }
        public Nullable<decimal> TYPEID { get; set; }
        public Nullable<decimal> OWNERID { get; set; }
        public Nullable<decimal> AFFLDETAILID { get; set; }
        public Nullable<decimal> REVENUEDOLLAR { get; set; }
        public Nullable<decimal> ORDERDOLLAR { get; set; }
        public Nullable<decimal> ISHAVEREVPLAN { get; set; }
        public Nullable<decimal> SOLPLANDONE { get; set; }
        public Nullable<decimal> SOLUTIONID { get; set; }
        public string PAFDCANO { get; set; }
        public Nullable<decimal> DCAKOD { get; set; }

        public virtual CRM_OP_DETAIL_STATUS CRM_OP_DETAIL_STATUS { get; set; }
        public virtual CRM_OPP_DETAIL_TYPE CRM_OPP_DETAIL_TYPE { get; set; }
        public virtual CRM_OPPORTUNITIES CRM_OPPORTUNITIES { get; set; }
        public virtual ICollection<CRM_OPP_DETAILS> CRM_OPP_DETAILS1 { get; set; }
        public virtual CRM_OPP_DETAILS CRM_OPP_DETAILS2 { get; set; }
        public virtual ICollection<CRM_REVENUE_PLAN> CRM_REVENUE_PLAN { get; set; }
        public virtual ICollection<CRM_SOLUTION_DISTRIBUTION> CRM_SOLUTION_DISTRIBUTION { get; set; }
    }
}

解决方案分配类

namespace CRM_V1.Models
{
    using System;
    using System.Collections.Generic;

    public partial class CRM_SOLUTION_DISTRIBUTION
    {
        public CRM_SOLUTION_DISTRIBUTION()
        {
            this.CRM_SOLUTION_VENDOR = new HashSet<CRM_SOLUTION_VENDOR>();
        }

        public decimal ID { get; set; }
        public Nullable<decimal> OPPDETAILID { get; set; }
        public Nullable<decimal> SOLUTIONID { get; set; }
        public Nullable<decimal> CRUSER { get; set; }
        public Nullable<System.DateTime> CRDATE { get; set; }
        public Nullable<decimal> MDUSER { get; set; }
        public Nullable<System.DateTime> MDDATE { get; set; }
        public Nullable<decimal> AMOUNT { get; set; }
        public Nullable<decimal> IS_VALID { get; set; }
        public string CURRENCY { get; set; }
        public Nullable<decimal> AMOUNTDOLLAR { get; set; }
        public Nullable<decimal> MARGIN { get; set; }
        public Nullable<decimal> MARGINRATE { get; set; }
        public Nullable<decimal> DISTMARGIN { get; set; }
        public Nullable<decimal> VENDORID { get; set; }
        public Nullable<System.DateTime> VENDORDATE { get; set; }

        public virtual CRM_OPP_DETAILS CRM_OPP_DETAILS { get; set; }
        public virtual CRM_SOLUTIONS CRM_SOLUTIONS { get; set; }
        public virtual CRM_VENDORS CRM_VENDORS { get; set; }
        public virtual ICollection<CRM_SOLUTION_VENDOR> CRM_SOLUTION_VENDOR { get; 

我开始创建一个MODELVIEW以显示机会和相关的详细信息,但我失败了?

这是我的modelview类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CRM_V1.Models;

namespace CRM_V1.ViewModels
{
    public class OppViewModel
    {

        public List<CRM_OPPORTUNITIES> OppList { get; set; }
        public List<CRM_OPP_DETAILS> OppDetailList { get; set; }


    }
}

我的控制器操作

public ActionResult addingDetailNew(decimal id = 0)
        {
            CRM_OPPORTUNITIES crm_opportunities = db.CRM_OPPORTUNITIES.Find(id);


            var viewModel2 = new OppViewModel
            {
                viewModel2 = db.CRM_OPPORTUNITIES.ToList()

            };



            if (crm_opportunities == null)
            {
                return HttpNotFound();
            }
            return View(viewModel2);
        }

最后我的观点。

@model CRM_V1.ViewModels.OppViewModel

@{
    ViewBag.Title = "addingDetailNew";
}

<h2>addingDetailNew</h2>

@Html.DisplayFor(model => model.NOTES)

但我无法获得NOTES字段。 我的第二个问题是如何获得像model.CUSTOMERS.NAME这样的连接字段。 ?

此致

1 个答案:

答案 0 :(得分:1)

您必须循环ViewModel的OppList属性,您必须更新您的视图:

@model CRM_V1.ViewModels.OppViewModel

@{
    ViewBag.Title = "addingDetailNew";
}

<h2>addingDetailNew</h2>

@foreach(CRM_OPPORTUNITIES opportunity in model.OppList) {
    <h3>@opportunity.NOTES</h3>
    @foreach(CRM_OPP_DETAILS detail in opportunity.CRM_OPP_DETAILS){
         <p>@detail.CODE</p>
         <!-- etc... if you want to retrieve more informations -->
    }
}

之后,您可以继续检索所需的对象/属性。

希望它有所帮助!