Asp.net mvc MVC局部视图[显示(名称=”)]无需DisplayFor或EditorFor即可使用
我试图在主索引视图中显示部分视图中的值,但很难理解为什么不尊重Asp.net mvc MVC局部视图[显示(名称=”)]无需DisplayFor或EditorFor即可使用,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,我试图在主索引视图中显示部分视图中的值,但很难理解为什么不尊重[display(Name=”“)]属性。控制器有一个类,其中两个属性设置了[Display(Name=”“)]属性,但未显示 如果我不能在我正在做的事情中使用这些属性,那么我很好,只要理解一下为什么没有@Html.DisplayFor或@Html.EditorFor之类的东西是不可能的 也有一个很好的机会,我可能有一些错误的代码,这是原因 控制器 [OutputCache(Duration = 0)] public
[display(Name=”“)]
属性。控制器有一个类,其中两个属性设置了[Display(Name=”“)]
属性,但未显示
如果我不能在我正在做的事情中使用这些属性,那么我很好,只要理解一下为什么没有@Html.DisplayFor
或@Html.EditorFor
之类的东西是不可能的
也有一个很好的机会,我可能有一些错误的代码,这是原因
控制器
[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; }
}
}
[OutputCache(持续时间=0)]
公共JsonResult OrderPreview(int id)
{
if(ModelState.IsValid)
{
尝试
{
//注释代码
尝试
{
字节[]数据=image.Save(图像);
//用字典测试有效,但可能不理想???
//字典属性=新字典();
//属性。添加(“作业”,订单。作业);
//添加(“订单Id”,Id);
//添加(“客户数量”、订单、客户数量);
//属性。添加(“高度”,设计。高度);
//属性。添加(“宽度”,设计。宽度);
var orderPreviewResult=new orderPreviewResult.OrderPreview()
{
作业=订单。作业,
OrderId=id,
CustomerCount=order.CustomerCount,
高度=设计高度,
宽度=设计宽度
};
var attributeList=新列表();
attributeList.Add(orderPreviewResult);
返回Json(新的OrderPreviewResult()
{
//属性=属性,
OrderPreviewAttributes=属性列表,
PngBase64=Convert.tobase64字符串(数据)
},JsonRequestBehavior.AllowGet);
//注释代码
}
捕获(例外e)
{
AddModelError(“,e.Message”);
}
}
var result=new OrderPreviewResult();
result.Errors=新列表();
//将错误添加到结果中
foreach(ModelState中的var值)
{
foreach(value.value.Errors中的var错误)
{
result.Errors.Add(error.ErrorMessage);
}
}
返回Json(结果,JsonRequestBehavior.AllowGet);
}
}
公共类OrderPreviewResult
{
公共字符串PngBase64{get;set;}
公共列表错误{get;set;}
//公共字典属性{get;set;}
公共列表OrderPreviewAttributes{get;set;}
公共类订单预览
{
[显示(Name=“订单Id”)]
公共int-OrderId{get;set;}
公共字符串作业{get;set;}
[显示(Name=“客户计数”)]
公共uint客户计数{get;set;}
公共单元高度{get;set;}
公共uint宽度{get;set;}
}
}
Index.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")
@{
ViewBag.Title=“订单”;
}
@第css节
{
}
@节脚本
{
}
命令
$(文档).ready(函数(){
//注释代码
});
函数OnShowPreview(){
$(“#加载指示器”).show();
$(“#预览图像”).hide();
$(“#PreviewErrorText”).hide();
$(“#PreviewAttributeText”).hide();
var id=$('.modal body#orderId').val();
$.ajax({
类型:“POST”,
url:'@url.Action(“OrderPreview”)/'+id,
数据:id,
成功:功能(数据){
$(“#加载指示器”).hide();
if(data.Errors!=null){
var errorList=$(“#PreviewErrorText ul”);
errorList.html(“”);
$(data.Errors)。每个(函数(i,项){
errorList.append(“”+项+” );
});
$(“#PreviewErrorText”).show();
}否则{
$(“#PreviewImage”).attr('src','data:image/png;base64',+data.PngBase64);
$(“#预览图像”).show();
if(/*data.Attributes!=null*/data.OrderPreviewAttributes!=null){
//var属性=data.attributes;
var attributes=data.OrderPreviewAttributes[0];
var attributeList=$(“#PreviewAttributeText”);
attributeList.html(“”);
attributeList.append('Attributes:');
变量表=$(“”);
for(变量键入属性){
如果(属性[键]!=null){
if(attributes.hasOwnProperty(key)){//这将检查键是否由数据对象而不是它的任何祖先拥有
table.append(“”+key+”:“”+attributes[key]+“”);
}
}
}
表.附录(属性列表);
$(“#预览论坛测试文本”).show();
}
}
}
});
}
函数生成器预览(e){
//防止发生默认提交
如果(e.u
<div class="modal hide fade" id="OrderPreviewModal" tabindex="-1" role="dialog">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 id="OrderPreviewModalLabel">Order Preview</h3>
</div>
<div class="modal-body">
<div>
<input type="hidden" name="orderId" id="orderId" value="" />
<div id="LoadingIndicator" style="display: table-cell; vertical-align: middle">
<img alt="Loading..." src="@Url.Content("~/Content/ajax-loader.gif")" style="display:block; margin-left: auto; margin-right: auto" />
</div>
<img id="PreviewImage" alt="Preview" src="" style="display: block; margin-left: auto; margin-right: auto" />
<div id="PreviewAttributesText">
</div>
<div id="PreviewErrorText" style="color: red">
<p>Errors occurred:</p>
<ul />
</div>
</div>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal">Close</button>
</div>
</div>
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)