如何改变[显示名称" XXX"在控制器? [英] How to change [DisplayName"xxx"] in Controller?

查看:154
本文介绍了如何改变[显示名称" XXX"在控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,

我MVC 2的新手,停留在此问题:

AccountModuls.cs

 公共类LogOnModel
{
[需要]
[显示名称(用户名)]
公共字符串用户名{获得;组; }
...
}

LogOn.aspx

 <%:Html.LabelFor(M = GT; m.UserName)%GT;

文本用户名最终将显示在网站 - 根据我的定义

[显示名称(用户名)。

没有问题。

但我怎么能在AccountController.cs改变这种文字?

 公众的ActionResult登录()
{
返回查看();
}


解决方案

您不能:)你必须改变类上的显示名称属性,以使.LabelFor帮手构建标签。如果你希望它是别的东西,你当然可以只写出来的标签自己的HTML。

不知道为什么你想显示名称从页面切换到页面虽然?我是
误解的东西?

编辑:

自定义displayName属性:

 公共类MyDisplayName:DisplayNameAttribute
{
    公众诠释DBID {搞定;组; }    公共MyDisplayName(INT DBID)
    {
        this.DbId = DBID;
    }
    公众覆盖字符串显示名称
    {
        得到
        {
            //做一些DB-查询检索名称
            返回从DBLookup一些字符串;
        }
    }
}公共类TestModel
{
    [MyDisplayName(2)]
    公共字符串myTextField将获得{;组; }
}

Folks,

I am MVC 2 newbie and stuck on this problem:

AccountModuls.cs

public class LogOnModel
{
[Required]
[DisplayName("User name")]
public string UserName { get; set; }
…
}

LogOn.aspx

<%: Html.LabelFor(m => m.UserName) %>

The text "User name" will be finally displayed in the website - based on my definition

[DisplayName("User name")].

No Problem.

But how can I change this text in AccountController.cs?

public ActionResult LogOn()
{   
return View();
}

解决方案

You can't :) You have to change the DisplayName-attribute on the class in order for the .LabelFor helper to construct the label. You could of course just write out the HTML for the Label yourself if you want it to be something else.

Don't see why you would want to change the Displayname from page to page though? Am I misunderstanding something?

Edit:

Custom displayname attribute:

public class MyDisplayName : DisplayNameAttribute
{
    public int DbId { get; set; }

    public MyDisplayName(int DbId)
    {
        this.DbId = DbId;
    }


    public override string DisplayName
    {
        get
        {
            // Do some db-lookup to retrieve the name
            return "Some string from DBLookup";
        }
    }
}

public class TestModel
{
    [MyDisplayName(2)]
    public string MyTextField { get; set; }
}

这篇关于如何改变[显示名称&QUOT; XXX&QUOT;在控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆