.net核心类以获取参数并作为json返回

时间:2018-12-17 10:02:25

标签: asp.net json asp.net-mvc asp.net-core

在某些情况下,我应该从MVC网页获取参数,例如: DateFrom,DateTo,Contract和Database,并以JSON格式返回。

我需要在.NET Core中编写该类。 任何帮助,因为我仅在ASP .net

中工作
public class FilterController : Controller
{
    private readonly IServiceProvider _serviceProvider;
    public FilterController(IServiceProvider serviceProvider)
    {
        _serviceProvider = serviceProvider;
    }

    public IActionResult Index([FromServices]IConnectionStringService service, int id)
    {
        List<ConnectionStringDto> list = new List<ConnectionStringDto>();

        if (service != null)
        {
            list = service.GetConnectionStringList(id);
            if (id > 0)
            {
                list.First(x => x.ConnectionStringId == id).IsSelected = true;
            }
        }
        ModelState.Clear();
        return View(list);
    }

    [HttpPost]
    public IActionResult RunFilter([FromServices]IConnectionStringService service, List<ConnectionStringDto> list)
    {
        list = new List<ConnectionStringDto>(); 
        return View(list);
    }
}

来自过滤器的代码,应该在哪里。表格尚未完成。

1 个答案:

答案 0 :(得分:1)

您可以返回JsonResult

    [HttpGet]
    [Route("GetDate")]
    public JsonResult GetDate(DateTime DateFrom, DateTime DateTo)
    {
        return Json(new { DateFrom = DateFrom, DateTo = DateTo });
    }