Asp.net mvc ASP MVC“;元数据类型和所需的“;习惯';行不通

Asp.net mvc ASP MVC“;元数据类型和所需的“;习惯';行不通,asp.net-mvc,expression,metadata,Asp.net Mvc,Expression,Metadata,我正在使用ADO.NET创建某种网格视图,没有实体框架。我不明白为什么元数据+Required和其他表达式不起作用。这是我的示例代码: 业务对象模型类: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel.DataAnnotations; namespace ObslugaStonyLibrary { [Meta

我正在使用ADO.NET创建某种网格视图,没有实体框架。我不明白为什么元数据+Required和其他表达式不起作用。这是我的示例代码:

业务对象模型类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;

namespace ObslugaStonyLibrary
{
    [MetadataType(typeof(zmienneWczytywanieTresci))]
    public partial class zmienneWczytywanieTresciMeta
    {
        public class zmienneWczytywanieTresci
        {
            public int ID { get; set; }
            public int druzynaID { get; set; }
            public string Druzyna { get; set; }
            [Required]
            public int LiczbaMeczy { get; set; }
            public string LiczbaGoliStrzelonych { get; set; }
            public string LiczbaGoliStraconych { get; set; }
        }
     }
}
应用模型:

namespace Start.Models
{
    public class InformacjeModel
    {
        public IPagedList<zmienneWczytywanieTresciMeta.zmienneWczytywanieTresci> tabela { get; set; }
    }
}
和视图:

@foreach (var news in Model.tabela)
    {   
        using (Html.BeginForm())
        {
            <div class="ramkaTRgridView">
                @Html.HiddenFor(model => news.ID, "ID");
                <button type="submit" class="floatDivGridView">Submit</button>
                <div class="ramkaTDgridViewID floatDivGridView">
                    <span>@Html.Label(news.druzynaID.ToString())</span>
                </div>
                <div class="ramkaTDgridViewDruzyna floatDivGridView">
                    <span>@Html.Label(news.Druzyna.ToString())</span>
                </div>
                <div class="ramkaTDgridViewStatystyki floatDivGridView">
                    @Html.EditorFor(model => news.LiczbaMeczy, "LiczbaMeczy", "LiczbaMeczy");
                    @Html.ValidationMessageFor(model => news.LiczbaMeczy)
                </div>
            </div>
        }
    }
@foreach(Model.tabela中的var新闻)
{   
使用(Html.BeginForm())
{
@HiddenFor(model=>news.ID,“ID”);
提交
@Label(news.druzynaID.ToString())
@Label(news.Druzyna.ToString())
@EditorFor(model=>news.LiczbaMeczy,“LiczbaMeczy”,“LiczbaMeczy”);
@Html.ValidationMessageFor(model=>news.LiczbaMeczy)
}
}

除了验证之外,其他一切都很好。你能帮我吗?

这不是它的工作方式。您通常会这样做(注意,您将MetadateType属性放在您想要拥有元数据的类上,而不是放在元数据类本身上)

有些人喜欢将元数据类嵌套在扩展部分中,因为您根本不需要自己引用元数据类

[MetadataType(typeof(A.AMeta))]
public partial class A
{
    public class AMeta
    {
        [Required]
        public int ID {get;set;}
    }    
}

如果你不使用EF,为什么你需要元数据类呢?我在后面有一个教程,里面有元数据。但在那之后,我以自己的方式完成了这项工作,没有元数据和任何教程,现在工作正常;)。
public partial class A
{
    public int ID { get; set; }
}

[MetadataType(typeof(AMeta))]
public partial class A
{
}

public class AMeta
{
    [Required]
    public int ID {get;set;}
}
[MetadataType(typeof(A.AMeta))]
public partial class A
{
    public class AMeta
    {
        [Required]
        public int ID {get;set;}
    }    
}