如何组合类,但将第二类发送到局部视图?

时间:2017-10-22 18:42:47

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

我有一个由属性代理组成的类,每个代理都有多个属性,因此在我的视图模型中,我有以下内容。我的问题是如果我只是在我的下面列出的Agent.cshtml视图的视图中引用代理

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

属性类

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

模特经纪人

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


 @Html.Partial("ActiveListings.cshtml")

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

我在这里如何访问要发送给模型的属性,它将呈现已存储的代理的属性列表。

我需要在这里向代码添加代理的模型属性。

@ Html.Partial(&#34; ActiveListings.cshtml&#34)

在我的代理控制器中,我有以下索引方法,我需要在这里添加我的属性。

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

1 个答案:

答案 0 :(得分:1)

您可以加载Properties对象的Agent集合,在主视图中,您可以将Properties集合作为局部视图的模型传递。

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

在主视图中

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

您的ActiveListings部分视图强烈类型化为Property对象的集合

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