Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .net核心系统.ValueTuple_C#_Asp.net Core - Fatal编程技术网

C# .net核心系统.ValueTuple

C# .net核心系统.ValueTuple,c#,asp.net-core,C#,Asp.net Core,我的应用程序是MVC Core c#,我从通用存储库中的方法返回两项,对象和字符串: public object PostWebApi(TObject model, string url, string dir) {... return (model, msg);} 在我使用的控制器中 var data = _studentService.PostWebApi(WebApiDirectory()); return View(data) 将模型传递给视图时,

我的应用程序是MVC Core 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传递到视图。

当您返回视图(数据)时,您正在将值元组返回到视图,其中
数据
是值元组的结果。您需要返回data.Item1

首先,为了便于阅读,您应该在PostWebApi方法中命名元组的返回值。。因此,您需要的不是像
public(Student,String)PostwebApi()
,而是像
public(Student,Student,String msg)PostwebApi()


然后在控制器内部,您可以执行
返回视图(data.student)
如果您需要元组的另一部分,可以通过
data.msg

访问它。执行
返回视图(数据)
操作时,您正在将值元组返回到视图中,其中
data
是值元组的结果。您需要返回data.Item1

首先,为了便于阅读,您应该在PostWebApi方法中命名元组的返回值。。因此,您需要的不是像
public(Student,String)PostwebApi()
,而是像
public(Student,Student,String msg)PostwebApi()


然后在控制器内部,您可以执行
返回视图(data.student)
如果需要元组的其他部分,可以通过
数据访问它。msg

另一种方法可能是将视图模型更改为

@model System.ValueTuple<WebUI.Models.Student, object>
@model System.ValueTuple

并访问项目1项目2属性中的值

另一种方法可能是将视图模型更改为

@model System.ValueTuple<WebUI.Models.Student, object>
@model System.ValueTuple


并访问项目1项目2属性中的值

您可以显示查看代码吗?看起来您的视图期望的是不同的结果type@modelWebUI.Models.Student。该方法返回要用于ViewBag的模型和字符串。挑战在于如何拆分item1和item2。@GiladGreen您需要安装c#7的新版本。安装软件包System.ValueTuple-版本4.4.0。成功了,我的应用程序是Core 2.0。你能为你的视图显示代码吗?看起来您的视图期望的是不同的结果type@modelWebUI.Models.Student。该方法返回要用于ViewBag的模型和字符串。挑战在于如何拆分item1和item2。@GiladGreen您需要安装c#7的新版本。安装软件包System.ValueTuple-版本4.4.0。成功了,我的应用程序是Core 2.0。谢谢。我更改了返回:public Object PostWebApi(TObject model,string url,string dir){…var result=new{student=model,message=msg};返回(result);}它返回:data{student={WebUI.Models.student},message=“{\'id\”:23,\'lastName}无法使用数据。student,获取错误(无引用),因此在返回视图(data.student)之前调试;显示学生有一个值,但在RazorView中ViewModel为空?我无法在控制器中使用data.student。错误是我返回的对象没有对学生的引用;我认为错误是由于返回了一个对象造成的。您的问题是,您没有正确使用ValueTuple,而是返回了一个匿名对象对象。您需要使您的方法看起来像
public(Student-Student,string-msg)PostWebApi(TObject-model,string-url,string-dir)
,然后对于您的返回,执行
return(model,msg)
我编辑了该问题。我正在一个通用存储库中使用该方法。我尝试了您的建议,但无法将模型Student用作ToObject。谢谢。我更改了返回:public Object PostWebApi(ToObject model,string url,string dir){…var result=new{Student=model,message=msg};return(result);}它返回:data{student={WebUI.Models.student},message=“{\'id\':23,\'lastName}无法使用data.student,获取错误(无引用),因此在
返回视图(data.student)之前进行调试;
显示学生有一个值,但在RazorView中ViewModel为空?我无法在控制器中使用data.student。错误是我返回的对象没有对学生的引用;我认为错误是由于返回了一个对象造成的。您的问题是,您没有正确使用ValueTuple,而是返回了一个匿名对象对象。您需要使您的方法看起来像
public(Student-Student,string-msg)PostWebApi(TObject-model,string-url,string-dir)
,然后对于您的返回,执行
return(model,msg)
我编辑了该问题。我正在一个通用存储库中使用该方法。我尝试了你的建议,但无法将模型学生用作TObject。感谢尝试,将尝试一次。感谢有趣,将尝试一次。谢谢