无法将必填字段发布到数据库中

时间:2015-07-28 06:03:12

标签: c# .net

function downloadURI(uri, name){
    var link = document.createElement("a");
    link.download = name;
    link.href = uri;
    $("#data_cert").html(link);
    link.click();
    $("#data_cert").html("");
}

我只需要将特定字段的值发布到数据库中而不是所有值。我的数据库值包含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); } } } Idname。我只需发布AgeId,但不发布name。那可能吗。请帮帮我。而且我需要将值发布到不同的表中而不是单个表。请快速帮助我,我需要一个有效的代码

1 个答案:

答案 0 :(得分:0)

创建一个像:

的对象
var post_object = new Emp_details 
{
   id= "Updated ID",
   name = "Updated Name"
};

将此对象发布到您的控制器。您将收到此对象,Age设置为默认值(如果Nullable类型,则为null)和其他字段将有新的数据/值。