Sometimes I put some of my C# code in my view (
var db = new Contracts.Models.DataContext();
var LawCat = db.LawCategory.ToList();
<select name="Type" id="Type" class="form-control">
@foreach (var i in LawCat)
<option value="@i.ID" @(Request.QueryString["id"] == i.ID.ToString() ? "selected" : "")>@i.Name</option>
You can use DBContext (EntityFramework) code directly in View as it is allowed and execute perfectly.
Now problem comes with design and MVC pattern itself.
In MVC pattern , we mostly use for separation of concern. Here view has more than one concern apart from managing view. It directly call EntityFramework and so it break that point.
It make code less testable. As here we also want to look at each view as view might contains some code and that not specified in Controller so by looking at controller only we can not identify dependency.