I have a problem when I want to convert one object to another, these models being the same but different namespace
.
Explaining myself better, I have two projects in a solution each with its model in which I have that repeated model ( AdminModel
) I am passing that variable from Session
and converting it
This is the initial model of the object.
namespace Admin.ModeloAdmin{
public class AdminModel
{
public string devKey { get; set; }
public string mobileKey { get; set; }
public string cuenta { get; set; }
public int idCliente { get; set; }
public int idCuenta { get; set; }
public int idEmpresa { get; set; }
public int regreso { get; set; }
}
}
I want to convert it to this other model
namespace User.ModeloAdmin{
public class AdminModel
{
public string devKey { get; set; }
public string mobileKey { get; set; }
public string cuenta { get; set; }
public int idCliente { get; set; }
public int idCuenta { get; set; }
public int idEmpresa { get; set; }
public int regreso { get; set; }
}
}
But when I want to convert it, I get an error in the following line:
AdminModel sesionManage = (AdminModel) Session["sessionManage"];
How can I pass data from one model to another?
this is the error message:
Server error in application '/'.
Cannot convert an object of type 'AdmixDownloads.Models.AdminModel' to type 'DownloadSU.Models.AdminModel'.
Description: Unhandled exception executing the current web request. Review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Cannot cast an object of type 'AdmixDownloads.Models.AdminModel' to type 'DownloadSU.Models.AdminModel'.
Source code error:
Line 31: else
Line 32: {
Line 33: sessionManage = (AdminModel)HttpContext.Session["sessionManage"];
Line 34: }
Line 35:
Source file: C:\Users\juan.gonzalez\Documents\JuanGonzalez\PruebasMVC\AdmixPortal\AdmixDescargas\AdmixDescargas\Areas\DescargaSU\Controllers\InicioController.cs Line: 33
Stack trace:
[InvalidCastException: Cannot cast an object of type 'AdmixDescargas.Models.AdminModel' to type 'DescargaSU.Models.AdminModel'.] DownloadSU.Controllers.InicioController.Index(String Sesion) in C:\Users\juan.gonzalez\ Documents\JuanGonzalez\PruebasMVC\AdmixPortal\AdmixDescargas\AdmixDescargas\Areas\DescargaSU\Controllers\InicioController.cs:33 lambda_method(Closure , ControllerBase , Object[] ) +103 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[ ] parameters) +14 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +157 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor,IDictionary`2 parameters) +27 System.Web.Mvc.Async.AsyncControllerActionInvoker.b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +22 System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +29 System.Web. Mvc.Async.WrappedAsyncResultBase`1.End() +49 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32 System.Web.Mvc.Async.AsyncInvocationWithFilters.b__3d() +50 System.Web.Mvc .Async.<>c__DisplayClass46.b__3f() +225 System.Web.Mvc.Async.<>c__DisplayClass33.b__32(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34 System.Web.Mvc.Async.<>c__DisplayClass2b.b__1c() +26 System.Web.Mvc.Async.<>c__DisplayClass21.b__1e(IAsyncResult asyncResult) +100 System.Web.Mvc .Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27 System. Web.Mvc.Controller.b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13 System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +36 System.Web.Mvc.Controller.b__15(IAsyncResult asyncResult, Controller controller) +12 System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22 System.Web.Mvc. Async.WrappedAsyncResultBase`1.End() +49 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) + 10 System.Web.Mvc.MvcHandler.b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21 System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29 System.Web.Mvc.Async.WrappedAsyncResultBase`1. End() +49 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28 System.Web.Mvc.+9765121
Each of the models are separate classes, even though they have the same attributes, what you can do is overload the type of conversion you're trying to do (I had explained this very superficially in another question ).
What you have to do is overload the explicit operator in the class that is going to perform the cast.
In your case it would be something like this
namespace User.ModelAdmin{
}
You do the same with the other class and so you can convert from one type to the other and vice versa.
Here is another example taken from dotnetperls
Departure:
Option 1:
(I use var to infer the type of variable, in a temporary variable.)
Option 2:
use automapper
I managed to get the same thing only maybe in a less optimal way.
Since they are practically the same classes, I simply serialize and then deserialize it using the other class.
It is not ideal but it is functional
It is something that perhaps you can use very frequently, what works for me in these cases is AutoMapper. https://github.com/AutoMapper/AutoMapper If this isn't something that will be done frequently for you, you could just copy the properties.