我有一个复杂的模型RecruiterProfileViewModel
通过multipart形式发布(因为可以包含文件上传)到我的ASP.NET MVC控制器。它很复杂,因为它包含一个自定义子CompanyOverviewViewModel
对象。
虽然浏览器调试显示要填充该子项的数据,但我的profile.CompanyOverview
帖子方法中null
仍为RecruiterProfileController.Edit(RecruiterProfileViewModel profile)
。
为什么,或者我如何轻松调查?
以下是:
的片段if (ModelState.IsValid)
点的VS中捕获){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
<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)
答案 0 :(得分:0)
如何将CompanyOverviewViewModel
声明为Action方法的单独参数,然后将其分配给RecruiterProfileViewModel
?
[CommunityRoles(CommunityRoles.Recruiter)]
[HttpPost]
public virtual ActionResult Edit(RecruiterProfileEditorViewModel profile,
CompanyOverviewViewModel overview) {
profile.CompanyOverview = overview;
if (ModelState.IsValid) {
ActivityMonitor.Log(ActivityLogEntryTypes.EditRecruiterProfile);
profile.Save(CurrentVisitor.Account.Recruiter);
return RedirectToAction(MVC.Hub.Index());
}
else {
return View(profile);
}
}
答案 1 :(得分:0)
我的问题是视图中其他地方的拼写错误,我通过在本地构建MVC并进入它来找到它。
我有一个CompanyOverview.History
textarea输入,我错误地将其指定为CompanyOverview
。模型绑定器首先尝试将值绑定到简单属性而不是基于提交的表单数据的复杂属性,而不是基于目标模型。
这导致尝试将字符串分配给我的自定义类型和空子模型的绑定错误。