C# CS1061:不包含定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18501120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:24:24  来源:igfitidea点击:

CS1061: does not contain a definition for

c#asp.net-mvcdata-annotations

提问by AFetter

Why I get this error? Of course SelectIssuePriority doesn't exist on my first model. I have add it.

为什么我收到这个错误?当然 SelectIssuePriority 在我的第一个模型上不存在。我已经添加了。

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS1061: 'Devcore' does not contain a definition for 'SelectIssuePriority' and no extension method 'SelectIssuePriority' accepting a first argument of type 'Devcore.' could be found (are you missing a using directive or an assembly reference?)

Source Error:


Line 77: 
Line 78:         <div class="editor-label">
Line 79:             <%: Html.LabelFor(model => model.SelectIssuePriority) %>
Line 80:         </div>
Line 81:         <div class="editor-field">

Model

模型

namespace Devcore.Models
{
    [MetadataType(typeof(IssueMetaData))]
    public partial class Issue
    {

    }


    public class IssueMetaData 
    {
        [Required(ErrorMessage="Summary is required",AllowEmptyStrings = false)]
        public string Summary { get; set; }


        [Display(Name = "Priority")]
        [Required(ErrorMessage = "Priority is required", AllowEmptyStrings = false)]
        public string SelectIssuePriority { get; set; }
    }
 }

Aspx

ASPX

<div class="editor-label">
            <%: Html.LabelFor(model => model.SelectIssuePriority) %>
        </div>
        <div class="editor-field">
            <%: Html.DropDownList("SelectIssuePriority") %>
            <%: Html.ValidationMessageFor(model => model.SelectIssuePriority) %>
        </div>

采纳答案by Simon Whitehead

IIRC, those MetaDataextension classes are purely for validation. If your base model doesn't have those properties.. it won't work.

IIRC,那些MetaData扩展类纯粹是为了验证。如果您的基本模型没有这些属性.. 它将无法工作。

So you need this for the view to accept that properties exist:

所以你需要这个让视图接受属性存在:

[MetadataType(typeof(IssueMetaData))]
public partial class Issue
{
    public string SelectIssuePriority { get; set; }
}

And you need the MetaDataclass for the DataAnnotationsto work with model validation.

并且您需要使用MetaData该类DataAnnotations来进行模型验证。