Refresh page with new data after Update in MVC5 - c#

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.

Related

TwoWay data binding with Realm in master/details views

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??

How to return an object from controller to View which may not be model

I am very new to MVC and I am not too conversant with the best practices here. I am facing a design issue which may be common or uncommon to newbies like me. My problem is the following:
I have a page with two parts in it.
Grid Control (with Employee basic info in it)
Employee Details (When someone clicks on Grid row, it loads all the details about the employee)
I am using KendoGrid and it is getting all its data from an ActionMethod from my controller.
Now, when I click on the row, I have the following options:
I call some ActionMethod in Controller and return all the Details
Should I use partial View with a separate model so that ActionMethod in response calls RenderPartialView()?
Should I NOT create a partial View, have Actionmethod return JSON and parse it in the Model?
3.1 If I go for this option then would the JSON be part of model?
3.2 If it is not going to be part of model, how can I use JSON to render the View?
Or probably I am missing something basic here?
Yes you can use action method returning JsonResult. what needs to be done is keep the uielements you need to show on the click of grid row in the page itself. initiate a ajax call to the action method and on success update the values of the UI elements from the values received in JSON and make the entire DIV as visible which holds the total information.
Instead of passing entire HTML over the network I think you can opt for json.
An example you can find at following location
example

How to add multiple values into a page, and then to controller in ASP.Net MVC

I have this scenario to implement in ASP.Net MVC.
In my view, I would like to have a dropdown with a button , where on click of Button, will add selected values into probably a Div table. There will be a separate 'Save' button which will call the ActionResult POST method to save these values to the database.
Here, I could probably use AJAX to add values into the Div (However I am not sure how I can pass the values here to the controller once user clicks on the 'Save' button).
I was also thinking I could probably do a postback and add the values into a Session value (Probably an ASP.Net Web Form technique?) and call the ActionResult POST Method once the user clicks a separate 'Save' button.
Hence, in an ASP.Net MVC framework, what would be the best way to implement this scenario?
Thanks.
You can put all you hml into a #html form and then on save buton click you can either post that for to server or you can write a click event of that form by making ajax call.In ajax call you can send form data by form.serialize method.

Redisplaying old data on a view mvc

I have an MVC application in which I create a search page to find projects I've created. Once the user enters the search criteria, the data is passed back to the controller as a model object and I take this data to search for any projects that match. The search page is redisplayed with the original criteria the user entered plus any projects that were found.
The user can now go an click on a row (the results of their search) and edit that data on another page. When the user hits the save button (on the other page), I save the data and go back to the search page. I don't have the original data (the search model object) to redisplay. How do I get it back? I'm thinking that I should just thorugh my model object that I got when the method was called through the controller into my session object and then retrieve it when I come back to the search page. Is there a more elegant solution to getting the data from the model object back when I return to the search page?
You can use TempData["whatevever"]= yourObject and the data there will exist until it is read the next time and then it is removed. Its like the session object, but is removed after you read it.

Ways to detect changed account/no account found in ASP.NET/C#

I have an ASP.NET page where at the top of the page is a search box. There are 2 text boxes - one is an autocomplete extender for the Name on a database, and one is just inputting the ID.
The page features DetailsViews and GridViews primarily, and even when no account has been searched for, these display blank data which is not ideal. I sort of fixed this by using if (IsPostBack), encasing the elements in a placeholder and setting it to visible only if the page ispostback. But this doesn't cover if the user types in an incorrect ID.
Also, some accounts have huge amounts of data inside the GridView's. I had an issue where because I have no way of detecting when a data source's rows has changed, I end up binding whenever the page loads (Page_Load method). I've come to realise this is simply very bad - there are lots of times when the user can click various things in the page and have the page postback, and it takes an eternity to load each time I click something for accounts with lots of data.
Anyway, my question is essentially two-fold but I have a feeling the solution will be similar:
1: How can I detect when there are no accounts returned when searching, and disable the Grids/Detailsviews and show an error message?
2: How can I figure out when the user searches for another account and only rebind the grids after that has happened?
Thanks
This method is very ugly but it'll get the work done.
1) To Check whether there are no records; after the AutoComplete Extenders Webservice is called if no record is returned put some value in Session like
Session["NoData"]=true;
if Records are found then;
Session["NoData"]=false;
after the webservice is called do ajax request to check that session & on the basis of value do what you want.
2) You can achieve this also by following the above option.

Categories

Resources