2016-09-15 18 views
2

問題は、モデルバインディングがViewModelのいずれかのプロパティをバインドしないということです。ASP.NET MVC 4モデルバインディングの問題

私はViewMode、HomeIndexViewModelを持っています。 プロパティの1つであるExcludeClientsWithoutAddressがコントローラにバインドされません。

フィドラーは、(ExcludeClientsWithoutAddressなし)

GET /ホーム/ searchByClient?DB = DEV &ファイル番号= &のfirstName = XXX & includeContacts =真HTTP/1.1

としてGET要求を示します何らかの理由で、他のプロパティ(FileNumber、FirstName、LastName、IncludeContacts、およびFile)が正しくバインドされます。

enter image description here

私はここで何をしないのですか?コントローラ内の

namespace CertifiedMail.Web.Mvc4.OneOffs.Models 
{ 
    public class HomeIndexViewModel 
    { 
     [DisplayName("File #")] 
     public string FileNumber { get; set; } 

     [DisplayName("First Name")] 
     public string FirstName { get; set; } 

     [DisplayName("Last Name")] 
     public string LastName { get; set; } 

     [DisplayName("Include Contact")] 
     public bool IncludeContacts { get; set; } 

     [DisplayName("Exclude Clients Without Address")] 
     public bool ExcludeClientsWithoutAddress { get; set; } 

     public HttpPostedFileBase File { get; set; } 
    } 
} 

、ここで

[System.Web.Mvc.HttpGet] 
public string SearchByClient([FromUri]HomeIndexViewModel model) 
{ 
    IEnumerable<SearchResult> searchResults = new List<SearchResult>(); 

    SearchByArgs args = BuildSearchByArg(model); 

    if (string.IsNullOrWhiteSpace(model.FileNumber)) 
    { 
     if (!string.IsNullOrWhiteSpace(model.FirstName) || !string.IsNullOrWhiteSpace(model.LastName)) 
      searchResults = ClientSearchDataAccess.SearchByClientName(args); 
    } 
    else 
     searchResults = ClientSearchDataAccess.SearchByClientNumber(args); 

    return JsonConverter.SerializeSearchResults(searchResults); 
} 

ビューです。

<div class="col-sm-5 searchPanel"> 
    <div class="panel panel-default glowGridPanel"> 
     <div class="panel-body searchPanelBody"> 
      <div class="row"> 
       @using (Html.BeginForm("SearchByClient", "Home", 
        new { db = @Request.QueryString["db"] }, 
        FormMethod.Get, 
        new { id = "searchByClientForm", @class = "form-horizontal" })) 
       { 
        <fieldset> 
         <legend> 
          Search by Client 
          <span class="glyphicon glyphicon-user" aria-hidden="true"></span> 
         </legend> 

         @{ 
          var labelAttributes = new { @class = "col-sm-4 control-label" }; 
         } 

         <div class="form-group"> 
          @Html.LabelFor(m => m.FileNumber, labelAttributes) 
          <div class="col-sm-8"> 
           @Html.TextBoxFor(m => m.FileNumber, new { @class = "form-control input-sm", ng_model = "fileNumber" }) 
          </div> 
         </div> 

         <div class="form-group"> 
          @Html.LabelFor(m => m.FirstName, labelAttributes) 
          <div class="col-sm-8"> 
           @Html.TextBoxFor(m => m.FirstName, new { @class = "form-control input-sm", ng_model = "firstName" }) 
          </div> 
         </div> 

         <div class="form-group"> 
          @Html.LabelFor(m => m.LastName, labelAttributes) 
          <div class="col-sm-8"> 
           @Html.TextBoxFor(m => m.LastName, new { @class = "form-control input-sm", ng_model = "lastName" }) 
          </div> 
         </div> 

         <div class="form-group"> 
          <div class="col-sm-12 col-sm-offset-3"> 
           <div class="checkbox"> 
            Include contacts in search result? 
            @Html.CheckBoxFor(m => m.IncludeContacts, new { id = "includeContactsCheckBox", ng_model = "includeContacts" }) 
           </div> 
          </div> 
         </div> 

         <div class="form-group"> 
          <div class="col-sm-12 col-sm-offset-3"> 
           Exclude Clients without Address? 
           <div class="checkbox"> 
            @Html.CheckBoxFor(m => m.ExcludeClientsWithoutAddress, 
             new { id = "excludeClientsWithoutAddressCheckBox", ng_model = "excludeClientsWithoutAddress" }) 
           </div> 
          </div> 
         </div> 

         <button type="button" class="btn btn-primary pull-right submitButton" 
           ng-click="addSearchResultToGrid()" 
           ng-disabled="loadingSearch"> 
          Search by Client 
          <span class="glyphicon glyphicon-search" aria-hidden="true"></span> 
          <div id="loadingSearch" ng-show="loadingSearch"></div> 
         </button> 
        </fieldset> 
          } 
      </div> 

      <div class="row strike"> 
       <h3> 
        <span class="label label-default"> 
         <span class="glyphicon glyphicon-minus" aria-hidden="true"></span> 
         OR 
         <span class="glyphicon glyphicon-minus" aria-hidden="true"></span> 
        </span> 
       </h3> 
      </div> 

      <div class="row"> 
       @using (Html.BeginForm("Upload", "Home", 
         new { db = @Request.QueryString["db"] }, 
         FormMethod.Post, new { id = "uploadFileForm", enctype = "multipart/form-data" })) 
       { 
        <fieldset> 
         <legend>Search by Uploading File</legend> 

         <div class="input-group"> 
          <input type="file" class="form-control" name="file" id="file" /> 
          <span class="input-group-btn"> 
           <button class="btn btn-primary" type="submit"> 
            Upload File 
            <span class="glyphicon glyphicon-arrow-up" aria-hidden="true"></span> 
           </button> 
          </span> 
         </div> 
        </fieldset> 
       } 
      </div> 
     </div> 
    </div> 
</div> 
+1

Fiddlerは、ブラウザから送信されたものを表示し、あなたの 'ExcludeClientsWithoutAddress'プロパティを送信していません。あなたの角度コードはどういうわけかこのフィールドをドロップしていますか? – Jasen

+0

ありがとう、@ジャセン。それはまさにそれでした!その引数を受け入れるために角度サービスを更新するのを忘れてしまった!私は答えとしてマークすることができるように答えとしてあなたのコメントを追加しますか? – Sung

答えて

1

あなたのFiddlerリクエストには、ブラウザから送信されたものが表示されます。 ExcludeClientsWithoutAddressプロパティは送信されていません。

このプロパティはnullableとマークされていないため、bool?はバインディングでデフォルト値が割り当てられています。

これらの入力はng_modelとなっており、あなたの角度コードはこのフィールドを送信していないことを示しています。

+0

あなたはこれで半日を過ごした後で私を救っただけです。私は答えをありがとう〜 – Sung

関連する問題