2012-11-13 60 views
0

我有一個webgrid,每一行都有一個複選框。當單擊複選框時,我想以某種方式通過jQuery Ajax將該行的值傳遞給我的actionResult方法。 actionResult和jQuery Ajax部分不是問題,但我不知道如何從複選框onclick事件中獲取這些值。使用webgrid中的複選框MVC3 Razor - 如何獲取行值

@{ 
    ViewBag.Title = "Index"; 
} 
<h2> 
    Index</h2> 
<p> 
    @Html.ActionLink("Create New User", "CreateUser") 
</p> 
<div class="webgrid-wrapper"> 
    @model IEnumerable<UserManager.Models.vw_UserManager_Model> 
@{ 
    ViewBag.Title = "Jobs"; 
    WebGrid grid = new WebGrid(Model, canPage: true, canSort: true, rowsPerPage: 15, selectionFieldName: "selectedRow", fieldNamePrefix: "gridItem"); 

} 
    @grid.GetHtml(
    fillEmptyRows: true, 
     tableStyle: "webgrid", 
       alternatingRowStyle: "webgrid-alternating-row", 
       headerStyle: "webgrid-header", 
       footerStyle: "webgrid-footer", 
       selectedRowStyle: "webgrid-selected-row", 
      rowStyle: "webgrid-row-style", 
     mode: WebGridPagerModes.All, 
columns: new[] { 
    grid.Column("UserName"), 
    grid.Column("salutation"), 
    grid.Column("FirstName"), 
    grid.Column("LastName"), 
    grid.Column("Password"), 
    //grid.Column("isactive"), 
    //grid.Column(header: "Is logged in?", format: (model) => @Html.Raw("<input type='checkbox' checked='" + ((model.isactive) ? "checked" : "unchecked") + "' />")), 
    grid.Column(header: "Print?", format: @<text><input name="Prints" 
     type="checkbox" @(item.isactive == true ? "Checked" : null) onclick="logUserOff()" id="chkboxIsActive" /></text>), 
    grid.Column("isApproved"), 
    grid.Column("MaxConcurrentUsers"), 
    grid.Column("email"), 
    grid.Column("group_name"), 
    grid.Column("module_name"), 


    grid.Column(header:"Edit", format:@<text><div id="btnEditSelectedRow"> 
     "@Html.ActionLink("Edit record", "EditUser", "UserManager", new { 
     userid = item.userid, 
     salutation = item.salutation, 
     firstname = item.FirstName, 
     lastname = item.LastName, 
     password = item.Password, 
     isactive = item.isactive, 
     isapproved = item.IsApproved, 
     maxconcurrentusers = item.MaxConcurrentUsers, 
     email = item.email, 
     module = item.module_name, 
     group = item.group_name }, null)</div></text>), 

    grid.Column(header:"Delete", format:@<text><div id="btnDelSelectedRow"> 
     "@Html.ActionLink("Delete record", "DeleteUser", "UserManager", new { 
     userid = item.userid, 
     username = item.UserName, 
     salutation = item.salutation, 
     firstname = item.FirstName, 
     lastname = item.LastName, 
     password = item.Password, 
     isactive = item.isactive, 
     email = item.email, 
     module = item.module_name, 
     group = item.group_name }, null)</div></text>) 


}) 
</div> 
<script type="text/javascript"> 
    $(document).ready(function() { 

     // Disable checkboxs where a user is not active. 
     $("input:not(:checked)").attr("disabled", "disabled"); 

     // Style tables. 
     function jQueryUIStyling() { 
      $('input:button, input:submit').button(); 

      $('.webgrid-wrapper').addClass('ui-grid ui-widget ui-widget-content ui-corner-all'); 
      $('.webgrid-title').addClass('ui-grid-header ui-widget-header ui-corner-top'); 
      jQueryTableStyling(); 
     } // end of jQueryUIStyling 

     function jQueryTableStyling() { 
      $('.webgrid').addClass('ui-grid-content ui-widget-content'); 
      $('.webgrid-header').addClass('ui-state-default'); 
      $('.webgrid-footer').addClass('ui-grid-footer ui-widget-header ui-corner-bottom ui-helper-clearfix'); 
     } // end of jQueryTableStyling 
    }); 
</script> 
<script type="text/javascript"> 
    function logUserOff() { 
     var answer = confirm('Are you sure you want to save this data?') 
     if (answer) { 
      return true; 
     } 
     else { 
      return false; 
     } 
    }; 
</script> 

我複選框

grid.Column(header: "Print?", format: @<text><input name="Prints" 
     type="checkbox" @(item.isactive == true ? "Checked" : null) onclick="logUserOff()" id="chkboxIsActive" /></text>), 

我怎麼會改變我的複選框行值傳遞給JavaScript的?

回答

1

我結束了自己的想法。當你使用正確的字符文字

grid.Column(header: "Print?", format: @<text><input name="Prints" 
     type="checkbox" @(item.isactive == true ? "Checked" : null) onclick="logUserOff('@Url.Action("LogUserOff", "UserManager")', '@item.userid')" id="chkboxIsActive" /></text>), 

的onclick功能,只要路過剃刀引擎產生的值,以便JavaScript可以解析字符串,你的傳球。

<script type="text/javascript"> 
    function logUserOff(url, value) { 
     var answer = confirm('Are you sure you want to save this data?') 
     if (answer) { 
      alert(url + ": " + value); 

//   $.ajax({ 
//    
//   }); 


      return true; 
     } 
     else { 
      return false; 
     } 
    }; 

使用警報以確保傳遞正確的值。也許這可以幫助其他被困住的人。

相關問題