[英]Form Post of an Complex Model to Controller in ASP.NET Core MVC
I have a question regarding a Form Post of an complex Model. 我有一个关于复杂模型的表单发布的问题。 My Model has a subclass and
IEnumerable
s: 我的模型有一个子类和
IEnumerable
:
public class MyViewModel
{
public int SingleInteger { get; set; }
public IEnumerable<int> MultipleInts { get; set; }
public IEnumerable<MySubclass> AssembledClass { get; set; }
public IEnumerable<string> MultipleStrings { get; set; }
}
public class MySubclass
{
public int Quantity { get; set; }
public int Type{ get; set; }
}
What is the best way to get the Input from my View to my Controller? 将输入从视图传输到控制器的最佳方法是什么? Can the Modelbinder even bind this?
Modelbinder可以绑定吗?
Can the Modelbinder even bind this?
Modelbinder可以绑定吗?
Yes, and it can do a lot more complexed stuff then this class structure. 是的,它可以比此类结构做更多复杂的事情。
What is the best way to get the Input from my View to my Controller?
将输入从视图传输到控制器的最佳方法是什么?
Post is as json. 发布为json。
Example: 例:
You could POST this json 你可以发布这个json
{"singleInteger":1,"multipleInts":[1,2,3],"assembledClass":[{"quantity":1,"type":2}],"multipleStrings":["one","two"]}
To an controller method like this 对于这样的控制器方法
public IActionResult Post([FromBody]MyViewModel model)
And it will be translated into an instance of MyViewModel like this 它将像这样转换为MyViewModel的实例
var model = new MyViewModel
{
SingleInteger = 1,
MultipleInts = new List<int>(){ 1,2,3 },
AssembledClass = new List<MySubclass>{
new MySubclass
{
Quantity = 1,
Type = 2
}
},
MultipleStrings = new List<string>(){ "one", "two"}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.