2012-01-24 4 views
1

私のViewModelで、コントローラのCreateアクションポストバックのベースモデルプロパティのnull以外のオブジェクトを返すのに問題があります。私は現在、ほとんど同じプロパティを持つ別のモデルで全く同じ操作をしている別のページを持っており、フォームは完全に動作しているので、何かを間違って配置することはできませんが、ここでViewModelベースモデルがMVC3で作成アクションポストバックでnullです

は私の基本クラスと私のViewModelです:

public class SystemFailureActionViewModel 
{ 
    /// <summary> 
    /// View model class for adding and modifying SystemFailureActions 
    /// </summary> 
    public SystemFailureAction action { get; set; } 

    //properties 
    public int TypeID { get; set; } 
    public string TypeDescription { get; set; } 
    public bool Assigned { get; set; } 

    public SystemFailureActionViewModel() { } 

    public SystemFailureActionViewModel(SystemFailureAction action) 
    { 
     this.action = action; 
    } 

    //Collections for views 
    public IEnumerable<SelectListItem> EditSystemFailiureTypesList { get { return ModelListProvider.FilteredSystemFailureTypeList; } } 
    public IEnumerable<SelectListItem> DetailsSystemFailiureTypesList { get { return ModelListProvider.FullSystemFailureTypeList; } } 
} 

[MetadataType(typeof(SystemFailureActionMetadata))] 
public partial class SystemFailureAction 
{ 
    private class SystemFailureActionMetadata 
    { 
     /// <summary> 
     /// ID for this failure action 
     /// </summary> 
     public int ID { get; set; } 

     /// <summary> 
     /// Action Description 
     /// </summary> 
     [Required] 
     [StringLength(200)] 
     [DisplayName("Action Description")] 
     public string Description { get; set; } 
    } 
} 

ここに私のコントローラを追加し、追加ポストバックメソッドです:

public ActionResult Add() 
    { 
     SystemFailureAction action = new SystemFailureAction(); 
     action.Description = ""; 
     populateSystemFailureActionData(action); 
     return PartialView("Form", new SystemFailureActionViewModel(action)); 
    } 


    [HttpPost] 
    public ActionResult Add(SystemFailureActionViewModel viewModel, string[] selectedTypes, FormCollection collection) 
    { 
     SystemFailureAction newAction = viewModel.action; 

     if (!ModelState.IsValid) 
     { 
      populateSystemFailureActionData(newAction); 
      return PartialView("Form", new SystemFailureActionViewModel(newAction)); 
     } 

     try 
     { 
      //Insert the new failure action type 
      context.SystemFailureActions.InsertOnSubmit(newAction); 
      context.SubmitChanges(); 

      //Insert the failure type mappings 
      updateSystemFailureAssociationData(newAction, selectedTypes); 
      context.SubmitChanges(); 

      //Return the new data 
      populateSystemFailureActionData(newAction); 
      return PartialView("Done", new SystemFailureActionViewModel(newAction)); 
     } 
     catch (Exception ex) 
     { 
      ModelState.AddModelError("", ex.Message); 
      populateSystemFailureActionData(newAction); 
      return PartialView("Form", new SystemFailureActionViewModel(newAction)); 
     }   
    }  

を最後にここに私のフォームがあり、それはjQueryのダイアログにロードされていますポストバックはAjax経由で行われています。

@using (Ajax.BeginForm(new AjaxOptions 
{ 
    HttpMethod = "Post", 
    UpdateTargetId = "formDialog", 
    InsertionMode = InsertionMode.Replace, 
    OnSuccess = "onDialogDone()" 
})) 
{ 
    @Html.ValidationSummary(true) 
    <div> 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.action.Description) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.action.Description) 
     </div> 
    </div> 
    <div class="categoryFieldSet"> 
     <fieldset> 
      <legend>Mechanical System Failure Categories</legend> 
      <div class="editor-field"> 
       <table> 
        <tr> 
         @{ 
          int count = 0; 
          List<ManageMAT.ViewModels.SystemFailureActionViewModel> types = ViewBag.Types; 

          foreach (var type in types) 
          { 
           if (count++ % 3 == 0) 
           { 
            @: </tr> <tr> 
           } 
           @: <td> 
            <input type="checkbox" 
              id="selectedTypes + @type.TypeID" 
              name="selectedTypes" 
              value="@type.TypeID" 
              @(Html.Raw(type.Assigned ? "checked=\"checked\"" : "")) /> 
            <label for="selectedTypes + @type.TypeID">@type.TypeDescription</label> 
           @:</td> 
          } 
         @:</tr> 
         } 
       </table> 
      </div> 
     </fieldset> 
    </div> 
} 

あなたはViewBag.Typesロジックは、それが私が以前に尋ねたthis質問に関連している形であるかと思っている場合。

編集:

私はにModelStateエラーをチェックし、私は取得しています例外が

"The parameter conversion from type 'System.String' to type Models.SystemFailureAction' failed because no type converter can convert between these types." 

である私はまた、障害の種類を追加し、私はまだ同じ問題を受けてるのロジックを削除しました。したがって、問題はViewModel.action.Descriptionをアクションにマッピングすることから来ているようです。

public SystemFailureAction action { get; set; } 

ASP.NET MVCでactioncontrollerはちょっと言葉を予約されています

答えて

2

問題は、あなたのSystemFailureActionViewModelで、次のプロパティです。それらはすべてのルートの一部です。そして、あなたがこの方法で呼ばれたプロパティを持っているならば、それはアクション名を指している文字列値と衝突し、複合体SystemFailureAction型にはっきりと結合できません。だから、単にあなたのビューモデルに、このプロパティの名前を変更し、問題を解決する

public SystemFailureAction FailureAction { get; set; } 
+0

優秀な、私もそれが問題であることを考えていませんでした。それが私の問題を解決しました。 私はそれが何らかの種類のコンパイルエラーを投げたり、VSの予約済みのキーワードとして表示したりするのはなぜだろうか? – Philter

関連する問題