我正在尝试在主索引视图中显示部分视图中的值,但很难理解为什么[Display(Name = "")]
属性不被尊重。控制器有一个类,其中两个属性设置了[Display(Name = "")]
属性但没有显示。
如果我能用我正在做的事情来利用这些属性,只要理解为什么没有像{{1}这样的事情就无法做到这一点,我就没事了。 }或@Html.DisplayFor
会有所帮助。
我也很可能在代码中出错了。这就是原因。
控制器
@Html.EditorFor
Index.cshtml查看
[OutputCache(Duration = 0)]
public JsonResult OrderPreview(int id)
{
if (ModelState.IsValid)
{
try
{
// Commented code
try
{
byte[] data = image.Save(image);
// Testing with a Dictionary works but maybe not ideal???
//Dictionary<string,object> attributes = new Dictionary<string, object>();
//attributes.Add("Job", order.Job);
//attributes.Add("Order Id", id);
//attributes.Add("Customer Count", order.CustomerCount);
//attributes.Add("Height", design.Height);
//attributes.Add("Width", design.Width);
var orderPreviewResult = new OrderPreviewResult.OrderPreview()
{
Job = order.Job,
OrderId = id,
CustomerCount = order.CustomerCount,
Height = design.Height,
Width = design.Width
};
var attributeList = new List<OrderPreviewResult.OrderPreview>();
attributeList.Add(orderPreviewResult);
return Json(new OrderPreviewResult()
{
//Attributes = attributes,
OrderPreviewAttributes = attributeList,
PngBase64 = Convert.ToBase64String(data)
}, JsonRequestBehavior.AllowGet);
// Commented code
}
catch (Exception e)
{
ModelState.AddModelError("", e.Message);
}
}
var result = new OrderPreviewResult();
result.Errors = new List<string>();
// Add the errors to the result
foreach (var value in ModelState)
{
foreach (var error in value.Value.Errors)
{
result.Errors.Add(error.ErrorMessage);
}
}
return Json(result, JsonRequestBehavior.AllowGet);
}
}
public class OrderPreviewResult
{
public string PngBase64 { get; set; }
public List<string> Errors { get; set; }
//public Dictionary<string, object> Attributes { get; set; }
public List<OrderPreview> OrderPreviewAttributes { get; set; }
public class OrderPreview
{
[Display(Name = "Order Id")]
public int OrderId { get; set; }
public string Job { get; set; }
[Display(Name = "Customer Count")]
public uint CustomerCount { get; set; }
public uint Height { get; set; }
public uint Width { get; set; }
}
}
_OrderPreview.cshtml部分视图
@{
ViewBag.Title = "Orders";
}
@section css
{
<link href="@Url.Content("~/Content/DataTables-1.8.2/css/DT_bootstrap.css")" rel="stylesheet" type="text/css" />
}
@section scripts
{
<script src="@Url.Content("~/Content/Datatables-1.8.2/js/jquery.dataTables.min.js")" type="text/javascript"></script>
}
<div class="page-header">
<h1>Orders</h1>
</div>
<script type="text/javascript">
$(document).ready(function () {
// Commented code
});
function OnShowPreview() {
$("#LoadingIndicator").show();
$("#PreviewImage").hide();
$("#PreviewErrorText").hide();
$("#PreviewAttributesText").hide();
var id = $('.modal-body #orderId').val();
$.ajax({
type: "POST",
url: '@Url.Action("OrderPreview")/' + id,
data: id,
success: function (data) {
$("#LoadingIndicator").hide();
if (data.Errors != null) {
var errorList = $("#PreviewErrorText ul");
errorList.html('');
$(data.Errors).each(function (i, item) {
errorList.append('<li>' + item + '</li>');
});
$("#PreviewErrorText").show();
} else {
$("#PreviewImage").attr('src', 'data:image/png;base64,' + data.PngBase64);
$("#PreviewImage").show();
if (/* data.Attributes != null */ data.OrderPreviewAttributes != null) {
//var attributes = data.Attributes;
var attributes = data.OrderPreviewAttributes[0];
var attributeList = $("#PreviewAttributesText");
attributeList.html('');
attributeList.append('<h4>Attributes:</h4>');
var table = $("<table class='table table-condensed table-striped'>");
for (var key in attributes) {
if (attributes[key] != null) {
if (attributes.hasOwnProperty(key)) { // this will check if key is owned by data object and not by any of it's ancestors
table.append('<tr><th scope="row">' + key + ':</th><td>' + attributes[key] + '</td></tr>');
}
}
}
table.appendTo(attributeList);
$("#PreviewAttributesText").show();
}
}
}
});
}
function GeneratePreview(e) {
// Prevent the default submit from occurring
if (e.preventDefault)
e.preventDefault();
else
//fix for IE
e.returnValue = false;
$('.modal-body #orderId').val(e.srcElement.id);
// Show the dialog
$('#OrderPreviewModal').modal('show');
}
</script>
@Html.Partial("_OrderPreview")
答案 0 :(得分:3)
有时您需要Model属性的DisplayName,而您不需要/需要使用@Html.DisplayFor()
因此,您可以使HtmlHelper只检索属性的DisplayName。
public static MvcHtmlString GetDisplayName<TModel, TProperty>( this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression )
{
var metaData = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData);
string value = metaData.DisplayName ?? (metaData.PropertyName ?? ExpressionHelper.GetExpressionText(expression));
return MvcHtmlString.Create(value);
}
在您看来,只需使用:
@Html.GetDisplayName(x => x.YourProperty)