如何在模型中组合两个表类并将其调用到视图?

时间:2017-10-05 06:24:25

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我已经在模型中单独创建了两个类,并且我已经提到了表名,然后当我在视图中调用单个类PharmaMainModel时,我已合并到单个类PharmaMainModel中的表中类PharmaMainModel的属性不起作用。

namespace MedeilMVC_CLOUD.Models
{
    [Table("PharmacompanyReg")]
    public class PharmaCompany
    {
        [Key]
        public int PCompanyID { get; set; }
        public string PCompanyName { get; set; }
        public string PShortName { get; set; }
        public string PCompnaytype { get; set; }
        public string PCompnaySubType { get; set; }
        public string EstdYear { get; set; }
        public string PEmail { get; set; }
        public string PAddress1 { get; set; }
        public string PAddress2 { get; set; }
        public string PPincode { get; set; }
        public int PCountry { get; set; }
        public int PState { get; set; }
        public string PCity { get; set; }
        public string PCountryCode { get; set; }
        public string PContactperson { get; set; }
        public string PDesignationID { get; set; }
        public string PMobileNo { get; set; }
        public string PPhoneNo { get; set; }
        public string PPanNo { get; set; }
        public string PTinNo { get; set; }
        public string PGSTno { get; set; }
        public string PIECCode { get; set; }
        public string TermsandConditions { get; set; }
        public string PIPAddress { get; set; }
        public string PLatitude { get; set; }
        public string PLongitude { get; set; }
        public int CreatedBy = -1;
        public DateTime CreatedDate = DateTime.UtcNow;
        public int ModifyBy = -1;
        public DateTime ModifyDate = DateTime.UtcNow;
    }


    [Table("CompanySocialNetwork")]
    public class CompanySocial
    {
        [Key]
        public int CompanySocialID { get; set; }

        public int CompanyID { get; set; }

        public string FaceBookID { get; set; }

        public string TwitterID { get; set; }

        public string linkedinID { get; set; }

        public int CreatedBy = -1;

        public DateTime CreatedDate = DateTime.UtcNow;

        public int ModifiedBy = -1;

        public DateTime ModifiedDate = DateTime.UtcNow;
    }

    public class PharmaMainModel
    {
        public PharmaCompany pharmaCompany { get; set; }
        public CompanySocial CompanySocial { get; set; }
    }
}

当我在视图中调用单个类PharmaCompany时,数据工作正常。

但是当我在一个类PharmaMainModel中调用两个类时,在视图中不起作用

@model IEnumerable<MedeilMVC_CLOUD.Models.PharmaMainModel>

@{
    ViewBag.Title = "ViewPharmaCompany";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="page-content">
    <div class="container-fluid">
        <header class="section-header">
            <div class="tbl">
                <div class="tbl-row">
                    <div class="tbl-cell">
                        <h2>View Company</h2>
                    </div>
                    <div class="tbl-cell tbl-cell-action button">
                        <a href="#" data-toggle="modal" data-target="#upload3Modal" class="btn btn-rounded btn-block">Search&nbsp;&nbsp;<span class="font-icon-search"></span></a>
                    </div>
                </div>
            </div>
        </header>

        <div class="cards-grid" data-columns>
            @foreach (var pharmaModel in Model)
            {
                <div class="card-grid-col">
                    <article class="card-typical">
                        <div class="card-typical-section">
                            <div class="user-card-row">
                                <div class="tbl-row">
                                    @*<div class="tbl-cell tbl-cell-photo">
                                            <a href="#">
                                                <img src="~/img/photo-64-2.jpg" alt="">
                                            </a>
                                        </div>*@
                                    <div class="tbl-cell">
                                        <p class="user-card-row-name"><a href="@Url.Action("CompanyDetails", "Company", new { PCompanyid = pharmaModel.pharmaCompany.PCompanyID })">@pharmaModel.pharmaCompany.PCompanyName</a></p>
                                        <p class="color-blue-grey-lighter">@pharmaModel.pharmaCompany.CreatedDate</p>
                                    </div>
                                    <div class="tbl-cell tbl-cell-status">
                                        <a href="@Url.Action("EditCompany", new { Id = pharmaModel.pharmaCompany.PCompanyID })" class="font-icon font-icon-pencil active" title="Edit"></a>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="card-typical-section card-typical-content">
                            <header class="title"><a href="@Url.Action("CompanyDetails", "Company", new { PCompanyid = pharmaModel.pharmaCompany.PCompanyID })">@pharmaModel.pharmaCompany.PCompanyName</a></header>
                            <section class="contacts-page-section">
                                <p class="contact-other"><i class="font-icon font-icon-pin-2"></i>&nbsp;@pharmaModel.pharmaCompany.PAddress1</p>
                                <p class="contact-other"><i class="font-icon font-icon-mail"></i>&nbsp;@pharmaModel.pharmaCompany.PEmail</p>
                                <p class="contact-other"><i class="font-icon font-icon-phone"></i>&nbsp;@pharmaModel.pharmaCompany.PMobileNo</p>
                            </section>
                        </div>
                        <div class="card-typical-section">
                            <div class="card-typical-linked">in <a href="http://medeil.com/" target="_blank">www.medeil.com</a></div>
                            <a href="#" class="card-typical-likes">
                                <i class="font-icon font-icon-heart"></i>
                            </a>
                        </div>
                    </article><!--.card-typical-->
                </div><!--.card-grid-col-->

            }
        </div>
    </div>
</div>

修改

public ActionResult ViewPharmaCompany()
        {
            var data = pharma.PharmaCompany.SqlQuery("Select * from PharmacompanyReg ORDER BY PCompanyID  DESC").ToList();
            return View(data);
        }

Error

0 个答案:

没有答案