如何将整个模型从索引视图传递给完全不同的控制器?

时间:2022-05-04 00:06:32

Here's the relevant part of my Index view (Index.cshtml):

以下是我的索引视图(Index.cshtml)的相关部分:

    @foreach (var item in Model) {
       <li>
            @Html.ActionLink(item.name, "Index", "Filler", new { cap = item }, null)
       </li>
    }

As you can see, the ActionLink is tied to the Index action on the Filler Controller, and is passing in the entire item (the model)- "item" is of type "capsule".

如您所见,ActionLink与填充控制器上的索引操作相关联,并且正在传递整个项目(模型)——“项目”类型为“capsule”。

Now, on my Filler Controller, in the Index action:

现在,在我的填充控制器上,在索引动作中:

        public ActionResult Index(capsule cap)
        {
            var fillers = db.fillers.ToList();
            return View(fillers);
        }

The capsule class that was automatically generated by Entity Framework is:

由实体框架自动生成的胶囊类是:

namespace CapWorx.Models
{
using System;
using System.Collections.Generic;

public partial class capsule
{
    public capsule()
    {
        this.fillers = new HashSet<filler>();
    }

    public int pk { get; set; }
    public string name { get; set; }

    public virtual ICollection<filler> fillers { get; set; }
}
}

The problem is "cap" is NULL in the above Index action. But, if I change the type to "object" instead of "capsule", I do get some weird non-null data, but I can't cast the object to "capsule". Does anyone know why this is NULL?

问题是“cap”在上述索引操作中为空。但是,如果我将类型改为“object”而不是“capsule”,我确实会得到一些奇怪的非空数据,但是我不能将对象强制转换为“capsule”。有人知道为什么这个是空的吗?

Thanks,

谢谢,

Mike

迈克

4 个解决方案

#1


3  

You usually just have to pass in the id to the action. For example, can you refactor your code so that it can take in a capsuleId, get the capsule from db and do whatever processing is needed. Adding the entire object to route values in ActionLink doesn't make any sense. Have a look at the link being generated. It is probably just something like ...?cap=Namespace.Capsule as the object would have be ToStringed

您通常只需将id传递给动作。例如,您是否可以重构代码,使其能够接收到一个胶囊状体,从db中获取胶囊,并执行所需的任何处理。将整个对象添加到ActionLink中的路由值没有任何意义。查看正在生成的链接。它可能只是类似于…?cap=Namespace。被囊的物体将会被弯曲

#2


0  

The first problem is in MVC you can't bind to an interface (ICollection). You'll need to change it to a List - List<filler>. The second problem you will face is that Lists/Arrays need to be represented in array notation for proper posting, something like name="books[0].book_id". Even though MVC does a lot of magic, the model in your link still has to be represented as a query string eventually.

第一个问题是在MVC中,您不能绑定到接口(ICollection)。您需要将它更改为列表-列表 <填充> 。您将面临的第二个问题是,列表/数组需要用数组表示法表示,以便正确地发布,比如name=“books[0].book_id”。尽管MVC做了很多神奇的事情,但是您的链接中的模型最终仍然必须表示为查询字符串。

Depending on what you are trying to do, you may be better off representing your model as a JSON object and posting with .ajax().

根据您正在尝试的操作,最好将模型表示为JSON对象,并使用.ajax()发布。

See this SO post for other ideas - Need help with binding Set with Spring MVC form

请参阅本文以获取其他想法——需要使用Spring MVC窗体绑定集的帮助

#3


-1  

I'm not totally sure why this would work(I think you're nulling out the html attributes), but try to remove the "null" part of the actionlink.

我不完全确定为什么会这样(我认为您正在取消html属性),但是尝试删除actionlink的“null”部分。

Or, the controller which created the models is wrong.

或者,创建模型的控制器是错误的。

Again, don't kill me for this.

再说一遍,别为此杀了我。

#4


-1  

@Html.ActionLink just generates an anchor element (<a href="...">...</a>), so it makes no sense to attempt to bind a complete object to the routeValues parameter. As @manojlds says, it make much more sense to just pass the relevent key value, since you'll be performing the lookup then anyway (remember, the web is "stateless").

@Html。ActionLink只生成一个锚元素(),因此尝试将一个完整的对象绑定到routeValues参数是没有意义的。正如@manojlds所说,仅仅传递相关的键值就更有意义了,因为您将执行查找(记住,web是“无状态的”)。

#1


3  

You usually just have to pass in the id to the action. For example, can you refactor your code so that it can take in a capsuleId, get the capsule from db and do whatever processing is needed. Adding the entire object to route values in ActionLink doesn't make any sense. Have a look at the link being generated. It is probably just something like ...?cap=Namespace.Capsule as the object would have be ToStringed

您通常只需将id传递给动作。例如,您是否可以重构代码,使其能够接收到一个胶囊状体,从db中获取胶囊,并执行所需的任何处理。将整个对象添加到ActionLink中的路由值没有任何意义。查看正在生成的链接。它可能只是类似于…?cap=Namespace。被囊的物体将会被弯曲

#2


0  

The first problem is in MVC you can't bind to an interface (ICollection). You'll need to change it to a List - List<filler>. The second problem you will face is that Lists/Arrays need to be represented in array notation for proper posting, something like name="books[0].book_id". Even though MVC does a lot of magic, the model in your link still has to be represented as a query string eventually.

第一个问题是在MVC中,您不能绑定到接口(ICollection)。您需要将它更改为列表-列表 <填充> 。您将面临的第二个问题是,列表/数组需要用数组表示法表示,以便正确地发布,比如name=“books[0].book_id”。尽管MVC做了很多神奇的事情,但是您的链接中的模型最终仍然必须表示为查询字符串。

Depending on what you are trying to do, you may be better off representing your model as a JSON object and posting with .ajax().

根据您正在尝试的操作,最好将模型表示为JSON对象,并使用.ajax()发布。

See this SO post for other ideas - Need help with binding Set with Spring MVC form

请参阅本文以获取其他想法——需要使用Spring MVC窗体绑定集的帮助

#3


-1  

I'm not totally sure why this would work(I think you're nulling out the html attributes), but try to remove the "null" part of the actionlink.

我不完全确定为什么会这样(我认为您正在取消html属性),但是尝试删除actionlink的“null”部分。

Or, the controller which created the models is wrong.

或者,创建模型的控制器是错误的。

Again, don't kill me for this.

再说一遍,别为此杀了我。

#4


-1  

@Html.ActionLink just generates an anchor element (<a href="...">...</a>), so it makes no sense to attempt to bind a complete object to the routeValues parameter. As @manojlds says, it make much more sense to just pass the relevent key value, since you'll be performing the lookup then anyway (remember, the web is "stateless").

@Html。ActionLink只生成一个锚元素(),因此尝试将一个完整的对象绑定到routeValues参数是没有意义的。正如@manojlds所说,仅仅传递相关的键值就更有意义了,因为您将执行查找(记住,web是“无状态的”)。