我的应用程序是MVC核心c#,我从通用存储库中的一个方法返回两个项,对象和字符串:
public object PostWebApi(TObject model, string url, string dir)
{...
return (model, msg);}在我使用的控制器中
var data = _studentService.PostWebApi(WebApiDirectory());
return View(data)将模型传递给视图,我会得到错误
The model item passed into the ViewDataDictionary is of type 'System.ValueTuple

在调试时,返回的对象有两个项,模型和字符串。无法知道如何将模型item1传递到视图。
发布于 2017-09-17 03:35:40
在执行return View(data)时,将值元组返回给视图,其中data是值元组的结果。您需要返回data.Item1。
首先,为了便于阅读,您应该在PostWebApi方法中命名元组的返回值。因此,您需要的不是像public (Student, String) PostwebApi()这样的东西,而是像public (Student student, String msg) PostWebApi()这样的东西。
然后在控制器内部,您可以执行return View(data.student);,如果需要元组的其他部分,可以通过data.msg访问它。
发布于 2017-09-17 10:19:47
另一种方法可能是将视图模型更改为
@model System.ValueTuple<WebUI.Models.Student, object>并在Item1和Item2礼仪中访问您的价值观
https://stackoverflow.com/questions/46260282
复制相似问题