2016-09-15 4 views
2

문제는 모델 바인딩이 ViewModel 속성 중 하나를 바인딩하지 않는다는 것입니다.ASP.NET MVC 4 모델 바인딩 문제

저는 ViewMode가 HomeIndexViewModel입니다. 속성 중 하나 인 ExcludeClientsWithoutAddress이 컨트롤러에 바인딩되지 않습니다.

피들러 (ExcludeClientsWithoutAddress없이) 같은 GET 요청

GET/홈/searchByClient? dB = dev에 &를 가진 FileNumber = & 인 firstName = XXX & includeContacts = 사실 HTTP/1.1

의 경우를 보여줍니다 어떤 이유로 다른 속성 (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는 브라우저에서 출력되는 내용을 표시하며 ExludeClientsWithoutAddress 속성을 보내지 않습니다. 각도 코드가 어떻게 든이 필드를 떨어 뜨릴 수 있습니까? – Jasen

+0

감사합니다. @ 재센. 그게 바로 그거야! 나는 인수를 받아들이는 각도 서비스를 갱신하는 것을 잊었다! 답변으로 표시 할 수 있도록 답변으로 의견을 추가 하시겠습니까? – Sung

답변

1

귀하의 피들러 요청에 따라 브라우저에서 방출되는 내용이 표시됩니다. ExcludeClientsWithoutAddress 속성을 전송하지 않습니다.

이 속성은 nullable로 표시되지 않으므로 bool? 바인딩에 기본값이 할당됩니다.

귀하는이 입력을 ng_model으로 표시합니다. 귀하의 각도 코드가이 필드를 보내지 않는다는 것을 나타냅니다.

+0

방금 ​​반나절을 보내고 나서 저를 구해 주셨습니다. 나는 그 대답을 고맙게 생각한다 ~ – Sung