如何修复不能隐式地转换type 'System.Web.Mvc。ViewResult”“字符串”

时间:2022-04-18 16:32:45

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

返回视图(视图模型);在visual studio 2008中导致错误

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

不能隐式转换type 'System.Web.Mvc。ViewResult”“字符串”

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; }
        }

1 个解决方案

#1


2  

You may need to append .ToString() to the method call where ever you are calling it, although this will not return the expected result in most cases. Instead I would make another Welcome method that returns a String and do what you need to within that method.

您可能需要将. tostring()附加到调用它的方法调用中,尽管在大多数情况下这不会返回预期的结果。相反,我将创建另一个Welcome方法,该方法返回一个字符串,并在该方法中执行所需的操作。

#1


2  

You may need to append .ToString() to the method call where ever you are calling it, although this will not return the expected result in most cases. Instead I would make another Welcome method that returns a String and do what you need to within that method.

您可能需要将. tostring()附加到调用它的方法调用中,尽管在大多数情况下这不会返回预期的结果。相反,我将创建另一个Welcome方法,该方法返回一个字符串,并在该方法中执行所需的操作。