Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am very new to mvc web api

I have crated a web api Post method which takes an object type "Bag" and return a HTMLString the code is as shown bellow

C#
public HtmlString PostBag(Bag bagofItem)
    {
       return  Utility.PostBagDiscountedItem(bagofItem);
    }


now from my web site i wanted to call the API method PostBag from the controller PostBag()

and i am do not know how to and appreciate if some one can show me how to do this

what i have got in my web application is as bellow

C#
public class HomeController : Controller
{
    private Bag _bag = new Bag();
    private string uri = "http://localhost:54460/";

 public ActionResult PostBag()
    {


          // would some one show me how to POST the _bag to API Method PostBag()

        return View();
    }



C#
public class Bag 
{
    private static  List<Product> _bag { get; set; }

    public List<Product> GetBag ()
    {
        if (_bag == null)
            _bag = new List<Product>();
       return _bag;
    }



}

appreciate all your help

thank
Posted
Comments
Jameel VM 25-Jul-13 7:21am    
why u are returning html string from webapi methods? you can return the json data from this method and can create html from the client side itself.

1 solution

You can post data in WebApi like below
C#
public class Bag
{
public string Name{get;set;}
}
var dataJSON = { bags: [{ Name: "Bag1", Name: "Bag2" }, 
                            { Name: "Bag4", Name: "Bag3" }] };
 $.ajax({
            type: 'POST',
            url: 'specify the url here',
            data: JSON.stringify(dataJSON),
            contentType: 'application/json; charset=utf-8',
            dataType: 'json'
        });



C#
public void Post(List<Bag> bags)
{
}

Hope this helps
 
Share this answer
 
v2
Comments
rushdy20 25-Jul-13 8:10am    
is that solution is passing a string what i am after is pass the bag object which is a List<product> as shown on my class. so how to Post the list of products in the Bag object
Jameel VM 25-Jul-13 8:47am    
i have update my answer.please try that

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900