2017-11-07 17 views
0

mvcのチェックボックスで常にnull値を取得しています。チェックボックスをオンにするかチェックを外すと、null値のみが含まれます。ここでチェックボックスの値は常にmvcのヌル値を表示します

ビューページ

@model IEnumerable<SchoolWebApplication.Models.EventMaster> 
<table id="tblEvent" class="table" cellpadding="0" cellspacing="0"> 
    <tr> 
     <th style="width:100px; display:none">Event Id</th> 
     <th style="width:150px">Event</th> 
     <th style="width:150px">Active</th> 
    </tr> 

    @if(Model != null) 
    { 
     foreach (SchoolWebApplication.Models.EventMaster eventMaster in Model) 
     { 
      <tr> 
       <td class="EventID" style="display:none"> 
        <span>@eventMaster.EventID</span> 
       </td> 
       <td class="Event"> 
        <span style="color:darkgreen">@eventMaster.Event</span> 
        <input type="text" value="@eventMaster.Event" style="display:none; color:darkgreen" /> 
       </td> 
       <td class="IsActive"> 
        <span style="color:darkgreen">@eventMaster.IsActive</span> 
        @if (@eventMaster.IsActive == true) 
        { 
         <input type="checkbox" value="@eventMaster.IsActive" style="display:none; color:darkgreen" checked="checked" name="abc"/> 
        } 
        else 
        { 
         <input type="checkbox" value="@eventMaster.IsActive" style="display:none; color:darkgreen" name="abc"/> 
        } 
       </td> 

       <td> 
        <a class="Edit" href="javascript:;">Edit</a> 
        <a class="Update" href="javascript:;" style="display:none">Update</a> 
        <a class="Cancel" href="javascript:;" style="display:none">Cancel</a> 
       </td> 
      </tr> 
     } 
    } 
</table> 
<script type="text/javascript"> 
    function AppendRow(row, EventID, Event, IsActive) { 
     //Bind EventID. 
     $(".EventID", row).find("span").html(EventID); 

     //Bind Event. 
     $(".Event", row).find("span").html(Event); 
     $(".Event", row).find("input").val(Event); 

     //Bind IsActive. 
     $(".IsActive", row).find("span").html(IsActive); 
     $(".IsActive", row).find("input").val(IsActive); 

     $("#tblEvent").append(row); 
    }; 

    //Edit event handler. 
    $("body").on("click", "#tblEvent .Edit", function() { 
     var row = $(this).closest("tr"); 
     $("td", row).each(function() { 
      if ($(this).find("input").length >= 0) { 
       $(this).find("input").show(); 
       $(this).find("span").hide(); 
      } 
     }); 
     row.find(".Update").show(); 
     row.find(".Cancel").show(); 
     $(this).hide(); 
    }); 

    //Update event handler. 
    $("body").on("click", "#tblEvent .Update", function() { 
     var row = $(this).closest("tr"); 
     $("td", row).each(function() { 
      if ($(this).find("input").length >= 0) { 
       var span = $(this).find("span"); 
       var input = $(this).find("input"); 
       span.html(input.val()); 
       span.show(); 
       input.hide(); 
      } 
     }); 
     row.find(".Edit").show(); 
     row.find(".Cancel").hide(); 
     $(this).hide(); 

     var event = {}; 
     event.EventID = row.find(".EventID").find("span").html(); 
     event.Event = row.find(".Event").find("span").html(); 
     event.IsActive = row.find(".IsActive").find("span").html(); 

     $.ajax({ 
      type: "POST", 
      url: "/Event/Update", 
      data: JSON.stringify({ eventMaster: event }), 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function (response) { 
       alert(response.IsActive); 
     } 
     }); 
    }); 
</script> 

コントローラ

try 
{ 
    EventMaster updatedEvent = (from c in entities.eventMaster 
           where c.EventID == eventMaster.EventID 
           select c).FirstOrDefault(); 
    updatedEvent.Event = eventMaster.Event; 
    updatedEvent.IsActive = eventMaster.IsActive; 
    entities.SaveChanges(); 

    return new EmptyResult(); 
} 
catch (Exception ex) 
{ 
    return View(); 
} 

、私のコードで、テーブル内の3つのフィールドイベントID、イベントおよびActiveがあります。アクティブなときには、更新時にチェックボックスが含まれています。

ここで、チェックボックスがチェックされているかどうかをチェックすると、null値のみが含まれているという問題が発生しています。

なぜフェッチ時にチェックが外されているのか

ありがとうございます。

答えて

1

チェックボックスの.valを入力すると、入力要素のvalue属性の内容(ある場合)が表示されます。これは、ユーザーがボックスをチェックすると変更されません。

チェックボックスは、あなたのようなものを使用する必要がありjQueryのにチェックされているかどうかを確認するには、次の瞬間に

if (input.is(":checked")){} 

を、あなたはスパンで.IsActiveの電流値と、チェックボックスの値を格納していて、更新メソッドが実行されるときに、同じ値を取得してスパンに入れるだけで、何も更新されません。しかし、あなたのコードでさらに探し

- あなたはあなたの方法は、実際にサーバーに投稿されたものを確認する必要があります - あなたがオブジェクトの上にいくつかのパラメータに生のHTMLを渡していることを見て:

event.IsActive = row.find(".IsActive").find("span").html(); 

をせいぜい、 event.IsActiveは、モデルが予期している実際のブール値ではなく、文字列"True"(またはFalse)になります。あなたのような何かにその行を変更したほうが良いでしょう:

event.IsActive = row.find(".IsActive").find("input").is(":checked"); 

そして、ブラウザのネットワークタブでサーバーに送信されているものを確認します。

+0

コードを実装する場所を教えてください。 –

+0

@skSoniコメント "//イベントハンドラの更新"の下にある "on(" click "'イベントハンドラ)を変更する必要があります。入力フィールドのタイプをチェックしてチェックボックスかテキストフィールドを作成し、それを適切に処理してください。 –

+1

結構です。ありがとう。:) –

関連する問題