I want to render a partial view within the main view.
This is what I have done so far, but non of the code not loading the partial view.
Can anyone help me with this? Thanks in advance.
This is my main model
public class AppRequest
{
[Key]
public int Id { get; set; }
[Required]
[Display(Name = "Request Type")]
public int ReqType { get; set; }
[Required]
[Display(Name = "Requesting By")]
public int Req_By { get; set; }
[Required]
[Display(Name = "Requesting Date")]
public DateTime Req_Date { get; set; } = DateTime.Now;
[Required]
[Display(Name = "Request Location")]
public int Req_Location { get; set; }
[Required]
[Display(Name = "Request Heading")]
public string Req_Heading { get; set; }
[Display(Name = "Cover Note")]
public string Req_CoverNote { get; set; }
[Required]
[Display(Name = "Company Name")]
public int Company_Id { get; set; }
public virtual IList<Purchase> Purchase { get; set; }
public virtual IList<General> General { get; set; }
[NotMapped]
public Purchase PurchaseItem { get; set; }
}
#region Purchase
public class Purchase
{
[Key]
public int Id { get; set; }
[Required]
[ForeignKey("AppRequest")]
public int Req_Id { get; set; }
public virtual AppRequest AppRequest { get; set; }
public virtual IList<PurchasingEmpl> PurchasingEmpl { get; set; }
public virtual IList<PurchasingItems> PurchasingItems { get; set; }
}
public class PurchasingEmpl
{
[Key]
public int Id { get; set; }
[Required]
[ForeignKey("Purchase")]
public int Purchase_Id { get; set; }
public virtual Purchase Purchase { get; set; }
public int Emp_Id { get; set; }
public bool Status { get; set; }
}
public class PurchasingItems
{
[Key]
public int Id { get; set; }
[Required]
[ForeignKey("Purchase")]
public int Purchase_Id { get; set; }
public virtual Purchase Purchase { get; set; }
public int Supp_Id { get; set; }
public int Itm_Description_Id { get; set; }
public decimal Unit_Amount { get; set; }
public byte[] Attachment { get; set; }
public decimal Qty { get; set; }
public string Recomandation { get; set; }
public bool Status { get; set; }
public bool Settled { get; set; } = false;
public string PoNo { get; set; }
}
#endregion
And this is the View. My partial view name is "_Purchasing". So under this _Purchasing partial view, I have added another 2 partial views. So I need to load this main partial view to show the other details.
@model Asp_PASMVC.Models.AppRequest
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>AppRequest</h4>
<hr />
<div id="PurchseReq">
@Html.Partial("_Purchasing");
</div>
</div>
}
you can use @RenderPartial
and @Partial
@RenderPartial
vs @Partial
As mentioned, you can incorporate a partial view in a Razor view by using either of two methods: @RenderPartial
or @Partial
. The difference between the two methods may look small and harmless, but it may bite at you if you don’t know how to handle it.
The key difference between the two methods is that Partial returns a HTML-encoded string while @RenderPartial
is a void method that writes directly to the response output stream. The usage of the two methods is slightly different:
@Html.Partial("_yourPartialView")
@Html.RenderPartial("_yourPartialView")