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値のみが含まれているという問題が発生しています。
なぜフェッチ時にチェックが外されているのか
ありがとうございます。
コードを実装する場所を教えてください。 –
@skSoniコメント "//イベントハンドラの更新"の下にある "on(" click "'イベントハンドラ)を変更する必要があります。入力フィールドのタイプをチェックしてチェックボックスかテキストフィールドを作成し、それを適切に処理してください。 –
結構です。ありがとう。:) –