不,不是。在您定义的模型中,您创建了两个不同的数组:第一个属性Field是字符串数组,第二个属性Check是布尔数组。把[]类型放在后面表示一个数组。
如果你有未知数量的我称之为“迷你表单”,并且这些数量是由用户通过 UI 决定的,那么你应该创建一个视图模型来表示这个迷你表单,并创建一个容器视图模型来容纳它和您的视图需要的任何其他属性。
例如:
public class MiniFormViewModel
{
public string MyInput { get; set; }
public bool MyCheck { get; set; }
}
然后在您的容器视图模型中:
public class ContainerViewModel
{
public IEnumerable<MiniFormViewModel> MiniForms { get; set; }
//Any other properties you need on the view that will occur a single time
}
现在,在 JS 中,您需要添加一些操作才能做到这一点:
function getViewModel() {
//You'll have to decide how you want to get the values of the mini form's fields. Perhaps you might even have a function to supply these values. Up to you.
return {
MiniForms: [{
MyInput: '', //value from the first "mini form' string field
Mycheck: false //value from the first "mini-form" bool field
},
{
MyInput: '', //value from the second"mini form' string field
Mycheck: false //value from the second"mini-form" bool field
}
]
}
}
然后,您需要将其发布回服务器。我将通过内置的 JS Fetch 函数演示如何做到这一点:
fetch(yourUrlForTheControllerAction,
{
method: 'post',
body: JSON.stringify(getViewModel()),
headers: {
'content-type': 'application/json; charset=UTF-8'
}
})
然后 blammo,你应该很高兴。我排除了动态添加迷你表单字段的部分,因为听起来您已经有了解决方案。