【问题标题】:Rendering partial view ASP MVC渲染局部视图 ASP MVC
【发布时间】:2021-06-17 07:46:08
【问题描述】:

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

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

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

这是我的主要模型

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

【问题讨论】:

  • _Purchase 还是_Purchasing 你说的是一回事,然后尝试渲染另一个?这只是一个简单的错字吗?
  • 要将模型传递给局部视图,请修改@Html.Partial("_Purchasing",Model);
  • @AjeetKumar 然后出现此错误The model item passed into the dictionary is of type 'Asp_PASMVC.Models.AppRequest', but this dictionary requires a model item of type
  • @phuzi 对不起我的打字错误,部分视图名称是_Purchasing
  • @smcdevelpments,然后将模态传递为@Html.Partial("_Purchasing",Model.Purchase);

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


【解决方案1】:

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

@Html.Partial("_yourPartialView")
@Html.RenderPartial("_yourPartialView")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-08
    相关资源
    最近更新 更多