[英]How do I create a new object using dependency injection
At the moment I have a form where users create a new object. 目前,我有一个表单,用户可以在其中创建一个新对象。 This is then passed to the controller as JSON.
然后将其作为JSON传递到控制器。
How can I create a new object from this JSON to insert into the DB that I have, without doing 我如何通过此JSON创建一个新对象以插入到我拥有的数据库中,而无需这样做
var x = new CustomObject {
ExampleField = JSONProperty,
ExampleField2 = JSONProperty2
};
repo.Create(x);
In general, you need something like this: 通常,您需要这样的东西:
[HttPost]
public ActionResult CreateCustomer(string json)
{
var customer = JsonConvert.DeserializeObject<Customer>(json);
repo.Create(customer);
return View(customer);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.