使用FluentAPI时如何控制DisplayNameFor结果(How to control DisplayNameFor result, when using FluentAPI)

系统教程 行业动态 更新时间:2024-06-14 16:57:18
使用FluentAPI时如何控制DisplayNameFor结果(How to control DisplayNameFor result, when using FluentAPI)

使用ASP.NET MVC 5应用程序。 使用Fluent API定义模型映射。 我想更改列名称外观,以便如果我执行DisplayFor它会正确显示。 例如,“Action Name”,而不是“ActionName”。

目前,我流畅的api映射是......

this.Property(t => t.ActionName).HasColumnName("ActionName");

我想在Action和Name之间放置一个空格,以便当列显示时,表列标题将显示“Action Name”,而不是“ActionName”。 所以,我认为这会起作用....

this.Property(t => t.ActionName).HasColumnName("Action Name");

但是,这会导致错误......

{"Invalid column name 'Audit Name'."}

在我的HTML中,它设置为...

@Html.DisplayNameFor(model => model.ActionName)

我以为DisplayNameFor会使用.HasColumnName设置,但显然不是。

我可以直接输入HTML,但我正在尝试“正确”地进行操作,并了解其工作原理。

有关如何正确执行此操作的任何建议,以便DisplayNameFor产生“操作名称”?

谢谢!

Working on an ASP.NET MVC 5 app. Using Fluent API to define model maps. I want to change the column name appearance so that if I do a DisplayFor it appears properly. For example, "Action Name", rather than "ActionName".

Currently, my fluent api mapping is....

this.Property(t => t.ActionName).HasColumnName("ActionName");

I want to put a space between Action and Name so that when the column shows up the table column header will say "Action Name", not "ActionName". So, I thought that this would work....

this.Property(t => t.ActionName).HasColumnName("Action Name");

But, this results in an error...

{"Invalid column name 'Audit Name'."}

In my HTML it's setup as...

@Html.DisplayNameFor(model => model.ActionName)

I was thinking that DisplayNameFor would use the .HasColumnName setting, but apparently not.

I could just type in the HTML directly, but I'm trying to do it "correctly" and understand how this works.

Any suggestions on how to properly do this, so that DisplayNameFor results in "Action Name"?

Thanks!

最满意答案

我可以直接输入HTML,但我正在尝试“正确”地进行操作,并了解其工作原理。

“正确”做事意味着不使用实体类作为ViewModels - 它们用于不同的目的。

例如,为什么将实体用作ViewModel是一个坏主意的一个很好的例子是用户帐户管理页面,因为您需要两个string密码输入“新密码”和“确认密码”,但您的实体类将只有密码哈希/摘要的单个byte[] (以及salt值, 我希望 ) - 所以通过这个例子我希望你明白为什么你不应该将User实体用作你网络中UserEdit页面的ViewModel -应用。

我不知道您的应用程序做了什么,并且您还没有发布数据库设计,只是为ViewModel创建了一个不同的类型:

class SomePageViewModel { [DisplayName("Action name")] public String ActionName { get; set; } }

只需在返回视图时将实体对象中的值复制到viewmodel中:

public ActionResult ControllerAction() { // ... SomePageViewModel viewModel = new SomePageViewModel(); viewModel.ActionName = entityObject.ActionName; return this.View( viewModel ); }

I could just type in the HTML directly, but I'm trying to do it "correctly" and understand how this works.

Doing things "correctly" means not using Entity classes as ViewModels - they serve different purposes.

A great example of why it's a bad idea to use Entities as ViewModels is a User Account management page, for example, because you will want two string password inputs for "New Password" and "Confirm password", but your Entity class will only have a single byte[] for the password hash/digest (and a salt value too, I hope) - so by way of this example I hope you see why you shouldn't use User entity as a ViewModel for the UserEdit page in your web-application.

I don't know what your application does, and you haven't posted a database design, but just create a different type for your ViewModel:

class SomePageViewModel { [DisplayName("Action name")] public String ActionName { get; set; } }

And simply copy the value from your entity object into the viewmodel when you return the view:

public ActionResult ControllerAction() { // ... SomePageViewModel viewModel = new SomePageViewModel(); viewModel.ActionName = entityObject.ActionName; return this.View( viewModel ); }

更多推荐

本文发布于:2023-04-12 20:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/473c6d5466ae2ec8506dfb1b4cc9b678.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:DisplayNameFor   FluentAPI   result   control

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!