This should be straight-forward, but here goes – we’re using MVC4 to handle a multi-part Form request to upload a binary along with some meta-data via a strongly-typed view in MVC4.
One of the fields is a version number for the file (i.e. something like 0.0.6, 0.4.5-pre, etc…)
I’m getting the following error from the model binder when it tries to bind this version number field to the model field (string type):
{“The parameter conversion from type ‘System.String’ to type
‘Models.NewFileVersion’ failed because no type converter can convert
between these types.”}
Specifically the error can be traced to our “VersionNumber” field – any ideas as to why this might be happening?
Edit: Source code below
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);
}
Try renaming the
versionparameter for yourCreateaction, e.g:The model binder may be getting confused between the
string versionmodel property and theNewFileVersion versionaction parameter.You can see why this happens in the
BindModelmethod, because the model has a property exactly matching the name of the action parameter it tries to bind as a simple type/model rather than a complex one.