I want to get into good habits from the start so I have this question and a problem:
I have been doing the following which has been working, then I read this post
This example of what I have been doing:
CONTROLLER
public ActionResult OneDollar130(Int32 number)
{
MyEDM db = new MyEDM();
MyDBModel model = db.MyTable.Where(t => t.Current == 1 && t.No == number).First();
return View(model);
}
VIEW
@model MyProject.MyDBModel
<table>
<tr>
@if (Model.fldNo1 == "")
{
<td class="numberTD">
@Html.ActionLink("1", "Number1", "Number", new { model = Model, number = 1 }, null)
</td>
}
else
{
<td class="numberTD2">
@Html.ActionLink("1", "Number2", "Number", new { model = Model, number = 1 })
</td>
}
</tr>
</table>
I am using the model from my EDM and passing it to the View.
I read in above post that I should not pass my database models to the view as this is bad practice. This concerns me somewhat as I want to be doing it right.
So based on the above post I change my code to experiment and hit a snag:
CONTROLLER
public ActionResult OneDollar112(Int32 TableNo)
{
return View(new getOneDollar112Game(TableNo));
}
MODEL
public class getMyModel
{
MyEDM db = new MyEDM();
public MyDBModel MyModel { get; set; }
public getMyModel() { }
public getMyModel(Int32 number)
{
MyModel = db.MyTable
.Where(t => t.Current == 1 && t.No == numbner).First();
}
}
VIEW
@model MyProject.Models.getMyModel
<table>
<tr>
@if (Model.fldNo1 == "")
{
<td class="numberTD">
@Html.ActionLink("1", "Number1", "Number", new { model = Model, number = 1 }, null)
</td>
}
else
{
<td class="numberTD2">
@Html.ActionLink("1", "Number2", "Number", new { model = Model, number = 1 })
</td>
}
</tr>
</table>
Okay so my 2 questions are:
Which is the best practice ... does what I was doing before break the rule of not passing DB models to the view?
If the second method is correct (which I am supposing) why do I continually get the error that fldNo1 does not exist?
e.g: CS1061: 'MyProject.Models.getMyModel' does not contain a definition for 'fldNo1' and no extension method 'fldNo1' accepting a first argument of type 'MyProject.Models.getMyModel' could be found (are you missing a using directive or an assembly reference?)