views:

117

answers:

1

Hello all,

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?

A: 

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.

griegs
This is what I ended up doing. Thanks!
Jacob Huggart