‏إظهار الرسائل ذات التسميات Object. إظهار كافة الرسائل
‏إظهار الرسائل ذات التسميات Object. إظهار كافة الرسائل

الخميس، 26 ديسمبر 2013

how to pass complex object to Web API method?

i have two models :-

public class Builder { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int BuilderId { get; set; } public string BuilderName { get; set; } }public class Project{ [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int ProjectId { get; set; } [Required] public string ProjectName { get; set; } [Required] public string Location { get; set; } [Required] public int BuilderId { get; set; } public Builder builder { get; set; }}

When i am trying to post the builder class object from client side to the web api method, then it is reading the object from body. there is no problem in that. but when i am trying to add any Project with some Builder then it is not calling to desired method. and my Web Method :-

public HttpResponseMessage PostProject([FromUri] Project project) { if (ModelState.IsValid) { db.Projects.Add(project); db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, project); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = project.ProjectId })); return response; } else { return Request.CreateResponse(HttpStatusCode.BadRequest); } }

my method to call the web api method :-

public async Task Create(Project projects) { try { HttpClient client = new HttpClient(); JavaScriptSerializer serializer = new JavaScriptSerializer(); String contentStr = serializer.Serialize(projects); ObjectContent content = new ObjectContent(typeof(Project), projects, new JsonMediaTypeFormatter()); HttpResponseMessage putResponse = await client.PostAsync(serviceprovider + "project/postproject", content); putResponse.EnsureSuccessStatusCode(); return RedirectToAction("Index"); } catch (HttpRequestException e) { return View(); } }

As per my knowledge, i think it is creating problem because my object is complex. I am new to Web API any suggestions from you are welcome. Please help.


View the original article here

الثلاثاء، 24 ديسمبر 2013

C++ Multiple Parameter Object creation

I am a virtual beginner in programming and am trying to understand a simple part of Object oriented programming. I apologize in advance if the answer to my question seems obvious, but I have tried to find similar examples online using google and youtube but sadly nothing fits the bill. With that said here is my question.

I am trying to make a simple video game inventory similar to the ones you would see in RPGs such as Final Fantasy VI or Chrono Trigger. What I am having trouble with is creating the items of the inventory. What I'd like help with is how to create one object that defines one item, for example a potion. I know it will take multiple parameters which are listed here:

itemText = "Potion";itemDesc = "Regain 50 HP";itemEffect = playerHP + 50;buyCost = 50;sellCost = 25;

What I'd like help with is syntax of the connection of these parameters in one object. Later I will use the data so that I can create an array that shows only the itemText and a Quantity for the player to see. Any and all help or places would be appreciated.

Thank you in advance, Delita


View the original article here