ASP MVC ViewData from one view to another (Html.Encode()) - asp.net-mvc

I have a page with a bunch of labels and checkboxes on it. On this page, the labels need to be easily customizable after the project is deployed.
So I made all of the labels in this style:
Html.Encode(ViewData["lblText"])
And I added a button on the page called "Edit Button Labels" that will only be seen by admins.
When that button is clicked, I would like to load another view that simply contains a table of two columns. One column needs to contain the current labels and the other should have text boxes for the user to enter new labels.
Then, once any changes have been made, I need to permanently change the "lblText" for each label on the original page.
I have tried passing viewdata and tempdata to the "Edit Button Labels" view using both return view() and return RedirectToAction() with no success. Am I missing something minor or is there a better way to do this?

Don't use ViewData is the first thing I'd say. Return a model to your view which contains the labels. Create a composite model if required.
Then, to edit your labels, go to a view that takes the same model, allows you to enter the new text and saves the results to the xml, db, text file, whatever.
So you might have a model like this;
public class Labels
{
public List<string> text{ get; set; }
}
So in your db layer, wherever that is, fill the object with the label text items.
On your EDIT view you should then do the following imho;
<% Html.PartialView("EditLabels", Model); %>
Then you have a partial view called EditLabels and it would have something like the following psuedo;
foreach(string item in Model)
{
<% Html.PartialView("labelEdit", item); %>
}
Then finally you have a partial view that takes a single item and allows you to edit it called labelEdit.
This, in my opinion, is the correct way to do it in MVC as to breaks the entire view into chunks of functionality and seperates the concerns nicely.
Obviously you still need an ActionResult to take a post and affect the changes.

Related

Populate Menu Bar dynamically in MVC

I have my category name and sub category names stored in a table.
I want to have a layout page(master page) which has a menu bar. I want to pull the category names and subcategory names from the table and display it as a menu and sub menus in my layout page.
How can this be achieved?
The best way to do that, as far as Im aware, is to use the ViewData. You can do this on every action call (which is not only wrong but also a nightmare) or you can create an abstraction for your controllers and inherit it for every controller. Then in the abstraction itself you will make the database call in the constructor and make sure it is loaded for every action call. See this for more info

Create Child View based on selection in Drop Down List

I am using MVC 5 and I have the following situation:
I have a drop down list on a view that changes the child view that is displayed based on the selection in the list. This needs to change on the onchange or similar event. The selections in the list are populated from a database and has the following properties:
Link ID - Int (to be used as a value for the selection (must be saved on the main view)
Link Text - What is to be displayed in the drop down list.
LinK URL - The address of the child view action (made up of different areas, controllers, and actions)
In essence, when the list changes, it must call the URL (passing the model) and return the content to a targeted placeholder div on the page
Any Ideas on where to get started on this one?
We were able to get this working using ajax to call the child view. Thanks for the pointers.

Partial with login and page logic

I have a navigation bar that I want to be a partial. The contents of the nav bar will vary slightly, like if the user is logged in or if you're on a certain page you might get an extra link.
How do I best deal with providing the data to the partial? Should I pass this in ViewData for every controller?
A list of options would help me the most, because likely I will have to utilize a few different techniques.
Make a model for it. Create a class NavModel which in its constructor gathers all the data it represents (friendly user name, current page, etc.) and publishes them as public properties. Then just bind the model to the view as usual. Oh, and avoid instantiating nav model directly in a view, instead create a controller action (called Nav) which instantiates the NavModel and returns the nav partial view. Mark that action as ChildActionOnlyAttribute so that it can't be requested by the client. Then use #Html.RenderAction in the view that needs to render the nav partial (usually a layout view).
I had the same problem sometime ago, when i have to show some menu to admin and some menu to super admin and some to others... What i did in that case. I made a an action returning partial view and rendered it on master page. The view was strongly typed. ( A class whose property representing roles of user.) so using that strongly typed class i wrote if and else if logic in my razor view..and that solved my problem....Hope this would help you.

How do I Change an MVC Actions' Associated View After using 'Add View...' Dialog Previously

If I use 'Add View' on a new Controller Action in Microsoft MVC, it will create a new view and I don't have to explicitly reference this view when calling it from the Controller:
return View();
However, what if at some time later I want to point my action to a different View? Since my Action is already implicitly bound to View created through the 'Add View' dialog, the only way I've found to override this implicit relationship is to explicitly identify the new view when returning from the Action:
return View("NewView");
Is there any way re-relate the "NewView" View to the Controller Action in the same way that the first View was implicitly bound through the 'Add View' dialog?
In order for the implicit
return View();
to work out of the box with a different named view you would have to change the name of your controller action.
If you are concerned with "magic strings" and would like to have a little better feeling that the view actually exists I suggest taking a look at T4MVC which will automatically generate strongly typed constants for your views.

MVC Navigation Tabs

OK, I am now wondering how to handle navigation tabs with ASP.NET MVC. Giving an example, suppose you have the tabs like you have here at stackoverflow. So, Questions, Tags, Users, etc.
Now lets say you have a "sub tab" under this main one. So there were for example View and Add tabs displayed once you had selected the main Questions tab. Some questions:
Would it be best to have a set of routes like http://site/questions/view and http://site/questions/add for those two instances?
Would you therefore have a NavigationController that contained actions for each of the main tabs i.e. Questions, Tags, etc and then and id value for the sub tab i.e. View and Add. This would then give you something like the following:
public ActionResult Questions(string view)
public ActionResult Tags(string view)
Etc
Or would you have a controller per tab/navigation item and if so how would that be implemented?
Say you needed to show the tab(s) selected via something like highlighting. In the view (I guess you would have a partial view for this) for the navigation tabs, would this directly reference the URL to determine which should be highlighted or is this best acieved some other way?
Thanks in advance for any pointers
Your best bet would be to stick to REST. I would stick to having a controller per main tab and each sub-tab corresponding to the possible REST actions: index, new. Edit and delete are item-specific so wouldn't get tabs. Create is called by new and update by edit.
An exception to that in your example would be the 'Ask a Question' tab. Its at the same level as the Questions tab (index) but would call Questions/New.
I think your controllers should be more closely bound to your model than your user interface (see my answer to this question). In general, I think you should think of a controller handling input for your model, i.e., do something to my model, then return a view (the UI) that corresponds to that action. The elements of your UI could, but do not necessarily have to, reflect the model hierarchy. For example, Questions, Unanswered, and Ask a Question in SO all seem to relate to the question model, but they are all top-level interface elements. Unanswered also seems to have it's own controller, but could easily have been implmented as .../questions/unanswered rather than as .../unanswered.

Resources