复杂模型子元素不绑定(为null)

时间:2013-10-14 20:36:32

标签: asp.net-mvc model-binding

我有一个复杂的模型RecruiterProfileViewModel通过multipart形式发布(因为可以包含文件上传)到我的ASP.NET MVC控制器。它很复杂,因为它包含一个自定义子CompanyOverviewViewModel对象。

虽然浏览器调试显示要填充该子项的数据,但我的profile.CompanyOverview帖子方法中null仍为RecruiterProfileController.Edit(RecruiterProfileViewModel profile)

为什么,或者我如何轻松调查?

以下是:

的片段
  • 绑定错误
  • 发布数据(一部分,在浏览器调试器中捕获)
  • 控制器(相关方法)
  • 父模型
  • 儿童模特
  • 模型调试(在if (ModelState.IsValid)点的VS中捕获)
  • 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 Data in model set to null when posting

的未答复事件

2 个答案:

答案 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。模型绑定器首先尝试将值绑定到简单属性而不是基于提交的表单数据的复杂属性,而不是基于目标模型。

这导致尝试将字符串分配给我的自定义类型和空子模型的绑定错误。