Calling cached data (in service layer) from an ASP.NET MVC View - what other alternative do we have? - asp.net

We are adding tooltips to our ASP.NET MVC product, and we are getting the text from our database (technically, from a cached copy of the data). To do so, we created an Html Helper method:
<%=Html.Tooltip(Model.GetTooltipText(Tooltips.ClientPage.StartDateId))%>
The GetTooltipText method is in our BaseViewModel, and simply uses the passed in Id to fetch the tooltip from cache.
Would this be considered a bad design? What other alternatives would we have?
Thanks!

Its probably a better idea to grab all the Tooltip's in one hit and put them in some sort of strongly-typed collection (perhaps a Dictionary<id,string>), cache all of that in your service layer.
Then you could put this in a ViewModel and pass it through to your strongly-typed view.
In your View you could simply access that strongly-typed collected via the Model based on the unique key?
I.e.
<%: Model.Tooltips[SomeDateId] %>

Related

Is it a good practice to access session data in view?

Which is better from design and coding point of view: accessing session data in the view directly or loading them in the controller and passing them to the view using a viewbag?
MVC wont really care if you do something in your controller or your views but i agree with shaftpolls for not hurting the mvc pattern. Since Session is already inside the ViewData wrapper, you can access it directly in the View.
Be aware, that the Session object should only contain simple, non-sensitive data like user picked language, or custom ui-colorsheme.
If you really need to use Session, you should get that data on the server side and then pass it to the View using Strongly Typed properties in a View Model rather than using the ViewBag.
So let's say you have a property on your view model which is of type IENumerable<SelectListItem> and inside your view you will use the strongly typed version of the Html.DropDownListFor helper to bind to the model this way:
#Html.DropDownListFor(x => x.ProductId, Model.Products)
The best thing to do is access the session in the controller. Why:
Performance. You have fine control of how to access the Session object. If you need to read just properties of the session, then specify that you need read-only access to the session, MVC won't block the session, you'll get more performance.
Pureness. In a pure MVC scenario, the view knows only about the model. So any value that you care about in your session must be passed as part of the model.

In ASP.NET MVC, Data exchange between Model and View is possible?

Is there a option to pass data between Model and view in ASP.NET. If it can be accomplished how? This was asked in an interview !!!
Yes... well, the view is either typed, which means the object that represents the model is directly accessible, or the controller returns a ViewBag with data or anything else the view needs to render. That's the whole point of the model-view part of the pattern.
The whole point of MVC is that the data(Model) should be shown to user (View) by use of Controller. Even if the view is typed, your still need the model to bind to it. Even to create a ViewBag, you need the controller action to fill it...

asp.net mvc3 / razor view best practices

I am using in my views (asp mvc3/razor cshtml) references to the Request object (eg, #Request.Params["Name"]). Do you think this is a very bad practice? Should I rewrite the value in the controller Request.Params ["Name"] to ViewBag.Name and then use it in the view (#ViewBag.Name)?
Best practice is to use a model class. An instance of the model class is created or updated in your controller. Then the controller displays a strongly-typed view.
So I'd avoid direct access to the request from the view as well as the use of the view bag.
Should I rewrite the value in the controller Request.Params ["Name"] to ViewBag.Name and then use it in the view (#ViewBag.Name)?
Yes. You will avoid runtime errors if "Name" does not exist.
The IDE will not warn you of the NullReferenceException about to be thrown with the following code.
#Request.Params["Fake"].ToString()
Of course, you'll have to be careful about ViewBag.Fake being null as well.
I like to use the viewbag to store things not related to the model, for example if I have a dropdown containing locations. I like to store only the id of the selected location on the model and the locations in the viewbag, since is not needed to create a contact. I think that's the purpose of the viewbag.
For me the model is a bag or properties used in business operations, for example if I have a customer creation view using a NewCustomerModel, I don't wanna pollute my model with things like a IList<CustomerType> AND a SelectedCustomerTypeId property. I just want the second since is the one imma use to create the customer.

How to call stored proc from ASP.Net MVC stack via the ORM & return them in json?

i'm a total newbie with asp.net mvc and here's my jam:
i have a 3 level list box which selection on box A shows options on box B and selection on box B will show the options for box C.
I'm trying to do the whole thing in asp.net MVC and what i see is that the nerd dinner tutorial uses the ORM method.
so i created a dbml to the database and drag the stored proc inside.
i create a datacontext object but i don't quite know how to connect the result from the stored proce which should be multiple rows of data and make it into a json.
so i can keep all the json data inside the html page and using jquery i could make the selection process faster.
i don't expect the data inside the three boxes to change so often thus i think this method should be quite viable.
Questions:
So how do i get the stored proc part
to return the data as json?
i've noticed some tutorial online
that the json return result part is
at the controller and not at the
model end.
Why is that?
Edit
FYI, i find what i mostly wanted to do here.
For the json part, i referenced here.
Return a JsonResult from your controller action. You may need to coerce the result from your stored procedure into a C# class serializable to Json.
Json conversion should be done in the controller because it's not really part of the domain. More a DTO in the MVVM (Model-View-ViewModel) style.

ASP.NET MVC View information stored in a data-store

I'm looking for some advice on storing views in a data-store (database, file, other) and display them based on routing data, all using ASP.NET MVC 2 and ASP.NET Routing.
For example, I'd like to be able to display different views based on the following route data:
/{country}/
/{country}/{area}
But in the same vein I'd like to display:
/{planet}/
/{planet}/{satellite}
All are based on strings, and the data isn't fixed. So based on the number of segments maybe, use that as the selection criteria into the data-store...additionally, I may not know the segments up front, so they'd all be dynamic.
I'm was hoping we could get a few different methods together here, as kind of a reference for all - I'm sure some methods won't suite everyone...
So, how would you do it?
Branislav Abadjimarinov suggested a Controller Factory which could be used to do the look-up and display the page dynamically. I like this idea, what do you think?
There is no way for MVC to understand from this url's which route to choose. You have to make the routes more specific. For example:
/planet/{planet}/{satelite}
/country/{country}/{area}
You also have the option to define your own controller factory. The controller factory decides which controller to instantiate based on the route. So you can put some custom logic in it like - check if the {planet} parameter exist and if yes instantiate Planet controller else instantiate Countries controller.
This Post could be really helpful for you.
Remember you always can add a new routing rule : )
Just like this

Resources