ASP .NET MVC + LINQ 生成的类 +数据注释

发布于 2024-10-12 06:11:29 字数 2890 浏览 4 评论 0原文

我有一个问题,我希望你能帮助我解决。好吧,我会尝试描述我的情况:

我已经创建了数据库。 添加表“订单”。 然后在 dbml 文件中生成 LINQ 类。

现在,我已在控制器中创建操作,并且我正在尝试为一个或我的订单类属性(客户端名称)设置 [DisplayName("Other name")]。所以,看看这个:

/Core/Extansions.cs

namespace SUPNew.Core
{
    public static class Extansions
    {

    }

    [MetadataType(typeof(OrderMetaData))]
    public partial class Order
    {
    }

    public class OrderMetaData
    {
        [DisplayName("ФИО клиента")]
        public string ClientFIO { get; set; }
    }
}

/Views/Manager/AddOrder.aspx

    <legend>Fields</legend>

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

/Controllers/ManagerController.cs

using SUPNew.Core;

/ Models / SUPNew.dbml

    public partial class Order : INotifyPropertyChanging, INotifyPropertyChanged
    {
...
        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ClientFIO", DbType="NVarChar(50) NOT NULL", CanBeNull=false)]
        public string ClientFIO
        {
            get
            {
                return this._ClientFIO;
            }
            set
            {
                if ((this._ClientFIO != value))
                {
                    this.OnClientFIOChanging(value);
                    this.SendPropertyChanging();
                    this._ClientFIO = value;
                    this.SendPropertyChanged("ClientFIO");
                    this.OnClientFIOChanged();
                }
            }
        }

那么,也许我忘了做点什么?我需要在部分类中选择的 LabelFor(model => model.ClientFIO) 中显示 ClientFIO 的 DisplayName。

谢谢帮助。


如果我更改 .dbml 文件 - 像这样添加 [DisplayName("OtherName")] :

[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ClientFIO", DbType="NVarChar(50) NOT NULL", CanBeNull=false)]
[DisplayName("Other name")]            
public string ClientFIO
            {
                get
                {
                    return this._ClientFIO;
                }
                set
                {
                    if ((this._ClientFIO != value))
                    {
                        this.OnClientFIOChanging(value);
                        this.SendPropertyChanging();
                        this._ClientFIO = value;
                        this.SendPropertyChanged("ClientFIO");
                        this.OnClientFIOChanged();
                    }
                }
        }

它可以工作,但是当我尝试创建部分类时 - 则不行。有人可以帮我处理 LINQ 生成类的部分类吗?

I have a problem, and i hope, u can help me with it. Ok, i'll try to describe my situation:

I've created database.
Added table "Orders".
Then generated LINQ Classes in dbml file.

Now, i have create action in controller, and i'm trying to set [DisplayName("Other name")] for one or my Order class property (Client name). So, look at this:

/ Core / Extansions.cs

namespace SUPNew.Core
{
    public static class Extansions
    {

    }

    [MetadataType(typeof(OrderMetaData))]
    public partial class Order
    {
    }

    public class OrderMetaData
    {
        [DisplayName("ФИО клиента")]
        public string ClientFIO { get; set; }
    }
}

/ Views / Manager / AddOrder.aspx

    <legend>Fields</legend>

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

/ Controllers / ManagerController.cs

using SUPNew.Core;

/ Models / SUPNew.dbml

    public partial class Order : INotifyPropertyChanging, INotifyPropertyChanged
    {
...
        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ClientFIO", DbType="NVarChar(50) NOT NULL", CanBeNull=false)]
        public string ClientFIO
        {
            get
            {
                return this._ClientFIO;
            }
            set
            {
                if ((this._ClientFIO != value))
                {
                    this.OnClientFIOChanging(value);
                    this.SendPropertyChanging();
                    this._ClientFIO = value;
                    this.SendPropertyChanged("ClientFIO");
                    this.OnClientFIOChanged();
                }
            }
        }

So, maybe i forgot to make something? I need to display DisplayName for ClientFIO in LabelFor(model => model.ClientFIO) that i've choosen in partial class.

Thx for help.


If i change .dbml file- adding [DisplayName("OtherName")] like this:

[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ClientFIO", DbType="NVarChar(50) NOT NULL", CanBeNull=false)]
[DisplayName("Other name")]            
public string ClientFIO
            {
                get
                {
                    return this._ClientFIO;
                }
                set
                {
                    if ((this._ClientFIO != value))
                    {
                        this.OnClientFIOChanging(value);
                        this.SendPropertyChanging();
                        this._ClientFIO = value;
                        this.SendPropertyChanged("ClientFIO");
                        this.OnClientFIOChanged();
                    }
                }
        }

it works, but when i try to make partial class- isn't. Can somebody help me with partial class for LINQ generated class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小糖芽 2024-10-19 06:11:29

好吧,之前是这样的,它不起作用:

/Core/Extansions.cs (命名空间 SUPNew.Core) (公共部分类 Order)
/Models/SUPNew.dbml (命名空间 SUPNew.Models)(公共部分类 Order)(生成)

然后,当我像这样替换时:

/Models/Extansions.cs (命名空间 SUPNew.Models)(公共部分类 Order)
/Models/SUPNew.dbml (命名空间 SUPNew.Models)(公共部分类 Order)(生成)

它开始工作。但是,问题出在哪里呢?我在各处使用了 all using 并在 View.aspx 中导入。

我需要使其像第一个示例一样工作:

/Core/Extansions.cs (命名空间 SUPNew.Core)(公共部分类 Order)
/Models/SUPNew.dbml (命名空间 SUPNew.Models)(公共部分类 Order)(生成)

Ok, before when it was like this, it wasn't work:

/Core/Extansions.cs (Namespace SUPNew.Core) (public partial class Order)
/Models/SUPNew.dbml (Namespace SUPNew.Models) (public partial class Order)(generated)

Then, when i replace like this:

/Models/Extansions.cs (Namespace SUPNew.Models) (public partial class Order)
/Models/SUPNew.dbml (Namespace SUPNew.Models) (public partial class Order)(generated)

it started to work. But, where is the problem? I used all using in everywhere and Import in View.aspx.

I need to make it work like in first example:

/Core/Extansions.cs (Namespace SUPNew.Core) (public partial class Order)
/Models/SUPNew.dbml (Namespace SUPNew.Models) (public partial class Order)(generated)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文