0

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

尽管浏览器调试显示了正在发布的孩子要填充的数据,profile.CompanyOverview但仍在nullRecruiterProfileController.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 数据在发布时设置为空

4

2 回答 2

0

我的问题是视图中其他地方的错字,我通过在本地构建 MVC 并进入它来定位它。

我有一个CompanyOverview.Historytextarea 输入,但我错误地指定为CompanyOverview. modelbinder 首先尝试根据提交的表单数据,而不是基于目标模型,将值绑定到简单属性而不是复杂属性。

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

于 2013-10-15T19:44:57.460 回答
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);
    }
}
于 2013-10-15T02:10:13.323 回答