今天遇到一个问题,在element ui向后台传递日期过程中,传递的时间是2020-08-05 16:02:35,接收的却是2020-08-05 20:02:35
解决办法:
1.后台安装包Microsoft.AspNetCore.Mvc.NewtonsoftJson
2.Startup文件中函数public void ConfigureServices(IServiceCollection services)中加上代码:
services.AddControllers().AddNewtonsoftJson();
问题解决
贴上代码:
<template>
<div class="app-container">
<el-form ref="form" :model="form" label-width="120px">
<el-form-item label="用户名">
<el-input v-model="form.username" />
</el-form-item>
<el-form-item label="添加时间">
<el-date-picker
v-model="form.addtime"
type="datetime"
value-format="yyyy-MM-dd HH:mm:ss"
placeholder="选择日期时间"
></el-date-picker>
</el-form-item>
<el-form-item label="数量">
<el-input
type="text"
oninput="value=value.replace(/[^\d]/g,'')"
maxlength="9"
v-model="form.num1"
placeholder="请输入数量"
clearable
></el-input>
</el-form-item>
<el-form-item style="text-align:center">
<el-button type="primary" @click="add();">确定添加</el-button>
</el-form-item>
</el-form>
</div>
</template>
<script>
import { add } from "@/api/test";
import { parseTime } from "@/utils";
export default {
data() {
return {
form: {
username: "",
num1: 0,
pickerOptions: {
shortcuts: [
{
text: "今天",
onClick(picker) {
picker.$emit("pick", new Date());
},
},
{
text: "昨天",
onClick(picker) {
const date = new Date();
date.setTime(date.getTime() - 3600 * 1000 * 24);
picker.$emit("pick", date);
},
},
{
text: "一周前",
onClick(picker) {
const date = new Date();
date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
picker.$emit("pick", date);
},
},
],
},
},
};
},
methods: {
add() {
// var addtime = this.form.date1 + " " + this.form.date2;
// addtime = "2020-08-05 14:38:26";
var params = {
username: this.form.username,
num1: parseInt(this.form.num1),
addtime: this.form.addtime,
};
add(params).then((res) => {
console.log("请求结果:", res);
alert(res.data);
});
},
},
};
</script>
<style scoped>
.line {
text-align: center;
}
</style>
后台:
[HttpPost("Add")]
public async Task<IpResponse> Add([FromBody] Dbtest model)
{
if (!ModelState.IsValid)
{
return IpResponse.Create(false, "100", ModelState.GetModelErrors());
}
var (result, message) = await _ItestStore.Add(model);
string code = CommonClass.GetReturnCode(message, "添加");
return IpResponse.Create(result, code, message);
}