2015-12-28 27 views
0

我的視圖是腳手架生成的列表。對於列表中的模式是:模型綁定對於MVC中的Ajax.BeginForm失敗5

public class LogsViewModel 
{ 
     public string Category { get; set; } 
     public string ClientIP { get; set; } 
     public string StartDate { get; set; } 
     public string EndDate { get; set; } 
     public string Message { get; set; } 
} 

要過濾整個列表,我提供了一種具有多個輸入元件,例如自舉模態形式:類別,clientip,日期和消息。這種模式的形式使用Ajax.BeginForm以下方式:在控制器

@using (Ajax.BeginForm(
         new AjaxOptions 
         { 
         HttpMethod = "get", 
         InsertionMode = InsertionMode.Replace, 
         UpdateTargetId = "results" 
         }))  
       { 
        <fieldset> 
        <div class="form-group"> 
         <label for="recipient-name" class="control-label">Category:</label> 
         <select class="form-control" id="Category"> 
          <option>Information</option> 
          <option>General Error</option> 
         </select> 
         @*<input type="text" class="form-control" id="category">*@ 
        </div> 
        <div class="form-group"> 
         <label for="recipient-name" class="control-label">IP:</label> 
         <input type="text" class="form-control" id="ClientIP"> 
        </div> 
        <div class="form-group" id="startDTPicker" style="position:relative"> 
         <label for="recipient-name" class="control-label" id="startDate">Start Date:</label> 
         <input type="text" class="form-control" id="StartDate"> 
        </div> 
        <div class="form-group" id="endDTPicker" style="position:relative"> 
         <label for="recipient-name" class="control-label" id="endDate">End Date:</label> 
         <input type="text" class="form-control" id="EndDate"> 

        </div> 
        <div class="form-group" id="startDTPicker"> 
         <label for="recipient-name" class="control-label">Message:</label> 
         <input type="text" class="form-control" id="Message"> 
        </div> 


        <div class="modal-footer"> 
         <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
         <input type="submit" class="btn btn-primary" value="Show Results" /> 
        </div> 
       </fieldset> 
      } 

於是,我試圖讓這些參數(類別= ...)。我已經宣佈ActionMethod爲:

public ActionResult Index(LogsViewModel viewModel) 
{ 
    //all the properties are null when the Ajax request invokes this method 
} 

什麼是導致問題,什麼是最好的處理方式?

+1

使用HTML輔助或補充name屬性 – Umer

回答

3

正如我在您的查看代碼中可以看到的,您在所有input標記上沒有name屬性。但是你需要他們,他們應該匹配你的模型屬性名稱,因爲當你將值發佈到服務器時,他們正在使用類似的鍵。

我的意思是你應該改變這樣的:

<input type="text" class="form-control" id="ClientIP"> 

以此爲所有標籤:

<input type="text" name="ClientIP" class="form-control" id="ClientIP">