呈现部分视图 ASP MVC
Rendering partial view ASP MVC
我想在主视图中渲染局部视图。
这是我到目前为止所做的,但没有代码没有加载局部视图。
谁能帮我解决这个问题?提前致谢。
这是我的主要模型
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>
}
您可以使用 @RenderPartial
和 @Partial
@RenderPartial
对比 @Partial
如前所述,您可以使用以下两种方法之一将分部视图合并到 Razor 视图中:@RenderPartial
或 @Partial
。两种方法的区别,看似小无害,但不知如何处理,可能会咬你一口。
这两种方法之间的主要区别在于 Partial returns 是 HTML 编码的字符串,而 @RenderPartial
是直接写入响应输出流的无效方法。两种方法的用法略有不同:
@Html.Partial("_yourPartialView")
@Html.RenderPartial("_yourPartialView")
我想在主视图中渲染局部视图。
这是我到目前为止所做的,但没有代码没有加载局部视图。
谁能帮我解决这个问题?提前致谢。
这是我的主要模型
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>
}
您可以使用 @RenderPartial
和 @Partial
@RenderPartial
对比 @Partial
如前所述,您可以使用以下两种方法之一将分部视图合并到 Razor 视图中:@RenderPartial
或 @Partial
。两种方法的区别,看似小无害,但不知如何处理,可能会咬你一口。
这两种方法之间的主要区别在于 Partial returns 是 HTML 编码的字符串,而 @RenderPartial
是直接写入响应输出流的无效方法。两种方法的用法略有不同:
@Html.Partial("_yourPartialView")
@Html.RenderPartial("_yourPartialView")