0

I have created two class separately in a model and i have mentioned the table name and then i have combined to tables in a single class PharmaMainModel when i invoke the single class PharmaMainModel in the view the properties of the class PharmaMainModel is not working.

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; }
    }
}

When i invoke single class PharmaCompany in the view the data is working fine.

But When i invoke two class in a single class PharmaMainModel is not working in the view

@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>

Edit

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

Error

Ivin Raj
  • 3,448
  • 2
  • 28
  • 65
  • Change the model directive like this: `@model IEnumerable` or define returned model in controller to match with `List`. – Tetsuya Yamamoto Oct 05 '17 at 06:26
  • Could you post the code that actually does the "combining" – Rand Random Oct 05 '17 at 06:26
  • @RandRandom check it now – Ivin Raj Oct 05 '17 at 06:42
  • Your controller method does not return `IEnumerable`! –  Oct 05 '17 at 06:47
  • @IvinRaj - okay, but there is no combining - you only query ``PharmaCompany`` - so where is the ``PharmaMainModel`` in your posted code? so it should more be something like this (droped the SQL since limited space in comment) ``var pharmaCompanies = pharma.PharmaCompany.SqlQuery("").ToList(); var companySocials = pharma.CompanySocial.SqlQuery("").ToList(); return View(pharmaCompanies.Select((x, i) => return new PharmaMainModel { pharmaCompany = x, CompanySocial = companySocials[i] }));`` - I dont know how the two models are linked so I just showed you a very basic approach (not tested code) – Rand Random Oct 05 '17 at 06:48
  • some error is showing @RandRandom – Ivin Raj Oct 05 '17 at 06:54
  • @IvinRaj - I have faith in your ability to fix them - I just wanted to give you the general idea – Rand Random Oct 05 '17 at 07:05
  • thanks sir @RandRandom . – Ivin Raj Oct 05 '17 at 07:05

0 Answers0