ASP.NET MVC
why do you have to torture me?
M V C
1 2 3
what is it that I've done to thee?
MVC says "don't blame me".
It's that jerk Entity Framework!
"Blame EF!" says MVC,
"it's probably another EF quirk".
My EF model represents an abstraction of up to two types of website material.
- a news article
- a highlight
Each record can be a news article, a highlight (basically just a picture with a short description), or both.
My EF model contains two bools:
This is very much an analogue to the movie database tutorial with a few more fields.
Walking the code with F11:
F5: debug starts ... http://localhost:50316/ our home page: tangent: an interesting (to me) discovery:
http://localhost:50316/home same home page } ~~ wait one minute!
http://localhost:50316/HOME same home page } ~~ AFAIK, URLs are supposed
http://localhost:50316/home/index same home page } to be case sensitive!
http://localhost:50316/Home/index same home page } ~~ in ASP.NET MVC, they appear
http://localhost:50316/hOmE/iNdEx same home page } to be case insensitive!
<%
= Html.ActionLink("Articles", "ArticlesIndex", "Article") %>
via above menu, I go to http://localhost:50316/Article/ArticlesIndex.
Another revelation: I notice a new folder in my solution called "Script Documents".
"Script Documents" contains a process called "Windows Internet Explorer".
"Windows Internet Explorer" ~~ later for this interesting (to me) tangent.
("Script Documents" disappears when I close IE7; the reason I see it is because
I've click "Show All Files" is Solution Explorer.).
http://localhost:50316/Article/Edit/6 off to edit an existing article.
public ActionResult Edit(int id)
{
var articleToEdit = (from m in _db.ArticleSet where m.Id == id select m).First();
// todo: why and where are status flags changing from true to false?
bool highlightValue = articleToEdit.highlight; // debugging
bool newsArticleValue = articleToEdit.newsArticle; // debugging
return View(articleToEdit);
F11: both bools are true at this point. Mousing over the return shows _highlight and _newsArticle as true
F11: stepping through Edit.aspx, <%= Html.Encode(Model.highlight) %> is true;
<%
= Html.Encode(Model.newsArticle)%> is also true.
Here, editing nothing at all, I submit the Edit.aspx page.
[ValidateInput(false)]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Article articleToEdit, string changeControlString, string continueEditing)
{
if (!ModelState.IsValid) return View();
try
{
// todo: why and where are status flags changing from true to false?
bool currentHighlightValue = articleToEdit.highlight; // debugging
bool currentNewsArticleValue = articleToEdit.newsArticle; // debugging
var originalArticle = (from m in _db.ArticleSet where m.Id == articleToEdit.Id select m).First();
// todo: why and where are status flags changing from true to false?
bool originalHighlightValue = originalArticle .highlight; // debugging
bool originalNewsArticleValue = originalArticle.newsArticle; // debugging
currentHighlightValue and currentNewsArticleValue are both false;
originalHighlightValue and originalNewsArticleValue are still true.
_db.ApplyPropertyChanges(originalArticle.EntityKey.EntitySetName, articleToEdit);
The above statement will flip the values to false even though in theory, I am not responsible for this change!!!
ASP.NET MVC
why do you have to torture me?
M V C
1 2 3
what is it that I've done to thee?
MVC says "don't blame me".
It's that jerk Entity Framework!
"Blame EF!" says MVC,
"it's probably another EF quirk".
Personally, I do not think these values should be flipped. My text fields do not get messed with. Therefore, these fields should also be left alone. I think the cause has something to do with their being set to do not allow nulls in the .mdf file.
Note: <%= Html.Encode(Model.Id)%> does not get messed up.
I'm going to kludge this for now instead of playing around with ideas like:
[Bind(Exclude = "hightlight, newsArticle")] ~~ perhaps Bind will work?!
Here's the kludge:
articleToEdit.highlight = originalArticle.highlight; // kludge
articleToEdit.newsArticle = originalArticle.newsArticle; // kludge
Somehow, it seems strange and unnatural that I need to kludge bool fields like this.
Any idea why?
Thank you.
Regards,
Gerry (Lowry)