MVC Default Edit View increments the id property of my model? Can't figure out why. . - entity-framework

I'm trying to learn web development, and I bet that this is a simple problem and that I'm overlooking something obvious.
In my default crud edit controller (generated using the MVC framework) I retrieve my model from a localDB instance using my EntityFramework's dbContext. That is sent to the View. In the debugger just before the controller call to return View(model) I can see that the Id is set to 2.
When the [post] edit controller is fired I see that the identity property is 3. I wanted to figure out why so I changed the View to display my Id property and I see that it is 3 as soon as I render the page. Last time I saw it the property was 2, now it is 3.
I don't know how to hook into any logic that would happen between the time I send off my model and when when the view is rendered.
Can anyone help me learn how to debug this so that I can figure out why my Id property is incremented when I pass the model into the view?

I don't know how EntityFramework works but I have worked with CakePHP. My advice is like this:
Usually PHP frameworks have debug mode which you can set in the configuration file (turn it on or off). Usually stack of operation executed is also displayed in debug mode or there is a simple way to do that. There's also for example in CakePHP exist function debug($yourVariable); try to search for sth. similar
It seams like not the edit happens but new row is inserted to the database. Check your database for this. I recommend to debug the id of the column being passed for edit action and check if there's the same id in the database first of all.
Hope something helps.

I figured it out. Posting here for anyone else that comes across my problem.
I tried dumping the whole model out without using any of the htmlhelper methods. (In my case I am uinging: Html.HiddenFor helper) When I did this I saw that the value in the model was what I expected it to be. So I began investigating why the helper methods might be broken. Google worked for me here :)
Turns out, when the helper methods run they first check the ModelState dictionary for the desired value. In my case I was looking for a value that was in my model object as well as the ModelState dictionary, because the name was very common: Id.
To fix the issue before I call return View(model) I call ModelState.Clear() in order to make sure there are no values in the dictionary that are in conflict. Doing this causes my page to be rendered correctly.

Related

PersistenceManager does not persist the last entry in TYPO3

first of all, i searched a lot but can't find a solution.
I wrote an Importer, which imports data from a xml file to TYPO3 via CommandController.
Basically everything works like charm, but the frontend does not display the second (and last) phonenumber, until i reopen the corresponding Object in the Backend and press "Save".
I did not change anything in the BE, just pressed "Save" once again.
What ive already tried:
Persist the datas more often, especially after adding the phoneNumbers(its a DataStorage Object).
with:
$this->thingsImportRepository->update($person);
$this->persistenceManager->persistAll();
As i said, everything is saved correctly, just the second phone number won't show up in the Frontend.
What could i have possibly done wrong?
Thanks!
BR,
Martin
The solution basically just was a detail.
All data had been persisted correctly. At first i thought it was a cache-issue.
But:
The Solution:
The sorting_foreign property was not set correctly, so the sorting were everywhere 0. After saving it in the Backend the sorting was correctly numbered. Adding the setForeignSorting($sort); in the controller before persisting the object has fixed this issue

Setting a value in Frappe application isn't reflected in ERPNext GUI

I have added a 'number_of_members' value to the Customer DocType via customization.
In my application I have tried several ways to update the value. However the value never updates in the webpage. I feel like I'm missing some sort of save or update or commit step.
For example I have tried:
frappe.client.set_value('Customer', '00042', 'number_of_members', 8887)
frappe.set_value('Customer', '00042', 'number_of_members', 8887)
frappe.db.set_value('Customer', '00042', 'number_of_members', 8887)
and also
customer = frappe.get_doc('Customer', '00042')
customer.number_of_members = 8887
customer.save()
In each case I can do something like frappe.get_value, or frappe.get_doc and it shows the value is set to 8887. However it never updates in the web side. This is what makes me think I'm updating some sort of cache or database transaction and I need some way to save it, but have not had any luck.
I am mostly testing this via bench console if that has any bearing on it, but I've tried a couple of the methods in my application code as well.
Relevant documentation:
Frappe Developer API - Document
Frappe Developer API - Database
Turns out the answer is to call frappe.db.commit() after making changes. If someone can point this out in the documentation so I can better understand how I'm missing stuff, I would appreciate it.
I also noticed if you try to Save something in the UI before you send frappe.db.commit() the UI will hang.

Stopping Ember.js Controller

My question is very basic on one hand but on the other hand the general situation is more complex, plus I cannot really get any working sample.
I'm developing/maintaing a web-application which is currently in transition from GWT code base into Ember.js.
Most of the newer code already relies on Ember.js and I think it's really awesome.
The problem is we cannot use Ember Router as all the request are being handled by the GWT.
In order to enabled the application run in this unusual configuration we have special JavaScript files that create our Ember main objects (Controllers & Models) for us.
As you can imagine navigation between tabs is cumbersome and is handled by GWT who creates Ember objects when needed. We are in transit toward a brave new world Ember Router and all.
But in the meantime, this is the problem I'm facing right now.
The user clicks a link which opens a page that contains some Ember based table.
The data is retrieved form the server using some Ajax code. Upon success it spawns a forEach loop which tries to pushObject all the received date into our Ember based components.
My problem happens when the user quickly switches between tabs. In this case the first list of object has not finished rendering yet and suddenly there's a new set of objects to handle. This causes Ember to throw errors like:
"Uncaught Error: Cannot perform operations on a Metamorph that is not in the DOM. "
and
"Uncaught NotFoundError: An attempt was made to reference a Node in a context where it does not exist."
Is it possible to prevent the loop from trying to render?
I've tried checking if the controller in question is already inDOM and it is, is there a way to notify Ember this object is no longer valid?
Sorry for a lengthy question and lack of running sample.
I'd probably modify the switch tab code to only execute afterRender has completed, that way you aren't mucking with ember objects while they are being used.
Ember.run.scheduleOnce('afterRender', this, function(){
// call GWT switch tab routine
});
Thank you Daniel and Márcio Rodrigues Correa Júnior. eventually what I did is to add a patch that would check the current context of the application (in my case the currently selected tab). If upon receiving the AJAX response the application is in the correct context (meaning the user haven't change the tab) go on. Otherwise just ignore the response and do not try to render it.
Now it seems to be working

Zend Sessions not stored for step3

I have used zend session as bellow..
$test = new Zend_Session_Namespace('test');
$test->test = "test text";
But when i move from one step to another step its working fine and echoes value Not on step3. even i tried using $_SESSION direct (because am desperate this to work ). But i have same problem that session not stored. Site seems almost broken when i use mozilla and IE. Not only in my PC But i have tested in multiple systems.
Tried clearing cookies, used cookie enabled browser only.
SO please suggest me what could be the posible problem in doing above. even i have inserted zend_session::start();
Thanks in advance,
Sanjeevk,
You haven't provided any useful code for what you are trying to accomplish, so I'm going to guess.
I have encountered a problem similar to this as well. My issue was simply trying to persist a page number from the paginator so I could perform an operation on a record and then return to the same page in the previous action.
The problem I was having was that the controller would overwrite the session data every time I called the action (page refresh), so while the page number would persist to other actions as soon as I called the original action the session would be over written and the page would reset to 1.
My solution was to get the page number from the view script and then feed it back to the controller later.
//the view script
<?php $session = new Zend_Session_Namespace('page'); $page = $this->paginator->getCurrentPageNumber(); ?>
so now when call $session->page anywhere in my controller/actions the data is still there and doesn't overwrite until the view is actually rendered, which is exactly what I needed.
You may be encountering something similar. Because of the way PHP and Zend Framework operate you may be inadvertently over writing your session data.
If this doesn't help, please provide more information and maybe an answer can be found.
Hope this provides some help or at least hints.

How to show previous url after user has canceled dialog with message from Activity#mayStop()?

In our app we need to check if the data is saved when we are in a particular place before navigating away from it. So the user should be able to negate a browser back button request. But by the time that the history value change event is received the url has already been changed. The History class doesn't seem to have a way to restore the url back. Anybody have any ideas?
In GWT 2.1 you get Activities and Places. And activity has a maystop method, which is exactly what you want, if I understand you correctly.
Use a window.onunload or window.onbeforeunload javascript callback to confrim/save state.
onbeforeunload example
I haven't actually implemented this behavior yet, but here is my plan and maybe it will work for you.
1) Each time you receive an onHistoryChanged event and decide to allow it, save the current historyToken in an instance variable somewhere.
2) Keep track of activity on the page that should block navigation. Use a data structure that can keep track of multiple activities, like multiple file uploads, multiple edits, etc.
3) When you receive a new onHistoryChanged event, if your data structure from #2 indicates that it's not safe to navigate, avoid changing the page and restore the historyToken that you saved in #1. I'm assuming that you can do this either by:
a) Calling History.newItem(oldHistoryToken, false) or
b) Calling History.newItem(oldHistoryToken, true) and keeping a flag to force the next onHistoryChanged to be ignored.
Again, I haven't actually implemented this so let me know how it works out.
If you have links that allow the user to leave the app and you want to prevent that as well, you'll need to also add an onbeforeunload.
Have a look at the PlaceManagerImpl class from the gwt-platform framework. Especially the onValueChange() method and the methods dealing with the onLeaveQuestion field.
Hope that helps.
In this issue report, t.broyer explains in his comment that such behavior was planned during design of Places framework. The most important part is:
mayStop was a mistake, or it should have only been called when unloading the app, not for internal navigation within the app.
So probably it's better to not use it at all...