MVC 4 – Partial View

In order to create partial view, right click on Shared folder->Add->View
1.View name: It is common practice partial view’s name starts with “_” symbol for example “_AuctionPartial”
2.Create a strongly typed view: set checked
3.Model class: choose your model(Auction)
4.Create as a partial view: set checked

@model Auction.Models.Auction

<div class="one-auction">
    <h4 class="title">@Model.Title</h4>
    <p class="end-time">Auction ends at @Model.EndTime.ToString("g")</p>
    <p class="price">Price:@Model.CurrentPrice.GetValueOrDefault(Model.StartPrice).ToString("C")</p>
</div>

Now we created Auction partial view. So lets use it in our main view where we showing list of items.
First of all we send array of objects from our controller – AuctionsController:

        public ActionResult Index()
        {

            var auctions = new[] {
                new Models.Auction(){
                    Title = "Macbook Air 2013",
                    Description = "Amazing ultrabook",
                    StartTime = DateTime.Now,
                    EndTime = DateTime.Now.AddDays(7),
                    StartPrice = 1.00m,
                    CurrentPrice = null ,
                },
                new Models.Auction(){
                    Title = "iPod Touch 5",
                    Description = "The best music player",
                    StartTime = DateTime.Now,
                    EndTime = DateTime.Now.AddDays(3),
                    StartPrice = 1.00m,
                    CurrentPrice = 3.00m ,
                },
                new Models.Auction(){
                    Title = "Drone",
                    Description = "You can use to take dronee",
                    StartTime = DateTime.Now,
                    EndTime = DateTime.Now.AddDays(30),
                    StartPrice = 10.00m,
                    CurrentPrice = 30.00m ,
                },
            };

            return View(auctions);
        }

On Index view we will show it:

@model IEnumerable<Auction.Models.Auction>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>


@foreach (var item in Model) {
    @Html.Partial("_AuctionPartial", item)
}