am new to MVC but have a Q about PartialViews. I have two PartialViews accessing Model Data from the ParentView (Model Data passed through via the ParentView's Controller). The first PartialView is used to update (add/delete values) Model Data to the database. The second PartialView generates a document based on the ParentsView Model Data. The problem is that if the data is changed in the database by the first PartialView then the ParentViews Model Data is now out of date and hence the Second PartialView (referencing the ParentsView Model Data) is now working with out of date data.
I realise the above should be re-engineered to to better suite, however is there a way to make the updated Model Data available at the ParentView level for the second PartialView to reference?
Usually in order to update something into the database an HTTP request is sent to the server and the controller action performs the update and renders a view meaning that the whole page is reloaded and the model data updated.
If you perform an AJAX request to update the database then you might need to update the second partial view as well so that changes are taken into account.
Related
I am using PRG pattern for form submission in mvc. But In most of the sites suggested me to use temp data in PRG instead of view data. But I don't know how to judge this.
View bag and view data Lives only during the current request from controller to view and Temp data also Lives only during the current request from action to action or controller to another controller.
Can we use View data instead of Temp data for PRG pattern ?
Why we need to use temp data instead of view bag, view data ?
in prg pattern you redirect after a successful form submission wheres as ViewData is saved after a redirect whereas temp is saved using the same storage that session uses.
what i mean by this is if you have saved any data in ViewData like
ViewData.CustomerId = 67; it wont be available to you after the form is submitted and the user is redirected to another action/controller
where as if you save data in TempData it will be saved for at least the time where you access it once
I have a view, model and a controller, I want the user to start editing the page of a new contact.
They will
Enter the First and Last Name
Click Save
The Controller commits the save.
Expand the page to Show display name for editing.
In forms Asp.net I stick the Primary Key of the Saved record in the view state so on the next save, I do an update vs. an Insert.
How do I do that in MVC, Razor? I have seen examples using a Hidden Field but I would think there is a better way. I would prefer it not shown at all, or at least encrypted but I do not want to build the encryption or decryption routines.
User HiddenFor() method of Html helper class. #Html.HiddenFor(model => model.Id). By the way, ViewState is stored in a hidden field in ASP.NET.
If you are really wanting to encrypt the data like in viewState, you can use Html.Serialize()method which serializes and encrypts entire model in view and you will then have to De-serialize it in the controller after it's posted. Have a look at this article
Here's what happens:
Partial views A and B show the same model
View A changes the data via Ajax (using Ajax.BeginForm)
View A re-renders itself (controller gets the updated model from the database)
Now we have to re-render View B as well, because the data has been changed, right?
We get the updated model from the database again and re-render View B
How do I prevent re-querying the database? Maybe cache the model-instance in the Session?
What's the "right" way to do this?
<!--works great when the page is rendered
via postback but what about Ajax?--!>
<div>
#Html.Partial(#ViewA", MyModelInstance)
#Html.Partial(#ViewB", MyModelInstance)
</div>
Can you create a new partial View C that contains both A and B. Whenever the model changes you have to invoke the action that returns the View C.
UPDATE:
The other simple solution I see is when View A updates the model instead of re-rendering the view, get the updated model through AJAX and the update HTML portions through javascript. If you are using jquery you can use the template plugin to update the html quite easily.
By this way you can avoid making one more unnecessary request to update the other View B.
How do I can persist data in MVC Razor without using TempData between requests?
I see when we can use TempData from this, but don't want to TempData as it creates a state on the machine.
Thanks, Anish
EDIT: I want to persist the previous sort direction in a View page where a user is allowed to sort fields such as Name, Age etc.
FIX: I fixed it using ViewBag. Previous sort field/direction sent to View from Controller using ViewBag and then passed it back as query string in the next click.
Good FIX: I handled the everything in .js file such as checking and then in setting the previous sort field and previous sort dir in Controller.
This is what I finally did. I used ViewBag to send previous details to ViewPage and did the validation in a .js based on the current user action and passed it back to the controller in form-data.
Maintaining State in client page is something which is breaking the concept of HTTP which is stateless. Why do you want to maintain state ? If you are looking for some solution for Passing some data from your controller action to corresponding view, i would suggest you to go with a ViewModel where you fill the data for the dropdown and send that ViewModel object to the strongly typed view. You will have your data available there. Also you should grab data from your DataLayer ( from tables/ or Cache or etc..) in every request if you want some data.
You may pass a relevant id in the query string to get the corresponding data.
As RTigger mentioned, you may store some data in session. That will be available across all the pages till the life time of that session.
I haven't done a lot of ASP.NET MVC 3 recently, but I think the only real way you can persist data across requests is either with session state, cookies, or by posting data to the server every request.
ViewData, ViewBag, and TempData are all effectively killed at the end of each request, whereas session state, cookies, and post data is made available at the beginning of every request based on information from the client browser.
What particular scenario are you trying to persist data for?
You could pass those values as query string parameters when redirecting.
You can set parameters to hidden input fields if you post the form. But if you gonna call action by HTTPGET then you can pass values by using QueryString parameters.
I'm wondering how you keep a constant value of the Model in an ASP.NET MVC framework. Like when adding something to the Model through the view. You go back to an action in the controller but where do you keep the Model? Is it a private in the controller? or is it passed back and forth from the view to the controller because if it gets big then you are passing alot of data back and forth to add/delete a single item from the Model.
Also any small examples to show this?
Thanks
What are you referring to? Are you meaning a database table loaded up into an object such as Ruby on Rails's ORM -- typically the 'Model' is a series of interfaces or domain objects that load data into objects from the database .. or more simply just the database period.
Please be more specific. There are many MVC frameworks and many different kinds of 'Models'
I think you should check out a ASP.NET MVC tuturial like NerdDinner (from "Professional ASP.NET MVC 1.0"). Scott Guthrie has posted a html version of the tutorial on his site. It's a fairly simple site that they build in the tutorial, and is a great intro to ASP.NET MVC (in my opinion).
There are also some good tutorials on the ASP.NET site.
Hope these help you with .NET MVC, it's a great framework to use!
You can pass the model to the page and you can then use UpdateModel(model name) within your controller.
Each member in the model must be a property with a getter and a setter.
On the page you can hold the data in say a hidden field if you need to maintain the value outside of state.
If you have problems using UpdateModel then you can use the following in your controller;
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult MyAction(int? id, FormCollection collection)
{
string commentText = collection["myFieldName"];
}
This will normally get your values from the model.
Hope this is what you were asking.
Think of the model as a data transfer object. In a list, display only or edit page, you pull it out of a data layer as a single object or a list of objects. The controller passes it along to the view and it gets displayed.
In the case of an insert, a new data transfer object is instantiated upon post back and is updated with the posted values. Then sent back to the the data layer for persistence.
In the case of an edit, it comes from the data layer on the HTTP GET request and is used to pre-populate the HTML form. Then on post back, the data transfer object gets updated with the posted values and sent back to the the data layer for persistence.
Definitely checkout NerdDinner or Stephen Walther's samples.