s

error asp.net mvc pass string to view edit button Edit

author
Murugan Andezuthu Dharmaratnam | calendar 27 January 2021 | 1313

I am getting an exception while passing a string to a view. What I was doing was to convert the object to a JSON string and then pass the string to the view. the exception is thrown even if just try to pass a hello world string to a view.

Solution

The solution is to typecast the string to an object then pass it to a view. Another reason why you should cast the string as a model is that MVC will try to load the string as the view name. In this case, it will look for the "hello world.cshtml" page if you don't cast the string as object and pass it return View(ret);

Controller 

String retval = "Hello world";
Object ret = (Object)retval;
return View("Index", ret);

View

In the razor page view on top, the type of receiving model should be a string 
@model String