c# - 如何在来自 View 的 View 值中显示标签

标签 c# asp.net-mvc asp.net-mvc-3 asp.net-mvc-4

我有一个标签,我需要在该标签中显示值,并且我已经在 Controller 中为该标签分配了值..

这是模型

 namespace MvcSampleApplication.Models
 {    
     public class labelsdisplay
     {
        public string labelvalue { get; set; }    
     }
 }

这是我的 Controller

namespace MvcSampleApplication.Controllers
{
    public class LbelDisplayController : Controller
    {               
        public ActionResult Index()
        {
            labelsdisplay lbldisx = new labelsdisplay();
            string name = "ABC";
            lbldisx.labelvalue = name;       
            return View(lbldisx);
        }    
    }
}

这是 View (强类型 View )

 @model MvcSampleApplication.Models.labelsdisplay
@{
    ViewBag.Title = "Index";
}    
<h2>Index</h2>
@using (@Html.BeginForm())
{     
    @Html.LabelFor(m=>m.labelvalue)    
}

我的问题是无法显示值(“ABC”),而不是它在 View 中的那个标签中显示“labelvalue”... 任何人都可以提出任何解决方案......关于这个......

非常感谢..

最佳答案

要仅显示值,您可以使用

@Html.DisplayNameFor(m=>m.labelvalue)

或者如果你想显示带有值的标签元素,你可以使用

@Html.LabelFor(m=>m.labelvalue, Model.labelvalue)  

第一个参数是名称的值,第二个参数是标签的值。

关于c# - 如何在来自 View 的 View 值中显示标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17985090/

相关文章:

c# - 工程转C#时ApplicationEvents.vb的函数写到哪里

asp.net-mvc - 有没有一种简单的方法来查看 FormCollection 的内容?

jquery - ajax 请求在 kiosk 浏览器上缓存

c# - 具有.Net 客户端和Android 客户端的WCF 服务器策略?

c# - .NET 程序集未从用户控件中找到错误

c# - 泛型的观察者模式

asp.net-mvc - 使用 jQuery getJson 发送列表/数组作为参数

asp.net-mvc - 在 MVC 5 应用程序中使用 autofac 注入(inject) SignalR Hub 的依赖项

asp.net - MVC3 应用程序忽略 Razor .cshtml 扩展名

asp.net-mvc - MVC中 Controller 如何知道DeleteConfirmed上的Id?