渲染局部视图 ASP MVC

时间:2021-06-17 07:46:08

标签: asp.net-mvc model-view-controller renderpartial

我想在主视图中渲染局部视图。

这是我到目前为止所做的,但没有加载局部视图的代码。

谁能帮我解决这个问题?提前致谢。

这是我的主要模型

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

这是视图。我的部分视图名称是“_Purchasing”。所以在这个 _Purchasing 局部视图下,我又添加了 2 个局部视图。所以我需要加载这个主要的局部视图来显示其他细节。

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

1 个答案:

答案 0 :(得分:0)

您可以使用 @RenderPartial@Partial
@RenderPartial@Partial 如前所述,您可以使用以下两种方法之一将部分视图合并到 Razor 视图中:@RenderPartial@Partial。这两种方法的区别可能看起来很小而且无害,但如果你不知道如何处理它,它可能会咬你。
这两种方法的主要区别在于 Partial 返回一个 HTML 编码的字符串,而 @RenderPartial 是一个直接写入响应输出流的 void 方法。两种方法的用法略有不同:

@Html.Partial("_yourPartialView")
@Html.RenderPartial("_yourPartialView")
相关问题