为什么在Create视图中出现字符串格式错误?

时间:2015-08-30 04:16:46

标签: asp.net-mvc asp.net-mvc-5

我有以下视图模型:

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视图时,我会在编辑器上获得Input string was not in a correct format.个例外'} BetName行,在上面提到的视图模型中:

<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将其设置为可切换的桌面浏览器/移动应用程序,但我还没有启用任何移动设备 - 它应该可以作为普通的桌面应用程序运行。

1 个答案:

答案 0 :(得分:0)

ErrorMessage字符串中出现错误 - {2)应为{2},但如果您希望邮件读取 BetName,则必须为100个字符或更少。,那么它应该是{1}

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