Manju Manju - 10 days ago 5
C# Question

How to remove the reference of @using @Model shown on View in MVC5 ASP.Net

I have a view with the following reference at the top of the view as shown below.

@using MVC_Application.ViewModels.SPA;
@Model MainViewModel;
@{
Layout = null;
ViewBag.Title = "Index";
}


When this view is viewed on a browser I do see the reference shown on browser as MVC_Application.ViewModels.SPA.MainViewModel MainViewModel; at the top of the screen and rest of the View is displayed as expected.

Can someone please tell me how to remove this reference when View is viewed on a browser.

Answer

You are trying to use the model directive by using an uppercase M. That is the instance of the model passed in which is why you see the typename output and then the plain text of MainViewModel following it. Simply change your directive to this:

@model MainViewModel

Note: The semicolons are not needed

Bonus: You could also get rid of the @using line by specifying the full model namespace (assuming the rest of the view doesn't rely on it):

@model MVC_Application.ViewModels.SPA.MainViewModel