那么下一个例子Controller
有效吗?或者这样的逻辑应该在其他地方?据我了解,我们需要使用DTO
在层之间传输数据,所以如果我们通过层JsonResult
或ViewModel
从BussinesLogic
层传递,它会出错吗?那么这个例子对了,专门用于ViewModel
创建的逻辑可以在controller
?
[HttpPost]
[ValidateAntiForgeryToken]
public JsonResult UploadImage(HttpPostedFileBase file)
{
var result = UploadedImageHandler.UploadFile(file);
JsonResult json;
if (result != null)
{
var uploadImageViewModel = new UploadedImagesViewModel
{
foo = result.foo
//here some values from result goes to ViewModel
};
var uploadResult = new UploadResultViewModel
{
Preview = new PreviewViewModel
{
bar = result.bar
//etc.
},
UploadedImage = uploadImageViewModel
};
json = new JsonResult
{
Data = uploadResult,
ContentType = "text/html"
};
}
else
{
json = new JsonResult
{
ContentType = "text/html"
};
}
return json;
}