dotnetdevcsharp
dotnetdevcsharp

Reputation: 3980

How do I combine to classes but send second class to partial view?

I have a class which consists of agents of properties each agent has mutiple properties against them so in my view model i have the following. My question is If I just reference Agents in the view on my view of Agent.cshtml listed below

public class Agents
{
    public int AgentId { get; set; }
    public int RatingsId { get; set; }
    public string Name { get; set; }
    public string About { get; set; }
    public string Bookerish { get; set; }
    public string Specialties { get; set; }
    public int LicenseNo { get; set; }
    public string image { get; set; }
    public double LocalKnowledge { get; set; }
    public double ProcessExpertise { get; set; }
    public double Responsiveness { get; set; }
    public double NegotiationSkills { get; set; }
    public int Reviews { get; set; }
    public double RatingAverage { get; set; }
    public List<Property> Properties { get; set; }
}

Property Class

public class Property 
{
    public int PropertyId { get; set; }
    public string image { get; set; }
    public string Details { get; set; }
    public string reprsented { get; set; }
    public decimal Price { get; set; }

    public int Beds { get; set; }
    public double Baths { get; set; }

    public double SQFT { get; set; }

    public Boolean isFeatuered { get; set; }

    public int isActiveListing { get; set; }
    public Boolean isPastSale { get; set; }
}

model Agents

@{
  ViewData["Title"] = "About";
 }


 @Html.Partial("ActiveListings.cshtml")

 <div class="l-divider l-divider--big"></div>

Where I am here how would one access the properties to send to the model that it would render a list of properties for that agent that has been stored.

I need someway to add to this pass the model property of agents here.

@Html.Partial("ActiveListings.cshtml")

In My agent controller i have the following index method where would I need to add my properies to here.

 Agents _newAgent = new Agents();

        _newAgent.AgentId = 5;
        _newAgent.Name = "Melissa Crosby";
        _newAgent.Specialties = "Property Management, Buyer’s Agent, Listing Agent";
        _newAgent.Bookerish = " Berkshire Hathaway HomeServices Elite Real Estate";
        _newAgent.image = "uploads/agents/5.png";
        _newAgent.Reviews = 16;
        _newAgent.RatingAverage = 4.5;
        _newAgent.LocalKnowledge = 4.5;
        _newAgent.ProcessExpertise = 4.2;
        _newAgent.NegotiationSkills = 4.1;
        _newAgent.About = "<p> Being a full-service Realtor since 2007, I have been baptized by fire in a very tough housing market. I have successfully closed over 60 transactions and processed over 70 short sales both as the listing agent and some for other agents. I am very knowledgeable about lenders and their processes. I strive to exceed expectations and never forget that I am always accountable to my clients. </p> <p> My objective is to establish relationships for life, not just for the current transaction. I enjoy assisting home buyers and sellers to get moved to a better place, one that is exciting. </p>";
        _newAgent.LicenseNo = 5452129;
        return View(_newAgent);
    }

Upvotes: 0

Views: 22

Answers (1)

Shyju
Shyju

Reputation: 218782

You can load the Properties collection of your Agent object and in your main view, you can pass the Properties collection as the model for the partial view.

Agents agent= new Agents();
agent.AgentId = 5;
agent.Name = "Melissa Crosby";
// To do : Fill other properties needed

// Load the Properties collection property
agent.Properties = new List<Property>{
  new Property { PropertyId=1, Details="4 Bedroom house"},
  new Property { PropertyId=2, Details="3 Bedroom house"},
};
return View(agent);

And in your main view

@model Agents
<h1>@Model.Name</h1>        
@Html.Partial("ActiveListings",Model.Properties)

Where your ActiveListings partial view is strongly typed to a collection of Property objects

@model List<Property>
@if (Model != null && Model.Any())
{
    foreach (var item in Model)
    {
        <p>@item.Details</p>
        <p>@item.PropertyId</p>
    }
}

else
{
    <p>No properties found!</p>
}

Upvotes: 1

Related Questions