简体   繁体   中英

Consuming REST API using razor MVC .net

I'm creating rest api MVC, to make model i'm using entity framework and to make my controller i'm using scaffolding item, i have my views(razor) but i dont know how to get data o how to post data from my view to my controller.

Somebody help me please!

Model

public partial class Information
{
    public int ID { get; set; }
    public string name { get; set; }
    public string address { get; set; }
    public string phone { get; set; }
    public string Email { get; set; }
}

Controller

private BaseNegociosEntities db = new BaseNegociosEntities();

// GET api/Informacion
public IEnumerable<Information> GetInformations()   
{
    return db.Information.AsEnumerable();


public HttpResponseMessage PostInformacion(Informacion informacion)
{
    if (ModelState.IsValid)
    {
        db.Informacion.Add(informacion);
        db.SaveChanges();

        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, information);
        response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = information.Id_Informacion }));
        return response;
    }
    else
    {
        return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
    }
}

You have to create list in model.

           List<Information> list1=new List<Information>{get;set;}

Then need to feed data into that list from controller. So you can send your Model's list to view. Then you can serialize your list to xml or json.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM