I have an EDM. I pass a model to a view when it is displayed based on the boolean value of the Current field (Only one record will ever be current):
public ActionResult Index()
{
myEDM db = new MyEDM();
MyEDMModel model = db.MyEDMTable.Where(g => g.Current == 1).First();
return View(model);
}
The table has 10 columns
fld1 fld2 fld3 ......
Each of the fields is created with a default value of emptystring ” for comparison reasons (I cannot compare to Null).
In the view I display a background image and a HTML.Actionlink based on the content of each field:
@if (Model.fldNo1 == "")
{
<td class="number">
@Html.ActionLink("1", "TakeNumber", "Numbers", new { Model, number = 1 })
</td>
}
else
{
<td class="numberTaken">
@Html.ActionLink("1", "NumberTaken", "Home", new { Model, number = 1 })
</td>
}
Each field can be taken and reserved by the user. Only one user per field (obviously) and if the field != ” then I must do nothing. The field is reserved by someone else. The field is populated with the userID if available.
This is where my question comes in. I have passed the model from the load of the page from the EDM. If the user then browses the view for say 5 minutes and then selects to reserve a specific number. In order to check if another user has reserved this field in the 5 minutes of browsing time do I need to refresh the model from the EDM or is the passed model updated dynamically? i.e.
Do I do this:
myEDM db = new MyEDM();
Int32 muser = (Int32)Membership.GetUser().ProviderUserKey;
String user = muser.ToString();
if (number == 1 && model.fldNo1 == "")
{
model.fldNo1 = user;
UpdateModel(model);
db.SaveChanges();
}
elseif ...........
Or must I first recall or refresh the model to ensure an entry in particular field has not been entered in the duration of the users browsing (5minutes for example):
myEDM db = new MyEDM();
myEDMModel updateEDMModel = db.MyEDMTable.Where(g => g.id == model.id).First
Int32 muser = (Int32)Membership.GetUser().ProviderUserKey;
String user = muser.ToString();
if (number == 1 && updateEDMModel.fldNo1 == "")
{
updateEDMModel.fldNo1 = user;
UpdateModel(updateEDMModel);
db.SaveChanges();
}
elseif ...........
Unfortunately I am a one man band and I am having great difficulty testing this.
A model populated from Entity Framework and passed to your View is no longer attached. Even if you made your EF call from your View (bad practice, dont), the rendered values would not be attached.
So, NO, your displayed values would NOT change as the database values changed
In order to create the behavior you want, you would need to use an Ajax method.
One I like a lot is to use an
@Html.Action()partial. Call if in a