Webapi+ef (Increase and deletion check)

Source: Internet
Author: User
Tags tojson connectionstrings

First, model establishes the ADO entity model.

The second part, controller to establish and delete the method of modification

public static Httpresponsemessage ToJson (Object obj)
{
String str;
if (obj is String | | obj is CHAR)
{
str = obj. ToString ();
}
Else
{
JavaScriptSerializer serializer = new JavaScriptSerializer ();
str = serializer. Serialize (obj);
}
Httpresponsemessage result = new Httpresponsemessage {Content = new stringcontent (str, encoding.getencoding ("UTF-8"), " Application/json ")};
return result;
}


Public Httpresponsemessage Get ()
{

var JG = Db.user.ToList ();
Return ToJson (JG);

}

protected override void Dispose (bool disposing)
{
Db. Dispose ();
Base. Dispose (disposing);
}

Connect database: vs interface to the left of Server Explorer, connect to database, modify or add in Web. config

<connectionStrings>
<add name= "defaultconnection" providername= "System.Data.SqlClient" connectionstring= "Data source= (LocalDb) \ V11.0;initial catalog=aspnet-webapitest1-20170420100509;integrated Security=sspi; attachdbfilename=| Datadirectory|\aspnet-webapitest1-20170420100509.mdf "/>
<add name= "testdbentities" connectionstring= "metadata=res://*/models.model1.csdl|res://*/models.model1.ssdl| Res://*/models.model1.msl;provider=system.data.sqlclient;provider connection String=&quot;data source=.; Initial catalog=testdb;persist security Info=true;user Id=sa; password=123456; Multipleactiveresultsets=true; app=entityframework&quot; "providername=" System.Data.EntityClient "/>
</connectionStrings>

Part III: View View page index.cshtml

@{
Layout = null;
}
<script src= "~/scripts/jquery-1.7.1.min.js" ></script>
<table id= "customertable" border= "0" cellpadding= "3" >
<tr>
<th>UserID</th>
<th>login</th>
<th>pwd</th>
<th>Actions</th>
</tr>
<tr>
<td>
<input type= "text" id= "Txtcustomerid" size= "5"/>
</td>
<td>
<input type= "text" id= "Txtcompanyname"/>
</td>
<td>
<input type= "text" id= "Txtcontactname"/>
</td>
<td>
<input type= "button" Name= "Btninsert" value= "Insert"/>
</td>
</tr>
</table>
<script type= "Text/javascript" >
$ (function () {
$.getjson ("Api/user", loadcustomers);
});
function LoadCustomers (data) {
$ ("#customerTable"). Find ("TR:GT (1)"). Remove ();
$.each (data, function (key, Val) {
var tableRow = ' <tr> ' + ' <td> ' + val.userid + ' </td> ' +
' <td><input type= ' text "value=" ' + Val.login + ' "/></td> ' +
' <td><input type= ' text "value=" ' + val.pwd + ' "/></td> ' +
' <td><input type= ' button ' name= ' btnupdate ' value= ' modify '/> ' +
' <input type= ' button ' name= ' btndelete ' value= ' delete '/></td> ' +
' </tr> ';
$ (' #customerTable '). Append (TableRow);
});
$ ("Input[name= ' Btninsert ']"). Click (OnInsert);
$ ("Input[name= ' btnupdate ']"). Click (OnUpdate);
$ ("Input[name= ' Btndelete ']"). Click (OnDelete);
}
function OnInsert () {
var userId = $ ("#txtCustomerId"). Val ();
var login = $ ("#txtCompanyName"). Val ();
var pwd = $ ("#txtContactName"). Val ();
var data = ' {' userid ': ' + userid + ', ' login ': ' + login + ', ' pwd ': ' + pwd + '}} ';
$.ajax ({
Type: ' POST ',
URL: '/api/user ',
Data:data,
ContentType: "Application/json; Charset=utf-8 ",
DataType: ' JSON ',
Success:function (Result) {
$ ("#txtCustomerId"). Val (');
$ ("#txtCompanyName"). Val (');
$ ("#txtContactName"). Val (');
$ ("#txtCountry"). Val (');
$.getjson ("Api/user", loadcustomers);
Alert (' Customer Added! ');
}
}). Fail (
function (XHR, textstatus, err) {
Alert (' Add failed for the following reason: ' + err);
});

}
function OnUpdate () {

var userId = $ (this). Parent (). Parent (). Children (). Get (0). InnerHTML;

var login = $ (this). Parent (). Parent (). Children (). Get (1)). Find ("Input"). Val ();

var pwd = $ (this). Parent (). Parent (). Children (). Get (2)). Find ("Input"). Val ();


var data = ' {' userid ': ' + userid + ', ' login ': ' + login + ', ' pwd ': ' + pwd + '}} ';

$.ajax ({
Type: ' PUT ',
URL: '/api/user/' + userId,
Data:data,
ContentType: "Application/json; Charset=utf-8 ",
DataType: ' JSON ',
Success:function (Results) {
$.getjson ("Api/user", loadcustomers);
Alert (' Customer Updated! ');
}
}). Fail (function (XHR, textstatus, err) {
Alert (' Failed update! The reason is: ' + err);
});
}

function OnDelete () {
var userId = $ (this). Parent (). Parent (). Children (). Get (0). InnerHTML;
$.ajax ({
Type: ' DELETE ',
URL: '/api/user/' + userId,
ContentType: "Application/json; Charset=utf-8 ",
DataType: ' JSON ',
Success:function (Results) {
$.getjson ("Api/user", loadcustomers);
Alert (' Customer deleted! ');
}
}). Fail (function (XHR, textstatus, err) {
Alert ("Delete Error! The reason is: "+ err);
});

}
</script>

Webapi+ef (Increase and deletion check)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.