我有一个RecruiterProfileViewModel
通过多部分表单(因为可以包含文件上传)发布到我的 ASP.NET MVC 控制器的复杂模型。它很复杂,因为它拥有一个自定义子CompanyOverviewViewModel
对象。
尽管浏览器调试显示了正在发布的孩子要填充的数据,profile.CompanyOverview
但仍在null
我RecruiterProfileController.Edit(RecruiterProfileViewModel profile)
的发布方法中。
为什么,或者我怎样才能轻松调查?
以下是其中的片段:
- 绑定错误
- 发布的数据(一部分,在浏览器调试器中捕获)
- 控制器(相关方法)
- 父模型
- 儿童模型
- 模型调试(此时在VS中捕获
if (ModelState.IsValid)
) - HTML 输入属性
- 看法
绑定错误
{System.InvalidOperationException: The parameter conversion from type 'System.String' to type 'MyApplication.Web.Models.RecruiterProfile.CompanyOverviewViewModel' failed because no type converter can convert between these types.
at System.Web.Mvc.ValueProviderResult.ConvertSimpleType(CultureInfo culture, Object value, Type destinationType)
at System.Web.Mvc.ValueProviderResult.UnwrapPossibleArrayType(CultureInfo culture, Object value, Type destinationType)
at System.Web.Mvc.ValueProviderResult.ConvertTo(Type type, CultureInfo culture)
at System.Web.Mvc.ValueProviderResult.ConvertTo(Type type)
at System.Web.Mvc.DefaultModelBinder.ConvertProviderResult(ModelStateDictionary modelState, String modelStateKey, ValueProviderResult valueProviderResult, Type destinationType)}
发布数据
Request URL:http://localhost:61775/recruiterprofile/edit
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryVKowpcCD1LxGmyfb
Request Payload:
------WebKitFormBoundaryVKowpcCD1LxGmyfb Content-Disposition: form-data; name="FirstName"
Marks
------WebKitFormBoundaryVKowpcCD1LxGmyfb Content-Disposition: form-data; name="CompanyOverview.PostalCode"
53213
控制器
public partial class RecruiterProfileController {
[CommunityRoles(CommunityRoles.Recruiter)]
[HttpPost]
public virtual ActionResult Edit(RecruiterProfileEditorViewModel profile) {
if (ModelState.IsValid) {
ActivityMonitor.Log(ActivityLogEntryTypes.EditRecruiterProfile);
profile.Save(CurrentVisitor.Account.Recruiter);
return RedirectToAction(MVC.Hub.Index());
}
else {
return View(profile);
}
}
父模型
public class RecruiterProfileEditorViewModel {
[Required]
[StringLength(100, MinimumLength = 2, ErrorMessage = "Please enter your full first name")]
[DisplayName("First name")]
public string FirstName { get; set; }
public CompanyOverviewViewModel CompanyOverview { get; set; }
儿童模型
public class CompanyOverviewViewModel {
[Required]
[DisplayName("Postal Code")]
public string PostalCode { get; set; }
模型调试
profile.FirstName: "Marks",
profile.CompanyOverview: null
HTML 输入
<input class="text-box single-line valid" data-val="true" data-val-length="Please enter your full first name" data-val-length-max="100" data-val-length-min="2" data-val-required="The First name field is required." id="FirstName" name="FirstName" placeholder="" type="text" value="Marks">
<input class="text-box single-line valid" data-val="true" data-val-required="The Postal Code field is required." id="CompanyOverview_PostalCode" name="CompanyOverview.PostalCode" placeholder="" type="text" value="53213">
看法
@Html.DecoratedEditorFor(m => m.FirstName)
@Html.DecoratedEditorFor(m => m.CompanyOverview.Name)
这是同一问题的未答复事件,模型中的 MVC3 数据在发布时设置为空