【问题标题】:Editor template for Nullables not working with MVCNullables 的编辑器模板不适用于 MVC
【发布时间】:2015-06-11 15:41:34
【问题描述】:

我所有的模板都适用于不可为空的值类型。但是,当值类型可以为空时,编辑器将改为使用字符串模板。

这是我的代码:

十进制.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; }
    }
}

缺少什么使得函数编辑器调用十进制模板而不是字符串模板?

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-4 razor


    【解决方案1】:

    这不是最令人满意的解决方案,但可能是最简单的:在模型属性上使用 UIHint 数据注释。例如:

    [UIHint("Decimal")]
    public Nullable<decimal> Weight { get; set; }
    

    【讨论】:

    • 感谢您的回答。但是,由于我有大量模型,我正在努力寻找更有效的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-03
    • 1970-01-01
    • 2012-08-07
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多