PDA

Click to See Complete Forum and Search --> : VS 2010 [RESOLVED] Convert viewmodel to string


Nightwalker83
May 31st, 2011, 08:19 PM
Hi,

How do I convert a view model to a string? This is what I have tried but it still returning the error "Cannot implicitly convert type 'System.Web.Mvc.ViewResult' to 'string'".


public string Index()
{
//Create a list of genres
var genres = new List<string> { "Rock", "Jazz", "Country", "Pop", "Disco" };

//Create the view model
var viewModel = new StoreIndexViewModel
{
NumberOfGenres = genres.Count(), Genres = genres
};
return View(viewModel.ToString());

}


Thanks,


Nightwalker

Nightwalker83
May 31st, 2011, 08:40 PM
Never mind! I realized my mistake. I deleted the ActionResult that was created when the class was created. The above is suppose to look like this code:


public ActionResult Index()
{
//Create a list of genres
var genres = new List<string> { "Rock", "Jazz", "Country", "Pop", "Disco" };

//Create the view model
var viewModel = new StoreIndexViewModel
{
NumberOfGenres = genres.Count(),
Genres = genres
};
return View(viewModel);
}

gep13
Jun 1st, 2011, 01:02 AM
Hey,

Looks to me like you are doing ASP.Net MVC, rather than straight up ASP.Net, so I am going to move this thread to the MVC thread, which is where questions on ASP.Net MVC should be asked.

Gary