TwoWay data binding with Realm in master/details views - c#

I am working on a project with master/detail views similar to the QuickJournal example code. The master view is GridView bound to a collection, and the detail view is bound to an individual item in the collection.
When I navigate from the master view to the detail view, I am starting a new Transaction. If I make changes to the detail view and save the item (commit the transaction), it will navigate back to the master view and everything is fine.
My problem is that if I do not make any changes to the detail view and try to navigate back to the master page using the back button, I get an exception "Cannot set values outside transaction". I have tried a couple different ways:
1) _trans.Dispose(); in the detail page OnNavigatingFrom handler (described above)
2) _trans.Rollback(); in the detail page same as above
3) leave the transaction open when navigating back; this doesn't throw an exception when navigating back to the master page, but instead throws an exception when trying to go back to the detail page again since there is already an open transaction
4) setting the current databound item to null before navigating back to master view; this causes different problems because you can't bind to null
I have seen that there is a Realm.DataBinding package that is supposed to automatically handle transactions as needed, but I cannot find any examples of its usage. Is there a way around this issue without making a duplicate copy of the object in question and using that in the detail view, and just throwing it out if you don't make any changes??

Related

Refresh page with new data after Update in MVC5

I have a MVC5 page that is shows Customer's information in Text box then you can update some of the data and push Update button. it is going to update info but it is not showing the new value after pushing the update button, you have to leave a page and comeback to see a new value.
UpdateCustomer and (presumably) UpdatePassword should not return a View but instead should return a RedirectResponse back to the [HttpGet] version of the Edit action. This is known as a Post-Redirect-Get pattern and is the preferred way of redisplaying the same page the user just edited. By returning the view with the same model that was posted you're just showing the user what they originally typed in. You're also leaving yourself open to multi-postback situations if the user tries refreshing their browser.
You also seem to be fighting the framework. If you've got two buttons on the page that perform two different actions you should have two forms that each POST to a different controller action.

Knockout partial view dynamic lazy loading

I am currently stuck with a scenario that I need to achieve and I am hoping you would be able to help me.
The scenario I am facing is as follow:
I am displaying a list of objects with a basic summary of information. These objects are populated from c# code via a web service call that retrieves the data. For each one of these objects that are then displayed there is a select button. When this select button is clicked, another div right below the current summary div should load a partial view. This partial view must also retrieve more data via web service and display the detailed view for the current object that was selected.
I have read the article on using the ko.onDemandObservable (http://www.knockmeout.net/2011/06/lazy-loading-observable-in-knockoutjs.html) which seems to be doing what I want, but I am unsure on how to re use the same template as detail view for each summary object.

C# ASP.NET - Dynamic MenuItems in a user control are randomly being duplicated outside of control creation

EDIT: I needed to skip control creation during post back -- see my answer below.
I'm working on a very basic front end to a simple tool and I wanted to present some data in a much more sorted and useful way, instead of making one huge wall of text. I found this tutorial on building a simple tabbed interface using MultiView, but have run into a bizarre problem. I can't use Ajax tabs because of legal hissy fits over 3rd party software.
My webpage is a basic ASP.NET page with a user control plopped in the middle of it. In this control's ascx file, I defined the Menu (empty) and the MultiView (also empty) so that I can dynamically populate the tabs with content driven from an external file.
When the default page's OnInitComplete function is called, I call through to the user control to load the data file, then build out the tabs and the view content based on the loaded data. I tried doing this from PageLoad, PreInit, and CreateChildControls, but I kept getting an errors saying that I was setting the the MultiView's active view index at an invalid time (and also that there were 0 views despite the fact I just added a bunch of them):
ActiveViewIndex is being set to '0'. It must be smaller than the
current number of View controls '0'. For dynamically added views, make
sure they are added before or in Page_PreInit event.
But OnInitComplete appears to work just fine, so I went with that.
I iterate over the loaded data (multiple lists of strings), and for each list, I add a MenuItem with the list's title to the Menu and a View to the MultiView. The View is populated with a table->row->cell as in the above tutorial. In the cell, I add the list title and a CheckBoxList data bound to the list of strings.
So far so good, but when I click on a tab (or one of the checkboxes, etc) and there is a postback or something like that (the screen flashes as the site redraws itself), there is now a duplicate set of MenuItems immediately after the original. Each time I click on a tab or checkbox, another set of menu items are added.
I clear the MenuItem's Items list prior to building the controls and I verify that the controls hierarchy is structurally as expected after the control construction. Yet when one of my callbacks is called, my MenuItem list magically has some items added to it. None of my other controls appear affected at all. As a hack, I can remove the duplicates manually in my menu's OnMenuItemClick event, but I'd have to do the same in any of the callbacks I receive. Obviously I'd rather prevent this from happening. This has me stumped and I haven't been able to find anything online about it. Why would one set of controls have some content duplicated, yet every other control maintain its state correctly? My code is really simple so there isn't a way to add additional menu items without also adding the views. Anyway, there are a correct number of items prior to clicking on the tab/checkbox, an additional set immediately following in the callback.
This is my first time using ASP.NET, so I'm learning as I go. :) Thanks!
My problem was that I was not testing for postback before creating the controls. The code below is working for me.
In my user control's code behind:
protected void OnInitComplete( EventArgs e )
{
if( !Page.IsPostBack )
{
CreateMyControls();
}
}

WPF binding issue while using NavigationCommands.BrowseBack property

I wonder why this behavior happening in my page.
I have a page (say 'x') which has set of text boxes which are defined inside a container say a stack panel and the data is bounded with this stack panel using datacontext in c#.
now when I am navigating to another page and again moving back to that page (page 'x') (URI Navigation) the data binding will happen and the fields will be filled buy the object's data as the getter method of the object's property will get called ,but by pressing back button (NavigationCommands.BrowseBack) there will be no data binding happens in the page. no field will take the date as the getters method of properties of the object will not called in this case!!
I don't want to do it in back end by reffering to each text box and assigning values to them.
any one having the same issue or solution for that please share.
You might try to update your bindings manually. For example you could write the following code as an extension to "BenCr's" answer in this post Is there a way to refresh all bindings in WPF?
var temp = yourStackPanel.DataContext;
yourStackPanel.DataContext = null;
yourStackPanel.DataContext = temp;

Using Commands declared in "Parent" ViewModel (MVVM)

(Note: I chose to not use the Navigation Framework)
I have a WizardViewModel which is linked to WizardView.
The WizardViewModel declares and instantiates a command "Next".
It also contains a Property "ActiveSpell" of Type SpellViewModel.
The SpellViewModel contains several PageViewModels, each having a View counterpart.
The ActivePage Property (on SpellViewModel) tells the ui which view to take.
Now I have the following problem:
When I click a button to switch to the next page,
I need access to the "Next" command defined in the WizardViewModel,
but I only have access to a PageViewModel there.
I could just add a Parent property to each child ViewModel,
but I'm not sure if that is a good idea.
Or maybe there is another nicer/common way to do that.
You can use Event Aggregator, to adjust the interaction between ViewModels.
You don't need Parent property. Your view model structure is good, just look at the picture, to understand how you should bind your view model onto the view:
Next command should be implemented something like that:
public void NextExecute()
{
ActualSpell.MoveToNextPage();
}
UPDATE:
According to your comment, Arokh, I've updated the post.
I think, in this case you should implement ActivateCreatePersonSpell command in WizardViewModel.This command should:
save actual spell state
open CreatePerson spell
once person is created set saved spell with result of creation person
The last what you need to do is to bind ActivateCreatePersonSpell command to button on the page. I propose to use ViewModelLocator for these purposes.Look at this post for example.
I had to implement a wizard once and I liked and mimicked the way Josh Smith and Karl Shifflett set up their WizardViewModel and wizard page view models in this example project (source code available with the article):
http://www.codeproject.com/KB/WPF/InternationalizedWizard.aspx
They kept the Next command as part of their WizardViewModel, but created a WizardPageViewModelBase that all of the wizard pages derive from. That allowed the WizardViewModel to control which page is the current page, and it allowed the WizardViewModel to query the current page view model to see if the Next command can execute, thus enabling or disabling the Next button on the wizard. (That is, the wizard view model knew about the page view models, but the page view models didn't need to know anything about the "parent" wizard view model.)
As for adding links to parent view models, it's an approach that works, and I've done it before when I started working with MVVM, but after time I found the approach to result in some difficult to maintain code as every view model becomes interdependent.

Categories

Resources