0

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

8
  • 1
    Is it _Purchase or _Purchasing You say one thing but then try to render the other? Is this just a simple typo? Commented Jun 17, 2021 at 7:51
  • 1
    To pass model to partial view, modify @Html.Partial("_Purchasing",Model); Commented Jun 17, 2021 at 8:19
  • @AjeetKumar then this error showed up The model item passed into the dictionary is of type 'Asp_PASMVC.Models.AppRequest', but this dictionary requires a model item of type Commented Jun 17, 2021 at 8:23
  • @phuzi Sory my typing mistake, the partial view name is _Purchasing Commented Jun 17, 2021 at 8:33
  • 1
    @smcdevelpments, then pass the modal as @Html.Partial("_Purchasing",Model.Purchase); Commented Jun 17, 2021 at 8:54

1 Answer 1

0

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")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.