简体   繁体   English

ASP.NET 核心 model 绑定值为 null

[英]ASP.NET Core model binding values are null

I have the following code where I am looping through a property in the model object as follows.我有以下代码,我在其中循环访问 model object 中的一个属性,如下所示。 The BlogDataItems is of type BlogDataItems的类型

public IPagedList<BlogData> BlogDataItems { get; set; }

from using X.PagedList library.从使用 X.PagedList 库。 I am able to see the Image, title, body etc the values I am binding.我能够看到图像、标题、正文等我绑定的值。

@foreach (var item in Model.BlogDataItems)
{
    <!-- === Blog item 1 === -->
    <div class="col-md-4 col-sm-6 m-bottom-40">
        <div class="blog wow zoomIn" data-wow-duration="1s" data-wow-delay="0.7s">
            <div class="blog-media">
                <a href="blog_single_post.html">
                    @*<img src="~/img/blog/b1.jpg" alt="" asp-append-version="true">*@
                    <img id="ItemPreview" src="data:image;base64,@System.Convert.ToBase64String(item.ImageData)" alt="Image" height="200" width="220" />
                </a>
            </div><!--post media-->

            <div class="blog-post-info clearfix">
                <span class="time"><i class="fa fa-calendar"></i> @Html.DisplayFor(modelItem => item.PostedDateTime)</span>
                @*<span class="comments"><a href="#"><i class="fa fa-comments"></i> 4 Comments</a></span>*@
            </div><!--post info-->

            <div class="blog-post-body">
                <h4><a class="title" href="blog_single_post.html">@Html.DisplayFor(modelItem => item.BlogTitle)</a></h4>
                <p class="p-bottom-20">@Html.DisplayFor(modelItem => item.BlogContent)</p>
                <a class="read-more" asp-action="DetailedView" asp-controller="BlogData" asp-route-blogItem="@HttpContextAccessor.HttpContext.Request.Query["item"]">Read More >></a>
            </div><!--post body-->
        </div> <!-- /.blog -->
    </div> <!-- /.inner-col -->
}

Now please see the 'a class="read-more"' element, I am trying to bind the item using asp-route-blogItem="@item" to the controller action method and all the values inside are either null or default.现在请看'a class="read-more"' 元素,我正在尝试使用asp-route-blogItem="@item"将项目绑定到 controller 操作方法,里面的所有值都是 null 或默认值。 Please help.请帮忙。

[HttpGet]
public IActionResult DetailedView(BlogData blogItem)
{
    if (blogItem != null)
    {
    }

    return View(blogItem);
}

When I debug the code, this is what I have当我调试代码时,这就是我所拥有的

值为 null 或默认值

For your requirement,I think you could try asp-all-route-data instead of asp-route-yourkey根据您的要求,我认为您可以尝试asp-all-route-data而不是asp-route-yourkey

I tried as below:我尝试如下:

@{
    var id = HttpContextAccessor.HttpContext.Request.Query["id"];
    var routedata = new Dictionary<string, string>();
    routedata.Add("id", id);
    routedata.Add("BlogTitle", "title");
    routedata.Add("BlogContent", "content");

}

.........

<a class="read-more" asp-action="Privacy" asp-controller="Home"  asp-all-route-data="@routedata">Read More >></a>

The Result:结果:

在此处输入图像描述

you could check this document about modelbinding,and document about taghelper您可以查看有关模型绑定的文档,以及有关 taghelper 的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM