2015-08-30 38 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; } 
相關問題