ASP.NET MVC

S

shapper

Hello,

I am using ASP.NET MVC and I am running into a problem.

I have a List Of MyClass which is a property of my controller
ViewData.

In the View I have a form where I need to display the List in the CSV
format.
When I submit the form I need to convert that CSV data to a list
before I save it to the database using Linq.

The View also displays the same data in an ordered list. And there are
other view which do the same.

How should I implement this and where to make the conversions? In the
View? In the Controller?

Thanks,
Miguel
 
B

bruce barker

you don't speciify how the user makes any changes to the list. is the comma
seperated list uploaded as a file, or is it in a textarea?

as only the controller sees the postback data, then it must parse the csv
data, though I'd code a seperate library that converted both ways that was
callable by however needed it.

-- bruce (sqlwork.com)
 
C

Cowboy \(Gregory A. Beamer\)

The conversion should be contained in a class library used behind the
scenes, IMO. As much as possible, you should massage the data formats prior
to binding.

You could, theorhetically, use the controller, as it is code. The problem
here is you are muddying the waters and make the controller into a
controller-converter. As far as what should call the converter, this is on
the controller level, not the view. A view is merely a tool to display data
to the end user, when used correctly.

One other option is to supe up your Model classes, especially with Preview 3
(and later? - nothing later released yet).

My favorite patterns, at least for now, is to alter the pages so they use
ViewPage<T> instead of ViewPage. This works best if T is a bit of a proxy
class that binds the model and adds some helpers. Do not overblow this class
with funcationality. It should be a data object, not a behavioral object.

Using this pattern, you then pass an object (T) to the page with your model.
I have not thought this through completely (I am thinking and writing), but
I could see creating different routines that serve up the model in
differernt formats, CSV, list, standard data using IEnumerable, etc. I will
have to think this pattern through a bit before giving a final seal of
approval, as this may be a bit too much behavior. Right now, I am leaning
this direction, at least for a simple implentation.

Longer term, you could set up an interface T to adhere to for that page, but
that is probably overkill. It would be fun, as a geek, however. :)
 
S

shapper

The conversion should be contained in a class library used behind the
scenes, IMO. As much as possible, you should massage the data formats prior
to binding.

You could, theorhetically, use the controller, as it is code. The problem
here is you are muddying the waters and make the controller into a
controller-converter. As far as what should call the converter, this is on
the controller level, not the view. A view is merely a tool to display data
to the end user, when used correctly.

One other option is to supe up your Model classes, especially with Preview 3
(and later? - nothing later released yet).

My favorite patterns, at least for now, is to alter the pages so they use
ViewPage<T> instead of ViewPage. This works best if T is a bit of a proxy
class that binds the model and adds some helpers. Do not overblow this class
with funcationality. It should be a data object, not a behavioral object.

Using this pattern, you then pass an object (T) to the page with your model.
I have not thought this through completely (I am thinking and writing), but
I could see creating different routines that serve up the model in
differernt formats, CSV, list, standard data using IEnumerable, etc. I will
have to think this pattern through a bit before giving a final seal of
approval, as this may be a bit too much behavior. Right now, I am leaning
this direction, at least for a simple implentation.

Longer term, you could set up an interface T to adhere to for that page, but
that is probably overkill. It would be fun, as a geek, however. :)


| Think outside the box!                               |

Hi,

Yes, the user makes the changes in a TextBox.

Basically the CSV values are tags associated to a file. For the file I
created a class which represents the ViewData:

public class FilePaper
{
public File File { get; set; }
public List<Tag> Tags { get; set; }
}
public class FileViewData
{
public PagedList<FilePaper> FilesPapers { get; set; }
public FilePaper FilePaper { get; set; }
}

Then on my File controller I have:
private FileViewData viewData = new FileViewData();

On my Update action I have something like this:

FilePaper paper = new FilePaper();
BindingHelperExtensions.UpdateFrom(paper.File, Request.Form);

I know that in my View TextBox I can insert some code to convert the
list to CSV but when I submit the form where should I convert the CSV
to the list and insert it in the viewdata Tags property?

I know I can use LINQ:
List<Tag> form = CSVList.Split(',').Select(p => new Tag { Name =
p.Trim() }).ToList();

But I am completely confused about this.

I know that I could use a property of type String in my ViewData to
hold the CSV values but if I have a List I think that is going
backward.
And in fact I might need to display that list in various formats so I
don't think this would be a good option.

Could someone, please, help me out?

Thanks,
Miguel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top