I am beginning with MVC ASP.NET and stumbled upon a problem. There is a paragraph (some 5 sentences) I would like to use on several of my child pages. This text changes quite often so I would like to have it defined on 1 place so I dont have to update like 10 pages when it changes again.
Problem is, that I dont know how to do it. I was thinking about using CSS (which should be used for styling, not text) or sections (doesnt work, as each child page needs its own sections defined).
Could someone advise me the most optimal and "correct" way to achieve this? I would like to define this paragraph at one place and then "call it" from the child page without need to use the database. Which approach should I choose?
Thank you
Create one partial view and call in your main view:
#Html.Partial("YourPartialViewName", Model) //if you have some dynamic data pass in model
Related
I have a model with attibutes name, title, description ect. If the name is by example weather-foracast I want to show an extra html element
= high_chart("my_id", #chart)
in the show template, wich generates a nice chart. When the page is not weather-foracast i don't want to show the chart.
What is the best approach? Conditions in the view?
Your problem looks really close to the Exhibit Pattern presented in Object on Rails.
Simply explained, the solution proposed is to wrap your model in a decorator (or multiple decorators) chosen using the state of your model (in your case the name of the record).
I cannot explain it as well as it is explained in the book but that Pattern is so useful that Avdi Grimm made a gem out of it : https://github.com/objects-on-rails/display-case
I know this answer is totally over the top, but something to consider is moving your conditional logic outside of your views.
Maybe going with the View Presenter pattern, see this:
About presenter pattern in rails. is a better way to do it?
Presenters are ruby objects and can be tested really easily.
Whereas having tons of logic inside the view is really hard to test.
Furthermore, you want isolation of logic, checking for model.name inside your view page is not the best way of MVC.
Since it looks like you're HAML, you can just use an if modifier like so:
= high_chart("my_id", #chart) if [model.name] == "weather-forecast"
where [model.name] is replaced with the appropriate code.
I am in the process of writing a web app that includes a reporting form. This form contains a number of radio buttons that allow the user to specify the return data.
There are about 6 different return data 'formats', and each of those has two variations - html data or JSON data for rendering to a chart.
I have begun coding it up and already my form post action method feels wrong.
I basically have a check on the requested data format and return it as needed. Each return type requires its own partial view / json object so there is little room for reusing code.
It feels like each one should have its own action method. Having the form post to different locations based on a radio button choice also feels wrong though.
Switching on report type and then redirecting to the appropriate action in the controller also feels like its not quite right.
Am I approaching this in the wrong way? As it currently stands my controller action contains a lot of code and a lot of logic...
Hope my query makes sense.
Thanks
I don't think there is anything wrong with your approach. To maximize reuse you could:
include reusable templates inside your views
make sure the business/data layer code is the same everywhere (where possible)
I suppose the views you need to return actually are different for each combination of options so whatever approach you take, you are stuck with that.
I wouldn't opt for the client-side approach. You then have code on both the server and the client that has to be updated whenever you change anything. I would keep the code that receives a set of options and determines what to do with them in one place.
I know what you mean about it feeling like each format should be a separate action, but maybe a hybrid approach would make it feel better.
If you set the value of each radiobutton to the name of the action it relates to, you then, in your main POST action, have a parameter that you can use to call the appropriate action in one line of code. You don't have to fudge anything in Javascript, it's easily extensible, and you get your separate actions.
If I understand your problem right you have a lot of switch code in action.
I think you can use Factory pattern. You can create factory that will accept switch parameter as parameter and will return ActionResult instance.
I have a create page and an edit page for an entity. The pages are similar so I have a base view model which contains common fields between the pages, and a view model for each page which inherit from the base.
One of the differences between the two pages is that the create page has a search form where the user can enter criteria and search using an ajax query. The search criteria fields are not part of the entity. I created a "SearchCriteria" sub model with its own properties for the different search criteria so that I could simply post this model when performing the search, and potentially add more search criteria in the future without having to modify method parameters.
It turns out I do need to add something else, but that something else is one of the properties of the base view model. I'm not sure what the best way do this is. I'm thinking that I will have to consider the property to be no longer common and move it into my Edit view model and my SearchCriteria model, but then I lose my common mapping to the entity and will have to repeat code.
I think I may have gone wrong somewhere so some design advice would be appreciated.
Thanks
I have faced a similar problem. First, with the search functionality. You can create a SearchServiceController. Then, add a partial view and pass it a model when you want to display the search bar, otherwise pass null and display nothing. This way you separate concerns by keeping the search functionality in its own process.
As far as adding a property that won't be used, I don't feel that this presents much of a problem. The .NET framework is filled with subclasses that do not implement parts of the base. Instead, you can throw a NotImplementedException. To me, its well worth the trade off to gain consistency and DRY.
Personally, I have found sharing viewmodels between controllers to not be a good thing (but in this case you may be using a single controller). Using IoC with Ninject, I get plenty of Cyclical Redundancy errors when binding my interfaces to the same viewmodels across controllers. For this reason, I took out Ninject. But, perhaps you can bind at another layer...have not tried it.
This might be similar to ASP.NET MVC - Populate Commonly Used Dropdownlists.
I want to populate DropDownLists. Some of it is static data. Some of it comes from the Database. A couple of times I found myself forgetting to call the code that populates my lists and sets the ViewBag accordingly. It is almost worth adding a unit test for this. The only way I think that this suits a unit test is if you place it in model/service. Is there a best practice for this kind of thing?
I'd suggest that the data is contained within the model but is perhaps constructed by a html.helper method. this way, you keep the plumbing markup out of the view and leave the controller free to invoke the neccesary view and model.
You could also of course hand it off to a partialview with an <IList<SelectList>> model.
cats and their skin :)
If you follow the spirit of the pattern then the Model should supply the View with everything it needs to present to the user that's not static. If you have static dropdown lists then you could say that these could be constructed within the mark-up. If you are passing a SelectList to the View from your Action then I'd stick it in the Model to make things simpler and more coherent.
My rule of thumb is that the data must somehow be in the model, either as a ready to use SelectList or at worst in some container that can easily be turned into a SelectList using a LINQ-to-object call.
The bottom line is that the view should never contain any non trivial code.
EDIT (answer to your comment):
I try not to put too much code in models. Models are more like a simple bunch of data gathered by the controller and used by the view.
Regarding simple and/or common things such as the days of week, I believe an HTML helper is the most elegant solution. See WayneC's answer in this question.
Ok, so this is an alternative to this question.
I'm trying to produce an MVC application using LinqToSql that allows for bulk editing of data on a single page.
Imagine a simple table Item with ItemId, ItemName, ItemPrice as fields.
There are many examples out there of extrmely simple MVC applications that show you a list of these items with an edit button next to each and an add button at the bottom.
From a UI perspective I find this very time consuming when a lot of data needs entering / updating.
I'm after a single page containing the items names and prices in textboxes that can all be edited in one go and then a single "Save" button pressed to update the data.
I've seen a number of examples that perform various stages of this but have yet to find one that implements the full solution. In particular the interaction with Linq.
I have a number of methods I've tried which all work, however, my gut feeling tells me my methods "smell" and therefore I'd like to see some examples of how other people have attempted this.
So, put simply, my question is can anyone provide some links to some examples please?
I have written about how to do this with MvcContrib's FluentHtml. Steve Sanderson has written about how to do it without FluentHtml. Both of our articles have a sample solution you can download and look at.
As far as LinqToSql, I would consider any interaction between bulk editing mechanism (controller and view) and LinqToSql to be a smell. That is to say, as far as possible your UI should be ignorant of your persistence mechanism.
What I would probably do to get around this is use jQuery to call a jsonResult when you switch rows. This jsonResult would call the code in the model to save the ItemId, ItemName, ItemPrice for only the row you are switching off of. More on general jsonResult w/ jQuery usage here : http://www.dev102.com/2008/08/19/jquery-and-the-aspnet-mvc-framework/
The other thing you could do is do model binding to a list of Items, iterating thru the list saving each item -- Phil Haack has an example of list binding here: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx With either method you'll want to do something to signify the row has changed so you're not updating every field if you're just changing a handful of rows.
What is your goal exactly, are you trying to commit a series of information all at once? Or do you simply not want your page to postback every time you change something. In either case jQuery is your best bet. If you want to do everything in one pass it is going to get complex unless you use a jQuery control that will do this for you. There are some great ones out there such as the Flexigrid.