2017-08-01 50 views
0

我在那裏我渲染我的局部視圖使用表作爲數據的MVC項目映射到C#列表:獲取價值和使用jQuery

<table id="tblUserSettings" class="table table-bordered CmtTable"> 
    <thead> 
     <tr> 
      <th>User Name</th> 
      <th>Country</th> 
      <th>System (s)</th> 
      <th>Actions</th> 
     </tr> 
    </thead> 
    <tbody> 
     @if (Model == null) 
     { 
      <tr></tr> 
     } 
     else 
     { 
      foreach (var item in Model) 
      { 
       <tr> 
        <td><input type="hidden" id="usrId" value="@item.UserId" /> 
        @item.UserName</td> 

        <td> <input type="hidden" id="usrCountryKey" value="@item.UserCountryKey" style="display:none"/> 
        @item.UserCountryName</td> 

        <td> <input type="hidden" id="usrSourceSystemKey" value="@item.UserSourceSystemKey" /> 
        @item.UserSourceSystemDescription</td> 

        <td><a onclick='DeleteUserSettingsRow();'><i class='fa fa-times'></i> Delete</a><a onclick='EditUserSettingsPopup();'><i class='fa fa-pencil'></i> Edit</a></td> 
       </tr> 
      } 
     } 
    </tbody> 
</table> 

我要去保存從該表中的值到數據庫中,需要調用AddUserSettingsaction方法在控制器:

[HttpPost, Route("AddUserSettings")] 
public ActionResult AddUserSettings(IEnumerable<UserSettings> request) 
{ 
    AddUserSettingsRequest apiRequest = null; 
    return View(); 
} 

UserSettings的模型如下:

public class UserSettings 
    { 
     public UserSettings(); 

     public string UserId { get; set; } 
     public string UserName { get; set; } 
     public string UserCountryKey { get; set; } 
     public string UserCountryName { get; set; } 
     public string UserSourceSystemKey { get; set; } 
     public string UserSourceSystemDescription { get; set; } 
    } 

我需要從表保存數據(包括隱藏字段)到使用jQuery數據庫,所以我已經創建了一個函數和調用它爲:

<button type="button" id="btnAdd" onclick="SaveUserSettings();"><i class="fa fa-save"></i> Save</button> 

     function SaveUserSettings() 
     { 
      debugger; 
      var userSettings = []; 

      $("table#tblUserSettings tr").each(function (i, v) { 
       userSettings[i] = []; 
       $(this).children('td').each(function (ii, vv) 
       { 
        userSettings[i][ii] = $(this).text(); 

       }); 
      }) 

      alert(userSettings); 

$.ajax({ 
      url: '@Url.Action("AddUserSettings", "Admin")', 
      type: "POST", 
      contentType: "application/json;", 
      data: JSON.stringify(userSettings), 
      success : function (result) 
      { 
       //alert(result); 
      }, 
      error: function (result) 
      { 
       //alert(result); 
      } 
     }); 
    } 

利用上述SaveUserSettings()函數,我可以獲取不隱藏的值,但是我需要創建一個包含隱藏屬性的數組,並且可以將ajax請求作爲參數與控制器一起發送。如何獲取隱藏字段並創建一個映射到我的控制器的IEnumerable請求的數組?

+0

不清楚你想做什麼。你沒有任何可編輯的表單控件,爲什麼你要回傳你的控制器發送給視圖的相同值。重複的'id'屬性是無效的html(使用類名和相對選擇器)。你也不需要'style =「display:none」'。 –

+0

局部視圖有一個UserSettings列表,所以最初它是空的,然後用戶將一些設置添加到表單中,並將相同的設置存儲到數據庫中。問題是當我從表中獲取數據時,隱藏字段的值正在檢索。任何建議都一樣嗎? –

+0

你不需要使用jquery解析它,只需要像這樣創建元素名稱'[0] .UserName' Modelbinder會爲你綁定它。 – Krishna

回答

1

提供的JavaScript是在局部視圖,您可以直接設置你的userSettings對抗模式,例如:

var userSettings = @Html.Raw(JsonConvert.SerializeObject(Model)); 

這一操作將序列化的對象轉換爲JSON,它會被傳遞給瀏覽器,然後在那裏你可以使用它在jQuery中將它傳遞迴服務器,而不必遍歷表中的每一行。

+0

或者你也可以這樣做: var userSettings = @ Html.Raw(Json.Encode(Model)); – pso

+0

這只是傳回原來的模型,發送到視圖是完全沒有意義的 –