我正在尝试将JSON数组发布到控制器操作方法并将其保存到数据库中。
这是控制器:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.8.0/js/bootstrap-datepicker.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/css/datepicker.css"/>
<input id="date">
<button onclick="getDate()">Get Date</button>
数据库有一个gitd表。
ajax调用:
public class Gitcontroller : Controller
{
public GithubContext _context;
public Gitcontroller(GithubContext context)//the database context
{
_context = context;
}
[HttpPost]
public IActionResult Updateto([FromBody] GitJSON gitjson)
{
try
{
if (ModelState.IsValid)
{
_context.AddRange(gitjson);
_context.SaveChanges();//adding list to table.
}
return View();
}
catch (Exception ex)
{
return RedirectToPage("Error", ex);
}
}
}
型号:
$.ajax({
type: 'POST',
url: 'http://localhost:60294/Git/Updateto',
contentType: 'application/json; charset=utf-8',
datatype: 'JSON',
data: obj,
success: function (data) {
alert('Post Succesful');
},
error: function (data) {
alert('error');
}
});
Json:
namespace Github.Models
{
public class gitd
{
public int ID { get; set; }
public string AvatarURL { get; set; }
public string Name { get; set; }
public decimal Score { get; set; }
public DateTime Updatedat { get; set; }
}
public class GitJSON
{
public List<gitd> gitdList { set; get; }
}
}
构建成功,但是出现500错误,提示无法加载资源。 另外,当我在Interactive中运行Updateto时,也会得到以下信息:
gitdList: [
{"AvatarURL":"https://avatars1.githubusercontent.com/u/7849225? v=4","Name":"simplenlg","Score":22.82041,"Updatedat":"2018-07-21T10:58:33Z"},
{"AvatarURL":"https://avatars2.githubusercontent.com/u/1123352?v=4","Name":"osgi.enroute","Score":7.6444883,"Updatedat":"2018-07-17T08:26:51Z"}
]
我正在使用的命名空间:
> (1,2): error CS0246: The type or namespace name 'HttpPostAttribute'
> could not be found (are you missing a using directive or an assembly
> reference?)
(1,2): error CS0246: The type or namespace name 'HttpPost'
> could not be found (are you missing a using directive or an assembly
> reference?)
(2,17): error CS0246: The type or namespace name
> 'IActionResult' could not be found (are you missing a using directive
> or an assembly reference?)
(2,41): error CS0246: The type or namespace
> name 'FromBodyAttribute' could not be found (are you missing a using
> directive or an assembly reference?)
(2,51): error CS0246: The type or
> namespace name 'GitJSON' could not be found (are you missing a using
> directive or an assembly reference?)
> + additional 6 errors
由脚手架生成的CRUD操作也存在于名为gitds的控制器中吗?它是否会影响结果?我不认为它已定义了控制器,请查看Gitcontroller动作Updateto,并在其中创建_context。 gitcontroller。
我该如何解决?500错误是什么意思?如何成功编译并编译出错误。
答案 0 :(得分:0)
从其存储库中提取OP的源。找到了一个与 let request = NSMutableURLRequest(url : url)
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
var rateValue:Double = 0.0;
if let error = error {
print(error)
} else {
if let unwrappedData = data {
let dataString = NSString(data: unwrappedData, encoding: String.Encoding.utf8.rawValue)
var stringSeperator = "<span class=\"ccOutputRslt\">"
if let contentArray = dataString?.components(separatedBy: stringSeperator){
if contentArray.count > 0 {
stringSeperator = "<span"
let newContentArray = contentArray[1].components(separatedBy: stringSeperator)
if newContentArray.count > 0 {
rateValue = Double(newContentArray[0])! + 10
}
}
}
}
}
//
print("Rate is \(rateValue)"); //Rate is 11.167
}
task.resume()
方法相关联的源不必要的select * from (
select column1, column2, row_number() OVER() AS rownum
from table
where flag = 0 )
where rownum < 1000
调用。推送了具有相关更改的PR。