如果未选中复选框则添加新的内容,然后按保存按钮,将值保存到数据库true中, 当我按下编辑按钮时,如果选中了值,则不会显示选中的复选框。 所以当复选框选中时我想要的是将true保存在数据库中,如果未选中复选框则要保存false。 当我按编辑时,如果复选框的值为true,则显示复选框的值已选中,如果不是true,则显示为未选中的状态。 同样是问题单选按钮。
HTML
<div class="modal-content" style="width:140%">
<!--Header-->
<div class="modal-header">
<p class="heading lead">Update Purchase Order</p>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true" class="white-text">×</span>
</button>
</div>
<!--Body-->
<form id="NewOrder">
<div class="modal-body">
<div class="form-row">
<div class="col" id="poidd">
<!-- Purchase PO ID -->
<div class="md-form">
@Html.TextBoxFor(m=>m.PO_ID, new { @class = "form-control mr-sm-3", @id = "POUID", Required = true })
</div>
</div>
<div class="col">
<!-- Purchase PO NO -->
<label for="lblPONo" style="font-weight:bold; ">PO No.</label>
<div class="md-form">
@Html.TextBoxFor(m=>m.PONo, new { @class = "form-control mr-sm-3", @id = "POUNO", Required = true })
</div>
</div>
<div class="col">
<!-- PO Date -->
<label for="lblPODatepicker" style="font-weight:bold; "> PO Date</label>
<div class="md-form">
@Html.TextBoxFor(m => m.PODate, new { @class = "form-control", @id = "POUDate", Required = true, @Readonly=true })
</div>
</div>
<div class="col">
<!-- Requisition Date -->
<label for="lblRequisitionDatepicker" style="font-weight:bold; "> Requisition Date</label>
<div class="md-form">
@Html.TextBoxFor(m => m.RequisitionDate, new { @class = "form-control", @id = "RequisitionUDate", Required = true, @Readonly = true })
</div>
</div>
<div class="col">
<!-- Delivery Date -->
<label for="lblDeliveryDatepicker" style="font-weight:bold; "> Delivery Date</label>
<div class="md-form">
@Html.TextBoxFor(m => m.DeliveryDate, new { @class = "form-control", @id = "DeliveryUDate", Required = true, @Readonly = true })
</div>
</div>
<div class="col">
<!-- IsCancel-->
<div class="custom-control custom-checkbox custom-control-inline">
@Html.CheckBoxFor(m => m.IsCancelled, new { @class = "custom-control-input", @id = "IsUCancelled" })
<label class="custom-control-label" for="IsUCancelled">Cancel</label>
</div>
<!-- IsClosed-->
<div class="custom-control custom-checkbox custom-control-inline">
@Html.CheckBoxFor(m => m.IsClosed, new { @class = "custom-control-input", @id = "IsUClosed" })
<label class="custom-control-label" for="IsUClosed">Closed</label>
</div>
<!-- IsPending-->
<div class="custom-control custom-checkbox custom-control-inline">
@Html.CheckBoxFor(m => m.IsPending, new { @class = "custom-control-input", @id = "IsUPending" })
<label class="custom-control-label" for="IsUPending">Pending</label>
</div>
</div>
</div>
<div class="form-row">
<div class="col">
<!-- Party Code -->
<div class="md-form">
@Html.DropDownListFor(m => m.PartyCode, ViewBag.PartyCode as List<SelectListItem>, new { @class = "chosen-select", id = "PartyUCode", Required = true })
</div>
</div>
<div class="col" style="margin-left:1.5%;">
<!-- SellerQuotRefNo -->
<label for="lblSellerQuotRefNo" style="font-weight:bold; ">Seller Quot Ref No.</label>
<div class="md-form">
@Html.TextBoxFor(m => m.SellerQuotRefNo, new { @class = "form-control", @id = "SellerUQuotRefNo", @placeholder = "SellerQuotRefNo", Required = true })
</div>
</div>
<div class="col" style="margin-left:1.5%;">
<!--SellerQuotDate -->
<label for="lblSellerQuotDateDatepicker" style="font-weight:bold; "> Seller Quot Date</label>
<div class="md-form">
@Html.TextBoxFor(m => m.SellerQuotDate, new { @class = "form-control", @id = "SellerUQuotDate", Required = true, @Readonly = true })
</div>
</div>
<div class="col" style="margin-left:1.8%;">
<!-- ComparativeRefNo -->
<label for="lblComparativeRefNo" style="font-weight:bold; "> Comparative RefNo</label>
<div class="md-form">
@Html.TextBoxFor(m => m.ComparativeRefNo, new { @class = "form-control", @id = "ComparativeURefNo", @placeholder = "ComparativeRefNo", Required = true })
</div>
</div>
<div class="col">
<fieldset>
@Html.RadioButtonFor(e => e.WithItemCode, true, new { id = "RUtrue", @checked = true })
@Html.Label("RUtrue", "With Item Code")<br />
@Html.RadioButtonFor(e => e.WithItemCode, false, new { id = "RUfalse" })
@Html.Label("RUfalse", "With GL Code")
</fieldset>
<!-- IsPending-->
<div class="custom-control custom-checkbox custom-control-inline">
@Html.CheckBoxFor(m => m.ValidateQty, new { @class = "custom-control-input", @id = "ValidateUQty" })
<label class="custom-control-label" for="ValidateQty">Validate Qty</label>
</div>
</div>
</div>
<div class="form-row">
<div class="col" style="margin-left:1.5%;">
<!--TermsOfDelivery -->
<label for="lblTermsOfDelivery" style="font-weight:bold; "> Terms Of Delivery</label>
<div class="md-form">
@Html.TextAreaFor(m => m.TermsOfDelivery, new { @class = "form-control", @id = "UTermsOfDelivery", @placeholder = "TermsOfDelivery", Required = true })
</div>
</div>
<div class="col" style="margin-left:1.8%;">
<!--TermsOfPayment -->
<label for="lblTermsOfPayment" style="font-weight:bold; "> Terms Of Payment</label>
<div class="md-form">
@Html.TextAreaFor(m => m.TermsOfPayment, new { @class = "form-control", @id = "UTermsOfPayment", @placeholder = "TermsOfPayment", Required = true })
</div>
</div>
<div class="col" style="margin-left:1.8%;">
<!-- TermsOfConditions -->
<label for="lblTermsOfConditions" style="font-weight:bold; ">Terms Of Conditions</label>
<div class="md-form">
@Html.TextAreaFor(m => m.TermsOfConditions, new { @class = "form-control", @id = "UTermsOfConditions", @placeholder = "TermsOfConditions", Required = true })
</div>
</div>
</div>
<div class="modal-footer">
<button type="reset" class="btn btn-primary" data-dismiss="modal">Close</button>
<button id="UpdatePO" type="submit" class="btn btn-primary">Update</button>
</div>
</div>
</form>
</div>
<!--/.Content-->
</div>
</div>
Json函数
#region PO Main Select
public JsonResult POMain_Select(Int64 PO_ID)
{
PurchaseOrder mPO = new PurchaseOrder();
mPO = mPO_Select(PO_ID);
return Json(mPO, JsonRequestBehavior.AllowGet);
}
#endregion
#region Select PO for Edit
public PurchaseOrder mPO_Select(Int64 PO_ID)
{
try
{
PurchaseOrder mPO = new PurchaseOrder();
con = new SqlConnection(constring);
SqlCommand cmd = new SqlCommand("Select PO_ID, PONo, PODate, Comp_ID, GL_Year, Party_Code, SellerQuotRefNo, SellerQuotDate, RequisitionDate, DeliveryDate, ComparativeRefNo, TermsOfDelivery, TermsOfPayment, TermsOfConditions, IsPending, IsClosed, IsCancelled, EntryDate, UserName, POAmount, PaidAmount, IsPaid, WithItemCode, ValidateQty from POMain where PO_ID ='"+ PO_ID + "' ", con);
cmd.CommandType = CommandType.Text;
con.Open();
DataTable mDT = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(mDT);
mPO.PO_ID = mDT.Rows[0]["PO_ID"].ToString();
mPO.PONo = mDT.Rows[0]["PONo"].ToString();
mPO.PODate = DateTime.Parse(mDT.Rows[0]["PODate"].ToString()).ToString("yyyy-MM-dd");
mPO.Comp_ID = mDT.Rows[0]["Comp_ID"].ToString();
mPO.GL_Year = mDT.Rows[0]["GL_Year"].ToString();
mPO.PartyCode = mDT.Rows[0]["Party_Code"].ToString();
mPO.SellerQuotRefNo = mDT.Rows[0]["SellerQuotRefNo"].ToString();
mPO.SellerQuotDate = DateTime.Parse(mDT.Rows[0]["SellerQuotDate"].ToString()).ToString("yyyy-MM-dd");
mPO.RequisitionDate = DateTime.Parse(mDT.Rows[0]["RequisitionDate"].ToString()).ToString("yyyy-MM-dd");
mPO.DeliveryDate = DateTime.Parse(mDT.Rows[0]["DeliveryDate"].ToString()).ToString("yyyy-MM-dd");
mPO.ComparativeRefNo = mDT.Rows[0]["ComparativeRefNo"].ToString();
mPO.TermsOfDelivery = mDT.Rows[0]["TermsOfDelivery"].ToString();
mPO.TermsOfPayment = mDT.Rows[0]["TermsOfPayment"].ToString();
mPO.TermsOfConditions = mDT.Rows[0]["TermsOfConditions"].ToString();
mPO.IsClosed = Convert.ToBoolean(mDT.Rows[0]["IsClosed"].ToString());
mPO.IsCancelled = Convert.ToBoolean(mDT.Rows[0]["IsCancelled"].ToString());
mPO.UserName = Session["AgentName"].ToString();
mPO.WithItemCode =Convert.ToBoolean(mDT.Rows[0]["WithItemCode"].ToString());
mPO.ValidateQty = Convert.ToBoolean(mDT.Rows[0]["ValidateQty"].ToString());
con.Close();
return mPO;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
Jqgrid
<script type="text/javascript">
jQuery(document).ready(function ($) {
// master grid
var $grid = $("#jqGrid");
$grid.jqGrid({
url: '@Url.Action("POGrid")',
datatype: "json",
jsonReader: { id: 'PO_ID' },
colModel: [
{ name: 'PO_ID', index: 'PO_ID', label: 'PO ID', hidden: true, editable: true, editable: "disabled", align: "center", width: 12 },
{ name: 'Comp_ID', index: 'Comp_ID', label: 'Comp ID', hidden: true, editable: true, searchoptions: { sopt: ['cn'] }, align: "left", width: 2 },
{ name: 'GL_Year', index: 'GL_Year', label: 'GL Year', hidden: true, editable: true, searchoptions: { sopt: ['cn'] }, align: "left", width: 40 },
{
name: 'PODate', index: 'PODate', label: 'PO Date', width: 100,
editable: true, edittype: 'text',
editoptions: {
size: 10, maxlengh: 10,
dataInit: function (element) {
$(element).datepicker({ dateFormat: 'yy/mm/dd' })
}
}
},
{ name: 'PartyName', index: 'PartyName', label: 'Party', width: 200, editable: true },
{ name: 'SellerQuotRefNo', index: 'SellerQuotRefNo', label: 'S Q RefNo', width: 100, editable: true },
{
name: 'SellerQuotDate', index: 'SellerQuotDate', label: 'S Q Date', width: 100,
editable: true, edittype: 'text',
editoptions: {
size: 10, maxlengh: 10,
dataInit: function (element) {
$(element).datepicker({ dateFormat: 'yy/mm/dd' })
}
}
},
{
name: 'RequisitionDate', index: 'RequisitionDate', label: 'R Date', width: 100,
editable: true, edittype: 'text',
editoptions: {
size: 10, maxlengh: 10,
dataInit: function (element) {
$(element).datepicker({ dateFormat: 'yy/mm/dd' })
}
}
},
{
name: 'DeliveryDate', index: 'DeliveryDate', label: 'D Date', width: 100,
editable: true, edittype: 'text',
editoptions: {
size: 10, maxlengh: 10,
dataInit: function (element) {
$(element).datepicker({ dateFormat: 'yy/mm/dd' })
}
}
},
{ name: 'ComparativeRefNo', index: 'ComparativeRefNo', label: 'C RefNo', width: 100, editable: true },
{ name: 'TermsOfConditions', index: 'TermsOfConditions', label: 'Terms Conditions', hidden:true, width: 250, editable: true },
{ name: 'TermsOfDelivery', index: 'TermsOfDelivery', label: 'Terms Delivery', width: 250, editable: true },
{ name: 'TermsOfPayment', index: 'TermsOfPayment', label: 'Terms Payment', width: 250, editable: true },
{
name: 'IsPending', index: 'IsPending', width: 100, label: 'Pending',
editable: true, edittype: 'checkbox', editoptions: { value: "True:False" },
formatter: "checkbox", formatoptions: { disabled: false }
},
{
name: 'IsClosed', index: 'IsClosed', label: 'Closed', width: 60,
editable: true, edittype: 'checkbox', editoptions: { value: "True:False" },
formatter: "checkbox", formatoptions: { disabled: false }
},
{
name: 'WithItemCode', index: 'WithItemCode', width: 100, label: 'With Item Code',
editable: true, edittype: 'checkbox', editoptions: { value: "True:False" },
formatter: "checkbox", formatoptions: { disabled: false }
},
{
name: 'ValidateQty', index: 'ValidateQty', label: 'Validate Qty', width: 100,
editable: true, edittype: 'checkbox', editoptions: { value: "True:False" },
formatter: "checkbox", formatoptions: { disabled: false }
},
{
name: 'PO_ID', label: '', search: false, width: '40', frozen: true , formatter: function (cellvalue, options, rowObject, rowdata) {
var rowID = rowObject['PO_ID'];
if (rowID != null) {
return '<a href="javascript:POGetby(' + rowID + ')" style="color: #3366ff" id="' + rowID + '" >Edit</a>';
}
}
},
],
autowidth: true,
shrinkToFit: false,
navOptions: {
reloadGridOptions: { fromServer: true }
},
rowNum: 20,
loadonce: true,
height: '250',
viewrecords: true,
caption: 'Purchase Order',
emptyrecords: 'No Records are Available to Display',
pager: "#jqGridPager",
onSelectRow: function (ids) {
if (ids != null) {
var data = $("#jqGrid").getLocalRow(ids);
$("#jqGridDetails").jqGrid('setGridParam', { url: "/Home/PODetailGrid/" + data.PO_ID, datatype: 'json' });
$("#jqGridDetails").jqGrid('setCaption', 'Purchase Order Detail For :: <b style="color: Red">' + data.PartyName + '</b>');
$("#jqGridDetails").trigger("reloadGrid");
}
},
onSortCol: clearSelection,
});
});
javascript
function POGetby(PO_ID) {
$.ajax({
url: "/Home/POMain_Select",
type: "GET",
contentType: "application/json;charset=UTF-8",
data: { 'PO_ID': PO_ID },
dataType: "json",
success: function (result) {
$('#POUID').val(result.PO_ID);
$('#POUNO').val(result.PONo);
$('#POUDate').val(result.PODate);
$('#RequisitionUDate').val(result.RequisitionDate);
$('#DeliveryUDate').val(result.DeliveryDate);
$('#IsUCancelled').val(result.IsCancelled);
$('#IsUClosed').val(result.IsClosed);
$('#IsUPending').val(result.IsPending);
$('#PartyUCode').val(result.PartyCode);
$('#SellerUQuotRefNo').val(result.SellerQuotRefNo);
$('#SellerUQuotDate').val(result.SellerQuotDate);
$('#ComparativeURefNo').val(result.ComparativeRefNo);
$('#RUtrue').val(result.WithItemCode);
$('#RUfalse').val(result.WithItemCode);
$('#ValidateUQty').val(result.ValidateQty);
$('#UTermsOfDelivery').val(result.TermsOfDelivery);
$('#UTermsOfPayment').val(result.TermsOfPayment);
$('#UTermsOfConditions').val(result.TermsOfConditions);
$('#UpdateModel').modal('show');
$('#UpdatePO').show();
},
error: function (errormessage) {
alert(errormessage.responseText);
}
});
return false;
}
答案 0 :(得分:0)
如果您使用@Html.CheckBoxFor(m => m.IsCancelled, new { @id = "IsUCancelled" })
并且Model.IsCancelled
为true,则MVC实际上会发出两个输入元素:
<input id="IsUCancelled" name="IsCancelled" type="checkbox" value="true">
<input name="IsCancelled" type="hidden" value="false">
这是由MVC完成的,因为未选中的复选框不会作为false
回发,但是无论如何ModelBinder都希望将IsCancelled
绑定到布尔值。因此,第二个隐藏的输入将以始终设置为false
的相同名称发送。
如您所见,用new { @id = "IsUCancelled" }
覆盖的ID仅用于复选框,而不是隐藏输入。
所以我的猜测是您的JS代码只会更改可见复选框的值,而不会更改隐藏输入的值。由于表单数据是使用name
属性发送的,因此您的帖子数据可能不包含您期望的值。
最重要的是:请勿覆盖该ID,否则会造成混乱。