简体   繁体   English

如何将列表从Controller传递到View?

[英]How to pass a list from Controller to View?

This is what I've tried: 这是我尝试过的:

In Controller: 在控制器中:

public ActionResult Detail()
{
     List<string> list = new List<String>();
     return View(list);
}

In View of Detail: 鉴于细节:

@model dynamic
<!-- some HTML -->
<script>
     @Model.add(document.getElementById("input_box").value);
</script>

Problem is, intelli-sense is not detecting that the passed in object is a List so @Model.add doesn't work. 问题是,智能感知无法检测到传入的对象是List,因此@ Model.add无法正常工作。

My goal is to pass a list into the view so I can gather data from user inputs, and then use that list of data in my controller. 我的目标是将一个列表传递到视图中,以便我可以从用户输入中收集数据,然后在控制器中使用该数据列表。 Any ideas? 有任何想法吗?

The current problem can be solved by using a statically typed model: 当前问题可以通过使用静态类型的模型来解决:

@model List<string>
<!-- some HTML -->
<script>
     @Model.Add(document.getElementById("input_box").value);
</script>

However, you'll quickly hit another wall when the compiler complains that it doesn't know what document is. 但是,当编译器抱怨它不知道什么document时,您很快就会碰壁。 That's because the Razor view is rendered on the server side, but document is an object provided by the browser on the client side. 这是因为Razor视图是在服务器端呈现的,而document是由客户端浏览器提供的对象。 You simply can't dynamically add elements to this server-side list from the client. 您根本无法从客户端向该服务器端列表动态添加元素。 You'll need to encode your list somehow for the client to be able to read, say in a JavaScript array. 您需要对列表进行某种编码,以便客户端能够读取,例如在JavaScript数组中。 Then dynamically modify the list on the client side, perhaps using the JavaScript .push method. 然后,可以使用JavaScript .push方法在客户端动态修改列表。 And then post this back to the controller in a separate action. 然后通过单独的操作将其发布回控制器。

It's not going to be that simple. 不会那么简单。

Firstly, the Razor statement you used 首先,您使用的Razor声明

@Model.add(document.getElementById("input_box").value);

is not a valid C# statement. 不是有效的C#语句。 It will cause an exception when you try to open the page, because the Model type you're using ( List<T> ) does not have a method called add (though it has Add ), and further because that javascript is not valid Razor code either :) 当您尝试打开页面时,它将导致异常,因为您使用的模型类型( List<T> )没有名为add的方法(尽管具有Add ),并且进一步,因为该JavaScript无效,Razor代码:)

Secondly, you seem to be confused about how the server-client interaction works. 其次,您似乎对服务器与客户端交互的工作方式感到困惑。 You need to carefully think about how what you want to do can be realized-- 您需要仔细考虑如何实现自己的目标-

For you to be able to get information from the client (the user's browser), you first need to render the page, send it to the client, let the user (or whatever) enter the information, and then get them to send it back to a Controller action. 为了能够从客户端(用户的浏览器)获取信息,您首先需要呈现页面,将其发送给客户端,让用户(或其他用户)输入信息,然后让他们将其发送回到控制器动作。 At the moment your page is not even rendering, so you couldn't hope to get data from anyone. 目前,您的页面甚至还没有呈现,因此您不希望从任何人那里获取数据。

This can still be solved in numerous ways, one of which is to use Html.BeginForm(...) to create a simple form and have an input element and a submit button somewhere in there. 仍然可以通过多种方法解决此问题,其中一种方法是使用Html.BeginForm(...)创建简单的表单,并在其中的某个位置具有输入元素和提交按钮。

If you can get the user to submit the data back to you, you can then do whatever you like with the data in the previously mentioned Controller action. 如果您可以让用户将数据提交回给您,那么您可以对前面提到的Controller操作中的数据做任何您想做的事情。

好的,通过在View的JavaScript中创建一个全局数组,然后将其作为参数与Ajax一起传递给Controller函数来解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM