ASP.NET MVC コントローラーにマルチパート フォームを介して投稿された複雑なモデルがRecruiterProfileViewModel
あります (ファイルのアップロードを含めることができるため)。1 つのカスタム子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 Data in model set to null when Postingの未回答のインシデントです。