我试图从表中选择并返回某些列,查询忽略选择并返回相关表中相关字段之外的所有列。 如何只返回选定的列?
这是我的API Controller
,
[Produces("application/json")]
[Route("api/Common")]
public class CommonController : Controller
{
private readonly ArtCoreDbContext _context;
public CommonController(ArtCoreDbContext context)
{
_context = context;
}
[HttpGet]
public IEnumerable<IdState> GetState()
{
return _context.IdState;
}
[HttpGet("{id}")]
public async Task<IActionResult> GetState([FromRoute] int id)
{
var L2EQuery = await (from i in _context.IdState
select new { Id = i.StaeId, i.text }).ToListAsync();
return Ok(L2EQuery);
}
}
更新
这是模型,
public class IdState
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity), Key]
public int StaeId { set; get; }
public int CouyId { set; get; }
[Display(Name = "State"), Required]
public string text { set; get; }
public ICollection<PatReg> State { get; set; }
public ICollection<IdCity> TblIdCity { get; set; }
}
,输出为,
[{
"staeId": 1,
"couyId": 2,
"text": "California",
"state": null,
"tblIdCity": null
}, {
"staeId": 2,
"couyId": 2,
"text": "WA",
"state": null,
"tblIdCity": null
}, {
"staeId": 3,
"couyId": 1,
"text": "Ajloun",
"state": null,
"tblIdCity": null
}, {
"staeId": 4,
"couyId": 1,
"text": "Amman",
"state": null,
"tblIdCity": null
}]
ajax
致电
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
<script src="~/ArtCoreScripts/Pages/ClinicCore/PatientRegistration.js"></script>
<script>
$(function () {
$.ajax({
type: "Get",
url: "/api/Common",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
console.log(response);
},
failure: function (response) {
alert(response.responseText);
},
error: function (response) {
alert(response.responseText);
}
});
});
</script>}
使用LINQPad 5我只获取所选列
答案 0 :(得分:3)
您的C#代码没有问题,但是JavaScript。
您没有在控制器中指定操作,因此默认为无参数HttpGet
启用的操作,该操作不会投影任何字段。所以问题出在你的AJAX调用中。像这样纠正:
$.ajax({
type: "Get",
// Pay attention on the next line !
url: "/api/Common/123",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
console.log(response);
},
failure: function (response) {
alert(response.responseText);
},
error: function (response) {
alert(response.responseText);
}
});
&#34; 123&#34;只是一个例子,但我可以看到,目前它并不重要,因为你不能过滤LINQ中的任何东西。但是,您必须在URL中指定它,以便可以将其解析为正确的方法。
答案 1 :(得分:2)
我认为,你调用方法public IEnumerable<IdState> GetState()
并返回整个表格。
也许,您需要致电Task<IActionResult> GetState([FromRoute] int id)
问题在这里:
url: "/api/Common",
你调用了错误的方法,添加参数:
url: "/api/Common/123",
或者也许:
$(function () {
$.ajax({
type: "Get",
url: "/api/Common",
data: {id: 123},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
console.log(response);
},
failure: function (response) {
alert(response.responseText);
},
error: function (response) {
alert(response.responseText);
}
});
});
答案 2 :(得分:0)
public class CommonController : Controller
{
private readonly ArtCoreDbContext _context;
public CommonController(ArtCoreDbContext context)
{
_context = context;
}
[HttpGet]
public IEnumerable<IdState> GetState()
{
return _context.IdState;
}
[HttpGet("{id}")]
public async Task<IActionResult> GetState([FromRoute] int id)
{
var L2EQuery = await (from i in _context.IdState
where i.test.equals("abc")
select new { Id = i.StaeId, text= i.text }).ToListAsync();
return Ok(L2EQuery);
}
}