c# - How to pass model object to Web API Post method -


i want save data database in mvc application. have below class calls webapi1 service post method. here sample model object. have view uses sample object. want inert sample object data db using service call.

public sampletestcreate(sample) {     string uri = baseuri + "test";     using (httpclient httpclient = new httpclient())     {         task<httpresponsemessage> response = httpclient.postasjsonasync(uri, new stringcontent(jsonconvert.serializeobject(test)));       //  task<httpresponsemessage> response = httpclient.postasjsonasync(uri, test);         var data = response.result.content.readasstringasync().result;        return jsonconvert.deserializeobjectasync<test>(response.result.content.readasstringasync().result).result;      } }  // post api/sample public httpresponsemessage postsample(sample sample) {     if (modelstate.isvalid)     {         db.samples.add(sample);         db.savechanges();          httpresponsemessage response = request.createresponse(httpstatuscode.created, sample);         response.headers.location = new uri(url.link("defaultapi", new { id = sample.sampleid }));         return response;     }     else     {         return request.createresponse(httpstatuscode.badrequest);     } } 

here service post method getting called , "sample" objects properties values coming null. model object binding not happening.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -