2015-08-30 26 views
0

我有以下视图模型:为什么我在创建视图中出现字符串格式错误?

public class BetViewModel 
{ 
    [HiddenInput(DisplayValue = false)] 
    public string OwnerId { get; set; } 

    [Required] 
    [Display(Name = "Bet Name")] 
    [StringLength(100, ErrorMessage = "'{0}' must be {2) characters or less.")] 
    public string BetName { get; set; } 

    [Required] 
    [Range(1, Int32.MaxValue, ErrorMessage = "'{0}' must be greater than zero.")] 
    public int Amount { get; set; } 
} 

当我通过这一个Create图,@model PayCaddy.Client.Models.BetViewModel,我上“EditorFor” BetName线的Input string was not in a correct format.例外,在视图模型上面提到:

<div class="form-group"> 
    @Html.LabelFor(model => model.BetName, htmlAttributes: new { @class = "control-label col-md-2" }) 
    <div class="col-md-10"> 
     @Html.EditorFor(model => model.BetName, new { htmlAttributes = new { @class = "form-control" } }) 
     @Html.ValidationMessageFor(model => model.BetName, "", new { @class = "text-danger" }) 
    </div> 
</div> 

当然BetName为空;这是一个Create操作。唯一的其他信息,我能给我是如何映射视图模型:

[HttpGet] 
public ActionResult Create() 
{ 
    var bet = new Bet(); 
    bet.Owner = GetApplicationUser(); 
    var model = Mapper.Map<BetViewModel>(bet); 
    return View(model); 
} 

我从来没有碰到过这样的事情在几年。顺便说一句,我试图使这个可切换的桌面浏览器/移动应用程序,使用jQuery.Mobile,但我没有任何移动启用 - 它应该作为一个正常的桌面应用程序。

+0

剃刀并不总是报告错误的正确路线,我怀疑你的错误实际上是别的地方在你的看法。如果你仅仅注释掉@ Html.EditorFor(m => m.BetName,..)'会发生什么? –

+0

@StephenMuecke如果我将'BetName'注释掉,它可以正常工作。 – ProfK

+1

啊,你错误信息字符串不正确 - '{2}'应该是'{2}'(但我想要'{1}') –

回答

0

你必须在ErrorMessage字符串错误 - {2){2},但如果你想要阅读的消息BetName必须在100个字符以内。,那么它应该是{1}

[StringLength(100, ErrorMessage = "'{0}' must be {1} characters or less.")] 
public string BetName { get; set; } 
相关问题