How to fix Cannot implicitly convert the type 'System.Web.Mvc.ViewResult' to 'string'

return View (viewModel); cause of error in visual studio 2008

Cannot implicitly convert type 'System.Web.Mvc.ViewResult' to 'string'

public ActionResult Welcome(string name, int numTimes = 1)
        {
            var viewModel = new WelcomeViewModel
            {
                Message = "Hello " + name,
                NumTimes = numTimes
            };

            return View(viewModel);
        }

        public class WelcomeViewModel
        {
            public string Message { get; set; }
            public int NumTimes { get; set; }
        }
+3
source share
1 answer

You may need to add .ToString () to the method call when you call it, although in most cases this will not return the expected result. Instead, I would make another welcome method that returns a string and does what you need as part of this method.

+2
source

Source: https://habr.com/ru/post/1781173/


All Articles