Saving data from MVC view without navigating to controller - asp.net-mvc

I would like to save data from MVC view to the database without navigating to the controller? Is this possible? If so how can I achieve it?
Thanks in Advance

While technically possible, all backend access should happen in the controller, if you want to stay true to the MVC design pattern.
Note that you can submit data to a controller action using AJAX, so the page from which you send the data will not be reloaded. For example, use jQuery.post.

Related

Maintaining input control state in MVC

In the app I'm working on the administrator has the ability to select a customer to work with in a drop down list. Thing is that there are a large number of views that the administrator can go through and each time they'd have to select the customer again. In webforms this was rather easy... store it in a Session variable and reset it when another page loads.
MVC not so much. I seem to be stuck at the point where I pass this value to the view from the controller. We are storing the value in a Session variable which we access using a base controller like this:
MyController.CurrentUser.CurrentCustomerId
The question I can't solve is how I pass this value to a partial view. The customer selector tool is in a partial view which is added to pages that need it.
I thought of using the ViewBag, but that means that in every single action in my controllers that requires this value I would have to add:
ViewBag.CurrentCustomerId = CurrentUser.CurrentCustomerId;
And even then I'm not sure if the ViewBag data is carried through to the partial view. I think it is.
Seems like there should be a more efficient way to do this and still abide by MVC rules?
#Andre Calil provided a very good solution for carrying user information between controllers and views and I ended up using this approach.
See his answer at the following link: Razor MVC, where to put global variables that's accessible across master page, partiview and view?

Persist value across multiple views in mvc

I have multiple tabs in an MVC page. Each tab is a seperate view aspx with same controller. I have a common master page.
On saving the data on 1st tab I get an ID which I want to use in other tabs of the page.
User can click any tab after saving data in 1st tab.
Currently I am using TempData to persist ID across tabs. I want to persist data without using TempData or cookies.
Is it possible to persist value in master page? Is there any other approach better than this?
Note: We have load balancer for the application.
I'd recommend ViewData rather than TempData as TempData disappears the first time it is used.
Based on your question though, I assume if you don't like TempData, than ViewData won't be what you're looking for anyway, but I do think it's the best solution for your scenario.
Create one page with several tabs on it, visible part you can manage by jquery tab plugin (eg. http://jqueryui.com/tabs/), you won't need to persist it on server side.
If you need this id as a part of your model, you can include it into the other view's routes like ../tab/<Your_Id>/. You would not have to persist its value in ViewData or Session or Cookie.

Use web-services instead of a model

I want to build an ASP.NET MVC3 application but instead of a model I would like to use a web-service. (WVC -Web-service - Views - Controller instead of the traditional MVC3, Model - View - Controller).
The reason behind this selection is the need to use the same database for a Windows Phone Application and a Website.
Which is the most "proper", "correct" way to use webservices in my application? Is it better to call them from the Controller? Or use them from the Model?
I am new in Asp.Net MVC3 and I would appreciate any help/suggestions.
Thanks
I'm not entirely sure of what you mean, however my take on your question would be as follows:
If your web service is serving up xml or JSON you would consume the service & deserialize related data at the controller level. In the event you need to present information to a user you would populate a model / view model from the previously deserialized data and render a view.
In the event data needs to be passed back to the service the data would flow from the view into the controller to be serialized and sent back to your webservice.

Hidden session Id in MVC Master Page

I have an MVC application, and I need to create and store a unique session Id for each application instance. In standard ASP.NET this is easy, I would have simply added a hidden field in the master page and stored a Guid there on the first Page_Load.
However, there is not code behind in MVC, and I also believe that you can't implement a controller for a master page OR create a strongly typed view master page.
Anyone know an easy way around this? I'm just playing around with MVC for the first time so be gentle if what I'm asking seems stupidly simple.
Thanks
If you need to persist a few objects for the lifetime of an instance of your application I would recommend you using the built-in Cache object or Application. If those objects need to be specific to each user then use the Session object.
Why not store this in a cookie?

ASP.NET MVC Page Editing Contrast To Web Forms

In our old system we had pages rendered from XSLT. In order to change the page into "edit" mode we would have a button of some sort, once clicked would have an EditYN flag which would be passed to the stored procedure. The stored procedure would simply give this variable back to indicate that the page was in edit mode. This meant that query strings, viewstate or session data were not required to indicate that the page is in edit mode.
I've been dealing with ASP.NET MVC only for the last week for RND purposes at work. I'm wondering what's the best way to have a page which displays data, to then turn into a page where you can edit all of that data? Should you move to a separate page? Should you stay on the same page and have rendering logic in the view to show the edit mode of the page?
Whilst on the same topic, I thought I'd also ask about GridViews and their place in the MVC architecture. Beforehand we'd simply use data sources and set them up with the GridView. Then the GridView could enter edit mode quite easily by itself with the UPDATE query set in the data source. How should this process be done using MVC?
Make a submit button for your edit mode. The controller action will respond to POST, set the "InEdit" flag in your model, then return the same view again. The view can then render based on the flag. But I would rather create two different views, for view and edit modes, then based on the flag analysis done in the controller action just return the one or the other view.
Whilst on the same topic, I thought
I'd also ask about GridViews and their
place in the MVC architecture.
How should this process be
done using MVC?
You can use javascript, something like jqGrid or Yahoo´s datatable. For simpler datatables, you can use jQuery´s tablesorter.
On the backend, your controller returns json objects that can be modified by the client, ie. the javascript grid which then sends the data back to the controller to be saved.

Resources