Home > Net >  Add or delete check change
Add or delete check change

Time:11-09

The most used in MVC is to add or delete check change, is to increase, click on the button to achieve the effect of adding,
1, in view of the page to add the button an ID, by ID to get the button's click event (add passengers)
$(" # btnAddPassenger "). Click (function () {})
2, add passenger information
Var passengerInfors=[];
For (var I=1; I & lt;=passengerNo; I++) {
Var passenger={};
$. Each ($(" # formPassenger "+ I) serializeArray (), function (index, item) {
Passenger [item name]=item. The value; });
PassengerInfors. Push (passenger); }
3, to verify the passenger information,
//determine whether name fill out
If (passengerInfors [I] passengerName==undefined | | passengerInfors [I] passengerName=="") {
MyAlert (" please fill out the first [" + (I + 1) + "] a passenger's name!" );
return; }
4, send the request
Var layerIdex=layer. The load ();
$.post (" addPNRPassenger, "
{PNRID: PNRID, passengerInfors: passengerInfors}, function (jsonData) {
Layer. The close (layerIdex);
MyAlert (jsonData. Text);
If (jsonData. State) {
Window. The location. Href="https://bbs.csdn.net/topics/pnrInfoShow? PNRID="+ PNRID; }
})
5, the new passenger information in the controller, open transaction, save to the database, to commit the transaction, return data
List//save to the database
MyModel. B_PNRPassenger. AddRange (listPassengers);
MyModel. SaveChanges ();
//to commit the transaction
Scope.Com plete ();
//return data
RtMsg. State=true;
RtMsg. Text="add passengers success!" ;

Delete, it is deleted, click the delete button can delete a effect,
1, obtain the delete button click event
$(" # btnDelete "). Click (function () {})
2, check the passenger information
Var passengerIds=[];
CAdultNum=0;
$(" input [name='passenger'] : checked "). Each (function () {
PassengerIds. Push ($(this). The data (" passenger - id "));
If ($(this). The data (" passenger - type - id ")==1) {
CAdultNum++; }
});
3, submit a request
Sure you want to delete the selected layer. Confirm (" passenger? (this operation cannot be withdrawn), "{icon: 3, the title: 'tip'}, function (index) {
layer.close(index);
//open load layer
Var layIndex=layer. The load ();
$.post (" deletePassenger, "
{PNRID: PNRID, passengerIds: passengerIds}, function (jsonData) {
Layer. The close (layIndex);
MyAlert (jsonData. Text);
If (jsonData. State) {
Window. The location. Href="https://bbs.csdn.net/topics/pnrInfoShow? PNRID="+ PNRID; }
});
});
4, query the passengers need to delete the information,
ListThe from tbPassenger in myModel. B_PNRPassenger
Where passengerIds. The Contains (tbPassenger. PNRPassengerID)
The select tbPassenger). ToList ();
5, remove the passenger information
MyModel. B_PNRPassenger. RemoveRange (passengerInfor);
MyModel. SaveChanges ();
6, save the changes, commit the transaction, return data
MyModel. Entry (dbPassengers [I]) State=System. Data. The Entity. The EntityState. Modified;
MyModel. SaveChanges ();
7, commit the transaction
Scope.Com plete ();
8, return the data
RtMsg. State=true;
RtMsg. Text="passenger information deleted successfully!" ;

Checked, is looking for, click the query button to achieve the effect of a lookup,
1, in the view access query button click event
$(" # btnSearchPNR "). Click (function () {
//get the query condition
Var pnrNo=$(" # searchPNR "). Val ();
//table data overloading
TabPNR. Reload ({
Where: {PNRNo: PNRNo},
Page: {curr: 1}
});
});
2, to the controller league table query out the needed data, data filtering
if (! String. IsNullOrEmpty (PNRNo))
{tempPNRInfor=tempPNRInfor. Where (p=& gt; P.P NRNo==PNRNo); }
Three, constructs the return data
LayuiTableData LayuiTableData=https://bbs.csdn.net/topics/new layuiTableData
{count=count, data=https://bbs.csdn.net/topics/listPNR};

Change, is modified, click the modify button to achieve the effect of a lookup,
Passenger name with the page 1, judge database mail come over whether the passenger name, passengers under the condition of the same ID
If (dbPassenger. PNRPassengerID==passengerInfor. PNRPassengerID & amp; &
DbPassenger passengerName!=passengerInfor. PassengerName)
{
2, to determine whether a page passenger name is empty
if (! String. IsNullOrEmpty (passengerInfor. PassengerName))
3, modify passengers table data
DbPassenger. PassengerName=passengerInfor. PassengerName;
4, save the changes
MyModel. Entry (dbPassenger). State=System. Data. The Entity. The EntityState. Modified;
MyModel. SaveChanges ();
5, to commit the transaction
Scope.Com plete ();
6, return the data
RtMsg. State=true;
RtMsg. Text="passenger name modify success!" ;
  • Related