我试图使用Ajax将数据从视图发送到控制器,但数据以null形式传递到控制器。我尝试了很多方法,但无法找到解决方案。FromBody
属性没有解决问题,我在启动时进行了一些更改,但它们也没有帮助我。
以下是我的脚本:
$(document).ready(function() {
$("#addColumn").click(function() {
AddColumnRecords();
});
$("#saveRecords").click(function() {
SaveRecords();
});
});
function AddColumnRecords() {
var columnRecords="<tr><td>" + $("#columnName").val() + "</td><td>" + $("#type").val() +"</td><td>"+ document.getElementById('primaryCheck').checked+ "</td></tr>";
$("#ColumnTable").last().append(columnRecords);
$("#columnName").val(" ");
}
function SaveRecords() {
var Columns = new Array();
$("#ColumnTable").find("tr:gt(0)").each(function(){
var name=$(this).find("td:eq(0)").text();
var type=$(this).find("td:eq(1)").text();
var isPrimary=$(this).find("td:eq(2)").text();
var ColumnModel={};
ColumnModel.Name = name;
ColumnModel.Type = parseInt(type);
ColumnModel.IsPrimaryKey = isPrimary;
Columns.push(ColumnModel);
});
var Table={};
var tableName=$("#tableName").val();
Table.TableName=tableName;
Table.Columns=Columns;
console.log(JSON.stringify(Table));
$.ajax({
type:'POST',
dataType:'JSON',
contentType:'application/json; charset=utf-8',
url:'/table/createtable',
data:{table : JSON.stringify(Table)},
success:function(data){alert("success")},
error:function(){alert("error")}
});
}
我的表格如下:
public class Table
{
public string TableName { get; set; }
public List<Column> Columns { get; set; } = new List<Column>();
}
列模型类是:
public class Column
{
public string Name { get; set; }
public virtual Types Type { get; set; } = Types.String;
public virtual bool IsPrimaryKey { get; set; } = false;
}
这是动作方法:
[HttpPost]
public IActionResult CreateTable([FromBody] Table table)
{
return View();
}
<div class="mb-3">
<label for="tableName" class="form-label">Table Name</label>
<input type="text" class="form-control" id="tableName" aria-describedby="tableName">
</div>
<form>
<div class="mb-3">
<label for="columnName" class="form-label">Name</label>
<input type="text" class="form-control" id="columnName" aria-describedby="columnName">
</div>
<div class="mb-3">
<select class="form-select" aria-label="Default select example" id="type">
@{
<option value=3>@DatabaseSample.Models.Types.String</option>
<option value=0>@DatabaseSample.Models.Types.Integer</option>
<option value=5>@DatabaseSample.Models.Types.Boolean</option>
<option value=4>@DatabaseSample.Models.Types.Char</option>
<option value=2>@DatabaseSample.Models.Types.Double</option>
<option value=1>@DatabaseSample.Models.Types.Float</option>
<option value=6>@DatabaseSample.Models.Types.Date</option>
}
</select>
</div>
<div class="mb-3 form-check">
<input type="checkbox" class="form-check-input" id="primaryCheck">
<label class="form-check-label" for="exampleCheck1">Primary key</label>
</div>
</form>
<button id="addColumn" type="submit" class="btn btn-primary">Submit</button>
<table class="table" id="ColumnTable">
<thead>
<tr>
<th scope="col">Column name</th>
<th scope="col">Type</th>
<th scope="col">Primary key</th>
</tr>
</thead>
</table>
<button id="saveRecords" type="submit" class="btn btn-primary">Save</button>