[英]Unable to post the required fields into the database
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using MvcApplication4.Models;
namespace MvcApplication4.Controllers
{
public class DemoController : ApiController
{
private DemoEntities db = new DemoEntities();
// GET api/Demo
public IEnumerable<Emp_details> GetEmp_details()
{
return db.Emp_details.AsEnumerable();
}
// GET api/Demo/5
public Emp_details GetEmp_details(int id)
{
Emp_details emp_details = db.Emp_details.Find(id);
if (emp_details == null)
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
}
return emp_details;
}
// PUT api/Demo/5
public HttpResponseMessage PutEmp_details(int id, Emp_details emp_details)
{
if (!ModelState.IsValid)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}
if (id != emp_details.Id)
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
db.Entry(emp_details).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException ex)
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
}
return Request.CreateResponse(HttpStatusCode.OK);
}
// POST api/Demo
public HttpResponseMessage PostEmp_details(Emp_details emp_details)
{
if (ModelState.IsValid)
{
//Emp_details empobject = new Emp_details();
//empobject.Id = Convert.ToInt32(val[0]);
//empobject.name = val[1];
//empobject.Age = Convert.ToInt32(val[2]);
db.Emp_details.Add(emp_details);
db.SaveChanges();
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, emp_details);
response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = emp_details }));
return response;
}
else
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}
}
// DELETE api/Demo/5
public HttpResponseMessage DeleteEmp_details(int id)
{
Emp_details emp_details = db.Emp_details.Find(id);
if (emp_details == null)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
db.Emp_details.Remove(emp_details);
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException ex)
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
}
return Request.CreateResponse(HttpStatusCode.OK, emp_details);
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
I need to post only particular fields' values into the database not all the values. 我只需要将特定字段的值发布到数据库中,而不是所有值。 My database values contain
Id
, name
, Age
. 我的数据库值包含
Id
, name
, Age
。 I need to post only Id
and name
but not Age
. 我只需要发布
Id
和name
,而不发布Age
。 Is that possible. 那可能吗。 Please help me out.
请帮帮我。 and Also i need to post the values into different tables not to the single table.Please help me out fast i need a working code for this
并且我还需要将值发布到不同的表中而不是单个表中。请快速帮助我,我需要一个有效的代码
create an object like : 创建一个像这样的对象:
var post_object = new Emp_details
{
id= "Updated ID",
name = "Updated Name"
};
And post this object to your controller.You'll receive this object with Age
set to it's default value(null if a Nullable
type) and rest of the fields will have the new data/value(s). 并将此对象发布到您的控制器。您将收到
Age
设置为默认值的对象(如果为Nullable
类型,则为null),其余字段将具有新的数据/值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.