c# - Trouble writing controller actions that work with JQuery to update database -


i have html table holds categories of things. each row consists of category id , name looped in model. there 2 buttons in each row. 1 want use set state of row enabled , other set disabled:

<table id="categorylist" class="table">     <thead>     <tr>         <th>category id</th>         <th>category name</th>     </tr>     </thead>     <tbody>     @foreach (var item in model.categories)     {         <tr>             <td>@item.id</td>             <td>@item.name</td>             <td>                 <button class="btn btn-success categoryenabled">enabled</button>                 <button class="btn btn-danger categorydisabled" style="display: none;">disabled</button>             </td>         </tr>     }     </tbody> </table> 

when set enabled or disabled mean change bit value row in sql table column called state. so, need buttons toggle bit value row in clicked.

here i'm @ solution:

here's jquery using try change bit value selected row when each button clicked:

$(".categoryenabled").on("click", function () {     $(this).hide();     $(this).next().show();     disablerow($(this).attr('id')); });  $(".categorydisabled").on("click", function () {     $(this).hide();     $(this).prev().show();     enablerow($(this).attr('id')); });  function enablerow(id) {     $.post("@url.action("disablerow", "category")", { "id": id }, function (response) {     if (response.success) {         alert('row disabled!');     } else {         alert('error disabling row!');     } }); }  function disablerow(id) {     $.post("@url.action("enablerow", "category")", { "id": id }, function (response) {     if (response.success) {         alert('row enabled!');     } else {         alert('error enabling row!');     } }); 

you can see i'm trying connect these controller action in categorycontroller. not sure put these actions make connection state column of category table using entity framework.

public actionresult disablerow()     {      }  public actionresult enablerow()     {      } 

i need loop identity row i'm trying update:

int = 0;  foreach (var row in model.rows) { string id = "row" + i.tostring() + "enable"; +=1; } 

it helpful see category object have it:

namespace rework.models { using system; using system.collections.generic;      public enum statestypes     {         disabled = 0,         enabled = 1     }  public partial class category {      public int id { get; set; }     public string name { get; set; }     public statestypes state { get; set; }  } 

}

db context:

namespace rework.models { using system; using system.data.entity; using system.data.entity.infrastructure;  public partial class categorydbentities : dbcontext {     public categorydbentities()         : base("name=categorydbentities")     {     }      protected override void onmodelcreating(dbmodelbuilder modelbuilder)     {         throw new unintentionalcodefirstexception();     }      public dbset<category> categories { get; set; } } 

}

just not sure if there better way or fit in. help!

ok firstly jquery isn't getting id. try updating code this

<table id="categorylist" class="table">     <thead>         <tr>             <th>category id</th>             <th>category name</th>         </tr>     </thead>     <tbody>         @foreach (var item in model.categories)         {             <tr>                 <td>@item.id</td>                 <td>@item.name</td>                 <td>                     <button class="btn btn-success categoryenabled" data-id="@item.id">enabled</button>                     <button class="btn btn-danger categorydisabled" data-id="@item.id" style="display: none;">disabled</button>                 </td>             </tr>         }     </tbody> </table> 

now id part of button can access using this keyword

$(".categoryenabled").on("click", function () {     $(this).hide();     $(this).next().show();     disablerow($(this).data('id')); });  $(".categorydisabled").on("click", function () {     $(this).hide();     $(this).prev().show();     enablerow($(this).data('id')); });  function enablerow(id) {     $.post("@url.action('disablerow', 'category')", { "id": id }, function () {          alert('row disabled!');      }).fail(function() {          alert('error disabling row!');     }); }  function disablerow(id) {       $.post("@url.action('enablerow', 'category')", { "id": id }, function() {          alert('row enabled!');       }).fail(function () {         alert('error enabling row!');     });  } 

next need update actions. im not sure why you're returning actionresult doesn't need specific response. don't know object use connect database context need replace this.

public void disablerow(int id) {      //     // create instance of ef database context     //     var context = new categorydbentities();      var record = context.categories.find(id);      if (record != null)     {         record.state = statetypes.disabled;          context.savechanges();     }   }  public void enablerow(int id) {      //     // create instance of ef database context     //     var context = new categorydbentities();      var record = context.categories.find(id);      if (record != null)     {         record.state = statetypes.enabled;          context.savechanges();     }  } 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

android - CollapsingToolbarLayout: position the ExpandedText programmatically -

Listeners to visualise results of load test in JMeter -