ASP.NET MVC模型Binder失败,看起来像版本号的字符串?

时间:2012-04-07 09:09:33

标签: asp.net-mvc model-binding asp.net-mvc-4

这应该是直截了当的,但是接下来 - 我们使用MVC4处理多部分Form请求,通过MVC4中的强类型视图上传二进制文件和一些元数据。

其中一个字段是文件的版本号(例如 0.0.6 0.4.5-pre 等等)

当我尝试将此版本号字段绑定到模型字段(字符串类型)时,我从模型绑定器中收到以下错误:

  

{“从类型'System.String'到类型的参数转换   'Models.NewFileVersion'失败,因为没有类型转换器可以转换   这些类型之间。“}

具体而言,错误可以追溯到我们的“VersionNumber”字段 - 关于为什么会发生这种情况的任何想法?

编辑:下面的源代码

NewFileVersion.cs

public class NewFileVersion
{
    [Display(Name = "Version # (0.67, 0.66-pre, etc...)")]
    [Required]
    public string Version { get; set; }

    [Required]
    [StringLength(2000, ErrorMessage = "ChangeLog must be between 30 an 2000 characters", MinimumLength = 30)]
    [Display(Name = "Version Notes (will be visible to end-users)")]
    [DataType(DataType.MultilineText)]
    public string ChangeLog { get; set; }

    [Display(Name = "Target Platform")]
    [UIHint("Enum")]
    public FileType PlatformTarget { get; set; }
}

New.cshtml

    @model ViewModels.NewFileVersion
@{
    ViewBag.Title = "New";
}
<div class="container" id="main-content">
    <div class="row">
        <h2>
            New</h2>
        @using (Html.BeginForm("Create", "Files", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
            @Html.ValidationSummary(true)

            <fieldset>
                <legend>NewFileVersion</legend>
                <div class="editor-label">
                    @Html.LabelFor(model => model.Version)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Version)
                    @Html.ValidationMessageFor(model => model.Version)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.ChangeLog)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.ChangeLog)
                    @Html.ValidationMessageFor(model => model.ChangeLog)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.PlatformTarget)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.PlatformTarget)
                </div>
                <div class="editor-label">
                    <label for="">
                        File:</label></div>
                <div class="editor-field">
                    <input type="file" name="fileData" required="required" /></div>
                <p>
                    <input type="submit" value="Upload File" />
                </p>
            </fieldset>
        }
        <div>
            @Html.ActionLink("Back to List", "Index")
        </div>
    </div>
</div>

FilesController.cs

 [HttpPost]
    public ActionResult Create(NewFileVersion version, HttpPostedFileBase fileData)
    {
        //if our model is valid
        if(ModelState.IsValid)
        {
            //etc....             
        }

        ModelState.AddModelError("", "Invalid file submission");

        return View("New", version);
    }

1 个答案:

答案 0 :(得分:1)

尝试为version操作重命名Create参数,例如:

public ActionResult Create(NewFileVersion fileVersion, HttpPostedFileBase fileData) { ... }

模型绑定器可能会在string version模型属性和NewFileVersion version操作参数之间混淆。


你可以在BindModel方法中看到这种情况发生的原因,因为模型的属性与它尝试绑定的动作参数的名称完全匹配,而不是复杂的类型/模型。