C# 将通用列表从控制器传递到视图mvc
我已经为此绞尽脑汁很久了,现在我要听从专家的意见。我知道这个问题已经被问了好几次,也回答了好几次,但我似乎什么都做不到。下面是一个场景:正如标题所说,我试图将一个列表从控制器传递到视图。我使用的API有一个方法,C# 将通用列表从控制器传递到视图mvc,c#,asp.net-mvc,list,view,controller,C#,Asp.net Mvc,List,View,Controller,我已经为此绞尽脑汁很久了,现在我要听从专家的意见。我知道这个问题已经被问了好几次,也回答了好几次,但我似乎什么都做不到。下面是一个场景:正如标题所说,我试图将一个列表从控制器传递到视图。我使用的API有一个方法,“GetInventoryLocations”,它的基本类型是List。在下面的示例中,我实例化了一个新列表,并使用foreach通过“InventoryLocation”以编程方式将集合中的每个项目转换为字符串,并将其添加到我创建的列表“locationlist”。最后,我将列表分配给
“GetInventoryLocations”
,它的基本类型是List
。在下面的示例中,我实例化了一个新列表,并使用foreach
通过“InventoryLocation”
以编程方式将集合中的每个项目转换为字符串,并将其添加到我创建的列表“locationlist”
。最后,我将列表分配给viewdata
。从那以后,我尝试了视图中的各种方法,但仍然无法使其发挥作用。谢谢你的帮助。善待初级开发人员
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Moraware.JobTrackerAPI4;
using Evolveware1_0.Models;
namespace Evolveware1_0.Controllers
{
[Authorize]
public class InventoryController : Controller
{
//./Inventory/Locations
[HttpGet]
public ActionResult Index()
{
//declare variables for connection string to JobTracker API Service
var DB = "databasename"; // your DB name here
var JTURL = "https://" + DB + ".somecompany.net/" + DB + "/";
var UID = "****"; // your UID here - needs to be an administrator or have the API role
var PWD = "password"; // your PWD here
//connect to API
Connection conn = new Connection(JTURL + "api.aspx", UID, PWD);
conn.Connect();
//declaring the jobtracker list (type List<InventoryLocation>)
var locs = conn.GetInventoryLocations();
//create a new instance of the strongly typed List<string> from InventoryViewModels
List<string> locationlist = new List<string>();
foreach (InventoryLocation l in locs) {
locationlist.Add(l.ToString());
};
ViewData["LocationsList"] = locationlist;
return View();
}//end ActionResult
}
};
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
使用Moraware.JobTrackerAPI4;
使用Evolveware1_.0模型;
命名空间Evolveware1_0.0控制器
{
[授权]
公共类InventoryController:控制器
{
///存货/地点
[HttpGet]
公共行动结果索引()
{
//为JobTracker API服务的连接字符串声明变量
var DB=“databasename”;//此处显示您的数据库名称
var JTURL=“https://“+DB+”.somecompany.net/“+DB+”/”;
var UID=“****”;//此处的UID-需要是管理员或具有API角色
var PWD=“password”;//此处显示您的PWD
//连接到API
连接连接=新连接(JTURL+“api.aspx”,UID,PWD);
连接();
//声明jobtracker列表(类型列表)
var locs=conn.GetInventoryLocations();
//从InventoryViewModels创建强类型列表的新实例
List locationlist=新列表();
foreach(locs中的库存位置l){
locationlist.Add(l.ToString());
};
ViewData[“LocationsList”]=locationlist;
返回视图();
}//最终结果
}
};
并且认为:
@using Evolveware1_0.Models
@using Evolveware1_0.Controllers
@*@model Evolveware1_0.Models.GetLocations*@
@using Evolveware1_0.Models;
@{
ViewBag.Title = "Index";
}
<h2>Locations</h2>
@foreach (string l in ViewData["LocationList"].ToString())
{
@l
}
使用Evolveware1_0.0模型
@使用Evolveware1_0.0控制器
@*@模型Evolveware1_0.Models.GetLocations*@
@使用Evolveware1_.0模型;
@{
ViewBag.Title=“Index”;
}
位置
@foreach(ViewData[“LocationList”].ToString()中的字符串l)
{
@l
}
您正在对列表执行toString(),这将不起作用。您需要将ViewData转换为其正确的类型,即InventoryLocation列表
因为您使用的是Razor和MVC,所以我建议改用ViewBag,无需强制转换
在控制器中,而不是在ViewData[“LocationList”]=LocationList中,初始化要传递到视图的ViewBag属性
ViewBag.LocationList = locationlist;
然后在循环中的视图中访问ViewBag.LocationList对象
@foreach (string l in ViewBag.Locationlist)
{
@l
}
您正在对列表执行toString(),这将不起作用。您需要将ViewData转换为其正确的类型,即InventoryLocation列表
因为您使用的是Razor和MVC,所以我建议改用ViewBag,无需强制转换
在控制器中,而不是在ViewData[“LocationList”]=LocationList中,初始化要传递到视图的ViewBag属性
ViewBag.LocationList = locationlist;
然后在循环中的视图中访问ViewBag.LocationList对象
@foreach (string l in ViewBag.Locationlist)
{
@l
}
您是一名初级开发人员,并且已经在使用MVC了?很好。不要使用
ViewData
-将方法更改为返回视图(locationlist)
和视图到@model List@foreach(模型中的var l){…
您是一名初级开发人员,并且已经在使用MVC?很好。不要使用ViewData
-将方法更改为返回视图(locationlist);
和视图到@model List@foreach(模型中的var l){…
我才意识到我从来没有感谢你回答这个问题。我很感激。我才意识到我从来没有感谢你回答这个问题。我很感激。