[英]Pass a generic list from controller to view mvc
I've racked my brain over this for way to long now to I'm deferring to the experts. 我已经为此花了很长时间才让我敬请专家们。 I know this question has been asked and answered several times but I can't seem to get ANYTHING to work.
我知道这个问题已被问过几次,但我似乎什么也做不了。 Here's the scenario: As the title says, I'm trying to pass a list from the controller to the view.
这是场景:正如标题所述,我正在尝试将列表从控制器传递到视图。 I'm using an API that has a method,
"GetInventoryLocations"
whose base type is List<string>
. 我正在使用一个具有基本类型为
List<string>
的方法"GetInventoryLocations"
的API。 In the example below I instantiate a new list and use a foreach
to loop through the "InventoryLocation"
programatically casting each item in the collection to a string and adding it to the list I created "locationlist"
. 在下面的示例中,我实例化了一个新列表,并使用
foreach
"InventoryLocation"
编程方式将集合中的每个项目强制转换为字符串,并将其添加到我创建的"locationlist"
列表中。 Finally I assign the list to the viewdata
. 最后,我将列表分配给
viewdata
。 From there I've tried all kinds of things in the view but still can't get it to work. 从那里开始,我尝试了视图中的所有事情,但仍然无法使它起作用。 Thanks for any help.
谢谢你的帮助。 Be kind to a junior developer.
对初级开发人员要好一些。
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
}
};
And in the view: 并在视图中:
@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
}
You are doing a toString() to a list, this will not work. 您正在对列表执行toString(),这将不起作用。 You will need to cast your ViewData to it's proper type, a list of InventoryLocation.
您将需要将ViewData强制转换为正确的类型,即InventoryLocation的列表。
Being that you are using Razor and MVC, I suggest using ViewBag instead, no casting necessary. 由于您使用的是Razor和MVC,因此建议您改用ViewBag,而无需强制转换。
In your controller instead of ViewData["LocationList"] = locationlist, initialize a ViewBag property to pass to your view. 在您的控制器而不是ViewData [“ LocationList”] = locationlist中,初始化一个ViewBag属性以传递到您的视图。
ViewBag.LocationList = locationlist;
Then in your view in your loop just access your ViewBag.LocationList object. 然后在循环视图中,只需访问ViewBag.LocationList对象即可。
@foreach (string l in ViewBag.Locationlist)
{
@l
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.