I am somewhat new to working with MVC Web Api and I was excited to get my MVC Web Api GET to work, but really frustrated trying to formulate my Web Api Put
I tried to create my dataset to be identical to the the but I did add one property, could that be an issue? Also, I noticed that some of my values on my checkboxes and such are returning nulls these are set up in the model as boolean, so I don't know.
Here is my Web Api put
// PUT: api/Datasets/5
[ResponseType(typeof(void))]
public async Task<IHttpActionResult> PutDataset(int id, Dataset dataset)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != dataset.Dataset_ID)
{
return BadRequest();
}
db.Entry(dataset).State = EntityState.Modified;
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!DatasetExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
Here is my HttpPost from my form with my datamodel
[HttpPost]
public ActionResult TestDataset(Dataset dataset)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:4251/");
//HTTP POST
var postTask = client.PostAsJsonAsync<Dataset>("api/datasets/1", dataset);
postTask.Wait();
var result = postTask.Result;
if (result.IsSuccessStatusCode)
{
return RedirectToAction("Index");
}
}
ModelState.AddModelError(string.Empty, "Server Error. Please contact administrator.");
return View(dataset);
}
This is my dataset.cs created from Entity Framework on my Web Api Project
public partial class Dataset
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Dataset()
{
this.Attachment = new HashSet<Attachment>();
this.Contact = new HashSet<Contact>();
this.Location1 = new HashSet<Location>();
}
public int Dataset_ID { get; set; }
public int Category_ID { get; set; }
public string Provider { get; set; }
public string Name { get; set; }
public string Institution { get; set; }
public string Description { get; set; }
public string Location { get; set; }
public Nullable<bool> Domestic { get; set; }
public Nullable<bool> International { get; set; }
public Nullable<bool> Includes_PHI { get; set; }
public Nullable<bool> PHI_Limited { get; set; }
public Nullable<bool> Includes_PIL { get; set; }
public Nullable<bool> PIL_Limited { get; set; }
public Nullable<bool> Citation_Requirements { get; set; }
public string Citation_Comments { get; set; }
public Nullable<System.DateTime> Availability_Beg_DT { get; set; }
public Nullable<System.DateTime> Availability_End_DT { get; set; }
public Nullable<bool> Subscription_Renewal { get; set; }
public Nullable<System.DateTime> Subscription_Renewal_DT { get; set; }
public Nullable<bool> Retention_Expiry { get; set; }
public Nullable<System.DateTime> Retention_Expiry_DT { get; set; }
public Nullable<bool> Data_Destruction { get; set; }
public Nullable<System.DateTime> Data_Destruction_DT { get; set; }
public string Data_Destruction_Instructions { get; set; }
public Nullable<int> Contract_ID { get; set; }
public bool Draft_Status { get; set; }
public bool Admin_Only { get; set; }
public string Dataset_Alias { get; set; }
public Nullable<bool> Loc_Amazon { get; set; }
public Nullable<bool> Loc_IT_Research { get; set; }
public Nullable<bool> Loc_Research_Proj { get; set; }
public Nullable<bool> Loc_Secure_Data { get; set; }
public Nullable<bool> Loc_Mercury { get; set; }
public Nullable<bool> Loc_Research_CC { get; set; }
public Nullable<bool> Loc_Research_VM { get; set; }
public Nullable<bool> Loc_External { get; set; }
public Nullable<bool> Access_Url { get; set; }
public Nullable<bool> Access_Download_App { get; set; }
public Nullable<bool> Access_Lab_Terminal { get; set; }
public Nullable<bool> Access_Email_Req { get; set; }
public Nullable<bool> Access_File_Download { get; set; }
public Nullable<bool> Access_Other { get; set; }
public string Location_Notes { get; set; }
public string Access_Notes { get; set; }
public Nullable<System.DateTime> Date_Added { get; set; }
public Nullable<System.DateTime> Date_Modified { get; set; }
public string Added_By_User { get; set; }
public string Updated_By_User { get; set; }
public Nullable<bool> External_Collaborators { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Attachment> Attachment { get; set; }
public virtual Category Category { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Contact> Contact { get; set; }
public virtual Contract Contract { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Location> Location1 { get; set; }
}
this is the model I am returning:
public class Dataset
{
public int Dataset_ID { get; set; }
public int Category_ID { get; set; }
public string Provider { get; set; }
public string Name { get; set; }
public string Institution { get; set; }
public string Description { get; set; }
public string Location { get; set; }
public Nullable<bool> Domestic { get; set; }
public Nullable<bool> International { get; set; }
public Nullable<bool> Includes_PHI { get; set; }
public Nullable<bool> PHI_Limited { get; set; }
public Nullable<bool> Includes_PIL { get; set; }
public Nullable<bool> PIL_Limited { get; set; }
public Nullable<bool> Citation_Requirements { get; set; }
public string Citation_Comments { get; set; }
public Nullable<System.DateTime> Availability_Beg_DT { get; set; }
public Nullable<System.DateTime> Availability_End_DT { get; set; }
public Nullable<bool> Subscription_Renewal { get; set; }
public Nullable<System.DateTime> Subscription_Renewal_DT { get; set; }
public Nullable<bool> Retention_Expiry { get; set; }
public Nullable<System.DateTime> Retention_Expiry_DT { get; set; }
public Nullable<bool> Data_Destruction { get; set; }
public Nullable<System.DateTime> Data_Destruction_DT { get; set; }
public string Data_Destruction_Instructions { get; set; }
public Nullable<int> Contract_ID { get; set; }
public bool Draft_Status { get; set; }
public bool Admin_Only { get; set; }
public string Dataset_Alias { get; set; }
public Nullable<bool> Loc_Amazon { get; set; }
public Nullable<bool> Loc_IT_Research { get; set; }
public Nullable<bool> Loc_Research_Proj { get; set; }
public Nullable<bool> Loc_Secure_Data { get; set; }
public Nullable<bool> Loc_Mercury { get; set; }
public Nullable<bool> Loc_Research_CC { get; set; }
public Nullable<bool> Loc_Research_VM { get; set; }
public Nullable<bool> Loc_External { get; set; }
public Nullable<bool> Access_Url { get; set; }
public Nullable<bool> Access_Download_App { get; set; }
public Nullable<bool> Access_Lab_Terminal { get; set; }
public Nullable<bool> Access_Email_Req { get; set; }
public Nullable<bool> Access_File_Download { get; set; }
public Nullable<bool> Access_Other { get; set; }
public string Location_Notes { get; set; }
public string Access_Notes { get; set; }
public Nullable<System.DateTime> Date_Added { get; set; }
public Nullable<System.DateTime> Date_Modified { get; set; }
public string Added_By_User { get; set; }
public string Updated_By_User { get; set; }
public Nullable<bool> External_Collaborator { get; set; }
public ICollection<Attachment> Attachment { get; set; }
public ICollection<Contact> Contact { get; set; }
public ICollection<Category> Categories { get; set; }
public IEnumerable<SelectListItem> CategoryList { get; set; }
}
Aucun commentaire:
Enregistrer un commentaire