使用逗号的Asp.Net表单输入

时间:2017-04-14 20:06:51

标签: c# asp.net forms asp.net-core asp.net-core-mvc

我有一个表单,其中包含一个收集金额的输入(例如:<div class="container-fluid"> <div class="menu-container"> <ul class="menu" id="sidemenu"> <li><a><i class="fa fa-calendar-plus-o"></i><br />Dashboard</a></li> <li><a><i class="fa fa-calendar-plus-o"></i><br />Dashboard</a></li> <li><a><i class="fa fa-calendar-plus-o"></i><br />Dashboard</a></li> <li><a><i class="fa fa-calendar-plus-o"></i><br />Dashboard</a></li> <li><a><i class="fa fa-calendar-plus-o"></i><br />Dashboard</a></li> <li><a><i class="fa fa-calendar-plus-o"></i><br />Dashboard</a></li> <li><a><i class="fa fa-calendar-plus-o"></i><br />Dashboard</a></li> <li><a><i class="fa fa-calendar-plus-o"></i><br />Dashboard</a></li> <li><a><i class="fa fa-calendar-plus-o"></i><br />Dashboard</a></li> <li><a><i class="fa fa-calendar-plus-o"></i><br />Dashboard</a></li> <li><a><i class="fa fa-calendar-plus-o"></i><br />Dashboard</a></li> <li><a><i class="fa fa-calendar-plus-o"></i><br />Dashboard</a></li> <li><a><i class="fa fa-calendar-plus-o"></i><br />Dashboard</a></li> <li><a><i class="fa fa-calendar-plus-o"></i><br />Dashboard</a></li> <li><a><i class="fa fa-calendar-plus-o"></i><br />Dashboard</a></li> </ul> </div> <div class="content-container"> <div class="header"> <div class="row"> <div class="col-sm-6"> left header </div> <div class="col-sm-6 text-right"> right header </div> </div> </div> <div class="page-content"> <div class="row"> <div class="col-sm-12"> this is a row #1 </div> </div> <div class="row"> <div class="col-sm-12"> this is a row #2 </div> </div> </div> </div> </div>

如何允许输入$5,000不会触发$5,000删除逗号((!ModelState.IsValid))和,进行投注?

视图模型

$

查看

public class FormViewModel
{
    [Required, RegularExpression("[0-9,]")]
    public int? Amount { get; set; }
}

控制器

<form>
  <input asp-for="Amount" />
  <span asp-validation-for="Amount"></span>
  <button type="submit">Submit</button>
</form>

4 个答案:

答案 0 :(得分:0)

我将视图模型更改为接受int?属性的字符串而不是Amount

public class FormViewModel {
    public string Amount { get; set; }

    public int? Value {
        get {
            int result = 0;
            if (Int32.TryParse(Amount, NumberStyles.Currency, CultureInfo.CurrentCulture, result) {
                return result;
            }
            return null;
        }
    }
}

现在用户输入存储在Amount属性中,而解析后的整数值存储在Value属性中。

答案 1 :(得分:0)

您是否尝试过IModelBinder?

public class DecimalModelBinder: System.Web.Mvc.IModelBinder
{
    public object BindModel(ControllerContext controllerContext, 
        ModelBindingContext bindingContext)
    {
        ValueProviderResult valueResult = bindingContext.ValueProvider
            .GetValue(bindingContext.ModelName);
        ModelState modelState = new ModelState { Value = valueResult };
        object actualValue = null;
        if (valueResult.AttemptedValue != string.Empty)
        {
            try
            {
                string attemptedVal = valueResult.AttemptedValue?.Replace(",", ".").Replace("$", "");
                actualValue = Convert.ToDecimal(attemptedVal, new CultureInfo("en-US"));
            }
            catch (FormatException e)
            {
                modelState.Errors.Add(e);
            }
        }
        bindingContext.ModelState.Add(bindingContext.ModelName, modelState);
        return actualValue;
    }
}

error with decimal in mvc3 - the value is not valid for field

的答案类似,但答案相同

答案 2 :(得分:0)

更新模型以使用数据类型注释。

public class FormViewModel {
    [DataType(DataType.Currency)]
    [Required]
    public int? Amount { get; set; }
}

如果您想包含美分,则将属性更改为十进制

public class FormViewModel {
    [DataType(DataType.Currency)]
    [Required]
    public decimal? Amount { get; set; }
}

答案 3 :(得分:0)

我最终使用AutoMapper来回映射属性。

我将ViewModel更改为String,然后使用下面的automapper:

//Map From Int To String
CreateMap<Models.ServiceModel, FormViewModel>().ForMember(dest => dest.Amount, opt => opt.MapFrom(src => src.Amount.ToString()));


//Map From String Back To Int
CreateMap<FormViewModel, Models.ServiceModel>().ForMember(dest => dest.Amount, opt => opt.MapFrom(src => int.Parse(src.Amount.Replace(",", ""))));