我的所有模板都适用于非可空值类型。当值类型可以为空时,编辑器将使用模板作为字符串。
这是我的代码:
Decimal.cshtml
@model Nullable<decimal>
@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue,
new { @class = "form-control validateFloat", placeholder = ViewData.ModelMetadata.Watermark })
Object.cshtml
@foreach (var prop in ViewData.ModelMetadata.Properties
.Where(p => p.ShowForEdit)) {
if ((prop.AdditionalValues.ContainsKey("DoNotAutoGenerate")) ||
(string.IsNullOrEmpty(prop.DataTypeName) && prop.Model is IEnumerable<object>))
{
// Do not generate
}
else if (prop.TemplateHint == "HiddenInput")
{
@Html.Hidden(prop.PropertyName)
}
else
{
<div class="form-group">
@Html.LabelLocalized(prop, ViewData.ModelMetadata, mgr)
<div class="col-md-10">
@if(ViewData.ModelMetadata.IsPrimaryKey(prop))
{
@Html.Editor(prop.PropertyName, "ReadOnly")
}else
{
@Html.Editor(prop.PropertyName, prop.DataTypeName) <-- This line calls String.cshtml instead of Decimal.cshtml when the data type is Nullable<decimal>. When the data type is decimal, the right template (Decimal.cshtml) is called.
@Html.ValidationMessage(prop.PropertyName)
}
</div>
</div>
}
}
ProductDefinitionViewModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Infrastructure.Attributes;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using ERPInterface;
using System.Web.Mvc;
using InventoryInterface;
namespace WebUI.ViewModels
{
public class ProductDefinitionViewModel
{
//public ProductDefinitionViewModel()
//{
// AttachedProductDefinitions = new List<AttachedProductDefinition>();
// ReplacementProductDefinitions = new List<ReplacementProductDefinition>();
//}
[DataType("ReadOnly")]
public int ProductDefinitionId { get; set; }
public bool IsActive { get; set; }
public Translation NameTranslation { get; set; }
public string InternalId { get; set; }
public string ExternalId { get; set; }
[DataType("UnitOfMeasurementId")]
public int UnitOfMeasurementId { get; set; }
[DataType("Taxes")]
public List<InventoryInterface.ProductDefinitionTax> ProductDefinitionTaxes { get; set; }
[AdditionalMetadata("DoNotAutoGenerate", true)]
public int NameTranslationId { get; set; }
public ICollection<InventoryInterface.ProductDefinitionCode> ProductDefinitionCodes { get; set; }
[DataType("ProductDefinitionNatures")]
public Nullable<int> ProductDefinitionNatureId { get; set; }
public bool FixedWeight { get; set; }
public Nullable<decimal> Weight { get; set; } <-- Calls String template instead of Decimal template
public Nullable<decimal> MinWeight { get; set; }
public Nullable<decimal> MaxWeight { get; set; }
public Nullable<decimal> PackagingWeight { get; set; }
[DataType("Categories")]
public Nullable<int> CategoryId { get; set; }
[DataType("ProductDefinitionGroups")]
public Nullable<int> ProductDefinitionGroupId { get; set; }
public List<AttachedProductDefinition> AttachedProductDefinitions { get; set; }
public List<ReplacementProductDefinition> ReplacementProductDefinitions { get; set; }
}
}
缺少什么,以便函数编辑器调用十进制模板而不是字符串模板?
答案 0 :(得分:0)
它不是最令人满意的解决方案,但可能最简单:在模型属性上使用UIHint
数据注释。 e.g:
[UIHint("Decimal")]
public Nullable<decimal> Weight { get; set; }