Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't find view even though it's there, it's not looking for the correct extension

I'm confused about this one. I'm using razor with MVC 3 and I have a problem with one view on one action.

 [HttpGet]
 [ActionName("Unsubscribe")]
 public ActionResult UnsubscribeGet(string maskedId)
 {    
     return View("Unsubscribe", maskedId);
  }

The Unsubscribe.cshtml view is in the correct view folder.

The route is

routes.MapRoute(
               "", // Route name
               "Unsubscribe/{maskedId}", // URL with parameters
               new { controller = "Account", action = "Unsubscribe" });

When going to the following url "/Unsubscribe/db94fddb", the action fires and then the following error is shown when it tries to find the view.

The view 'Unsubscribe' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Account/Unsubscribe.aspx
~/Views/Account/Unsubscribe.ascx
~/Views/Shared/Unsubscribe.aspx
~/Views/Shared/Unsubscribe.ascx
~/Views/Account/db94fddb.master
~/Views/Shared/db94fddb.master
~/Views/Account/db94fddb.cshtml
~/Views/Account/db94fddb.vbhtml
~/Views/Shared/db94fddb.cshtml
~/Views/Shared/db94fddb.vbhtml

Notice it doesn't look for the cshtml extension when looking for Unsubscribe but does when looking for db94fddb

This is only happening with this view, completely at a loss on how to fix it.

** EDIT **

Found it,

I was using System.String as the model. For some reason that was causing it to blow up.

I swapped it to @model UnsubscribeViewModel

[HttpGet]
[ActionName("Unsubscribe")]
public ActionResult UnsubscribeGet(string maskedId)
{
    return View("Unsubscribe", new UnsubscribeViewModel { MaskedId = maskedId } );
}

Although this now works I still have no idea why it didn't work before. I have my views set to compile on build so there were no compiler errors.

I would love to know what's happening here.

like image 438
Magpie Avatar asked Dec 17 '22 05:12

Magpie


1 Answers

The overload of the View method you were using takes two strings. It doesn't interpret the second string as a model, it interprets it as the master page name:

protected internal ViewResult View(
    string viewName,
    string masterName
)

If you instead called it like this, it would work:

return View("Unsubscribe", (object) maskedId);
like image 190
Paul Tyng Avatar answered Dec 18 '22 19:12

Paul Tyng