.NET核心System.ValueTuple
我的应用程序是MVC核心C#中,我从一个方法两个项目,对象和字符串返回一个通用存储库:.NET核心System.ValueTuple
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传递给视图。
当您执行return View(data)
时,您将值元组返回到您的视图,其中data
是值元组的结果。你需要返回data.Item1。
其中之一,你应该命名你的PostWebApi方法中的元组返回值为了可读性的目的。所以,而不是像public (Student, String) PostwebApi()
之类的东西,你想要的东西像public (Student student, String msg) PostWebApi()
。
然后在你的控制器里,你可以做return View(data.student);
如果你需要元组的其他部分,你可以通过data.msg
来访问它。
谢谢。我改变了回报: public Object PostWebApi(TObject model,string url,string dir){... var result = new {student = model,message = msg};它返回:data {student = {WebUI.Models.Student},message =“{\”id \“:23,\”lastName} 无法使用data.student,得到一个错误(没有引用), – hncl
因此,在返回View(data.student)之前调试;'''显示学生有一个值,但在RazorView中ViewModel是空的? –
我无法在控制器中使用data.student。错误是我返回的对象没有为学生提供参考;我认为错误是由于返回一个对象? – hncl
另一种可能是你改变视图模型
@model System.ValueTuple<WebUI.Models.Student, object>
和访问项目1和项目2礼仪
有趣的,会试试看。谢谢 – hncl
你的价值观,你可以显示您的视图的代码?它看起来像你的看法是期待不同的类型 – Jonesopolis
@model WebUI.Models.Student。该方法返回我想用于ViewBag的模型和字符串。挑战是如何分割item1和item2。 – hncl
@GiladGreen你需要安装c#7的新版本。 Install-Package System.ValueTuple -Version 4.4.0。它的工作,我的应用程序是核心2.0。 – hncl