Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Dec 14, 2018. It is now read-only.
/MvcPublic archive
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Different working in ViewComponents in v.1.1.0 #5561

Closed
@simon25608

Description

@simon25608

Hello!

After upgrading from version 1.0.1 to 1.1.0 I have detected that the behavior of the viewcomponets has changed.
In the previous version it had the following code:
<div> @await Component.InvokeAsync("Archivos", new { vista = Archivos.Listado, archivos = Model.Archivos, titulo = "Documentos adjuntos" }) </div>

Wherearchivos = Model.Archivos is a List. And in the component template I receive the model:
@model IList<COFC.ViewModels.ArchivoViewModel>

In this same view, I call the component again, with different parameters and the one who calls another different view
@await Component.InvokeAsync("Archivos", new { vista = Archivos.Nuevo })

that receives the model
model COFC.ViewModels.ArchivoViewModel

And this is the component code:

` public class Archivos : ViewComponent
{

    public const string Detalle = "Detalle";    public const string Listado = "Listado";    public const string Nuevo = "Nuevo";    public const string Modificar = "Modificar";    public IViewComponentResult Invoke()    {        object vista;        object archivos;        object titulo;        object archivo;        if (ViewComponentContext.Arguments.TryGetValue("vista", out vista) && ViewComponentContext.Arguments.TryGetValue("archivos", out archivos) && ViewComponentContext.Arguments.TryGetValue("titulo", out titulo))        {            ViewBag.TituloArchivos = (string)titulo;            return View((string)vista, (ICollection<ArchivoViewModel>)archivos);        }        else if (ViewComponentContext.Arguments.TryGetValue("vista", out vista) && ViewComponentContext.Arguments.TryGetValue("archivos", out archivos))        {            return View((string)vista, (ICollection<ArchivoViewModel>)archivos);        }        else if (ViewComponentContext.Arguments.TryGetValue("vista", out vista) && ViewComponentContext.Arguments.TryGetValue("archivo", out archivo))        {            return View((string)vista, (ArchivoViewModel)archivo);        }        else if (ViewComponentContext.Arguments.TryGetValue("vista", out vista))        {            return View((string)vista);        }        else        {            throw new Exception("Los parámetros enviados no se corresponden con los definidos");        }    }}`

The second call to the viewcomponent returns the view
else if (ViewComponentContext.Arguments.TryGetValue("vista", out vista)) { return View((string)vista); }

And this is where the different behavior occurs between version 1.0.1 and 1.1.0. The previous version was perfectly executed, creating an empty object of the viewmodel used in the view (in this case ArchivoViewModel), but in the new version it tries to use the viewmodel of the first call (in this case List ) causing the exception:

InvalidOperationException: The model item passed into the ViewDataDictionary

In this case, this is solved with
else if (ViewComponentContext.Arguments.TryGetValue("vista", out vista)) { return View((string)vista, new ArchivoViewModel()); }

But there are other cases in which it is more difficult to solve, is this change of normal operation? I find nothing about it.

I hope I explained well

Thanks.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2025 Movatter.jp