我有一个标签,我需要在该标签中显示值,并且我已经在 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/