绑定Razor页面的下拉列表到复杂类型数据库时,无法将其绑定到视图模型。

huangapple go评论56阅读模式
英文:

Razor pages binding dropdownlist to complex type database not binding viewmodel

问题

我有一个从数据库填充的下拉列表,它渲染得很好,项目也显示出来。问题出现在当我尝试保存模型时,视图状态说对于postCategory的标题和描述无效,因为它们为null,但选择中具有Id值。

我的数据库类如下:

 public class Article
{
 public long ArticleId { get; set; }
 [Required]
        [MaxLength(200)]
        public string Title { get; set; }
    public ArticleCategories PostCategory { get; set; } //this the problem
}

    public class ArticleCategories
    {
       
        public long Id { get; set; }
       
        [Required]
        [MaxLength(100)]
        public string Title { get; set; }
        [Required]
        [MaxLength(300)]
        public string Description { get; set; }
        public string Slug { get; set; }
        public List<Article> AssociatedPosts { get; set; }
    }

在我的页面模型中,我加载下拉列表如下。

public ArticleCategories NewArticleCategory { get; set; }
public List<SelectListItem> PostCategories
{

    get
    {

        List<SelectListItem> NewList = new List<SelectListItem>();

        NewList = _context.ArticleCategories.Select(a =>
                              new SelectListItem
                              {
                                  Value = a.Id.ToString(),
                                  Text = a.Title.ToString(),
                              }).ToList();

        return NewList;

    }

}

页面上的代码如下:

<div class=&quot;form-group&quot;>
    <label asp-for=&quot;BlogArticle.PostCategory&quot; class=&quot;control-label&quot;>&lt;/label&gt;
    <select asp-for=&quot;BlogArticle.PostCategory.Id&quot; class=&quot;form-control&quot; asp-items=&quot;Model.PostCategories&quot;>
            &lt;option value=&quot;&quot;&gt;--选择一个类别--&lt;/option&gt;
        </select>

        @Html.HiddenFor(m=&gt;m.BlogArticle.PostCategory.Title )
        
        @Html.HiddenFor(m=&gt;m.BlogArticle.PostCategory.Description )
  
    <span asp-validation-for=&quot;BlogArticle.PostCategory&quot; class=&quot;text-danger&quot;>&lt;/span&gt;
</div>

它只选择了Id,所以我尝试通过从数据库中检索它来附加它。

var PostCategory = _context.ArticleCategories.Where(c =&gt; c.Id == BlogArticle.PostCategory.Id).FirstOrDefault();
if (PostCategory != null)
{
    BlogArticle.PostCategory = PostCategory;
}

if (!ModelState.IsValid)
{
    return Page();
}

不确定我哪里出错了,如果有建议或建议,将不胜感激。提前感谢。

英文:

I got a dropdown list that is populated from a database, It renders fine and the items are shown. The issue comes in when i try to save the model and the viewstate says its invalid for the postCategory Title and description as they are null but does have the Id value from the selection.

my db class is as follows.

 public class Article
{
 public long ArticleId { get; set; }
 [Required]
        [MaxLength(200)]
        public string Title { get; set; }
    public ArticleCategories PostCategory { get; set; } //this the problem
}

    public class ArticleCategories
    {
       
        public long Id { get; set; }
       
        [Required]
        [MaxLength(100)]
        public string Title { get; set; }
        [Required]
        [MaxLength(300)]
        public string Description { get; set; }
        public string Slug { get; set; }
        public List&lt;Article&gt; AssociatedPosts { get; set; }
    }

In my page model i load the dropdown list as follows.

        public ArticleCategories NewArticleCategory { get; set; }
        public List&lt;SelectListItem&gt; PostCategories
        {

            get
            {

                List&lt;SelectListItem&gt; NewList = new List&lt;SelectListItem&gt;();

                NewList = _context.ArticleCategories.Select(a =&gt;
                                      new SelectListItem
                                      {
                                          Value = a.Id.ToString(),
                                          Text = a.Title.ToString(),
                                      }).ToList();

                return NewList;

            }

        }

and on the page

                            &lt;div class=&quot;form-group&quot;&gt;
                    &lt;label asp-for=&quot;BlogArticle.PostCategory&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
                    &lt;select asp-for=&quot;BlogArticle.PostCategory.Id&quot; class=&quot;form-control&quot; asp-items=&quot;Model.PostCategories&quot;&gt;
                            &lt;option value=&quot;&quot;&gt;--Choose a Catergory--&lt;/option&gt;
                        &lt;/select&gt;

                        @Html.HiddenFor(m=&gt;m.BlogArticle.PostCategory.Title )
                        
                        @Html.HiddenFor(m=&gt;m.BlogArticle.PostCategory.Description )
                  
                    &lt;span asp-validation-for=&quot;BlogArticle.PostCategory&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;



                &lt;/div&gt;

It only select the Id so tried to attach it by retrieving it from the db.

            var PostCategory = _context.ArticleCategories.Where(c =&gt; c.Id == BlogArticle.PostCategory.Id).FirstOrDefault();
            if (PostCategory != null)
            {

                BlogArticle.PostCategory = PostCategory;
            }

            if (!ModelState.IsValid)
            {
                return Page();
            }

not sure where i am going wrong, if there any advice or suggestions it would be greatly apricated. thank you in advance.

答案1

得分: 1

根据您的代码,当您从视图向后端传递数据时,ArticleCategories 模型只会从选择中获得 Id 值,因为您没有向其输入标签传递任何 TitleDescription 的值,所以它们为 null。ModelState 只会验证从视图传递的模型。现在从视图传递的 ArticleCategories 模型只有 id 值,而您还将 TitleDescription 属性添加了 [Required] 标记,因此在 ModelState 中,Title 和 Description 将无效。

在您的代码中,我认为您希望 ModelState 验证其他属性,因此您需要从 ModelState 中移除 Title 和 Description 属性,请参考以下代码:

if (ModelState.Remove("BlogArticle.PostCategory.Title") && ModelState.Remove("BlogArticle.PostCategory.Description"))
{
     if(!ModelState.IsValid)
         return Page();
}

return Page();
英文:

From your code, When you pass data to backend from view, the ArticleCategories model will only have Id value from selection, the values of Title and Description are null because you do not pass any value to their input tag right? modelsate will only validate the model passed from the view. Now the ArticleCategories model passed from the view only has id value, you also add [Required] tag to Title and Description properties, So Title and Description will be invalid in modelsate.

In your code, I think you want ModelSate to validate other properties, So you need to remove Title and Description properties from ModelSate, Please refer to this code :

if (ModelState.Remove(&quot;BlogArticle.PostCategory.Title&quot;) &amp;&amp; ModelState.Remove(&quot;BlogArticle.PostCategory.Description&quot;))
{
     if(!ModelState.IsValid)
     return Page();
}

    return Page();

huangapple
  • 本文由 发表于 2023年2月16日 05:46:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75465729.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定