使用razor MVC.net使用REST API
本文关键字:使用 API REST MVC razor net | 更新日期: 2023-09-27 18:25:57
我正在创建rest api MVC,为了使模型使用实体框架,为了使控制器使用脚手架项,我有自己的视图(剃刀),但我不知道如何获取数据,也不知道如何将数据从视图发布到控制器。
谁来帮帮我!
型号
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; }
}
控制器
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);
}
}
您必须在模型中创建列表。
List<Information> list1=new List<Information>{get;set;}
然后需要将数据从控制器馈送到该列表中。因此,您可以将模型的列表发送到视图。然后,您可以将列表序列化为xml或json。