1

I have a table in the database that is a list of Products. Then I have a Fundraiser class (made into a controller and views) that will have a "collection" of Products. The point is that "for this fundraiser, I am selling these selected products".

My idea is to have a list of checkboxes so that the user can select which products are to be included in the fundraiser. I know that the typical way of doing this is to have an intermediary table that has a compound key of ProductID and FundraiserID. I don't want to do this.

Instead my idea is to make the list of products as checkboxes, use JavaScript to create a JSON string as the user selects/deselects the checkboxes, then save that JSON string into a simple text field in the Fundraiser table.

public class Fundraiser
{
    public int FundraiserID { get; set; }
    public string EventName { get; set; }
    public string ItemsList { get; set; }
}

public class Product
{
    public int ProductID { get; set; }
    public string Name { get; set; }
}

So, there are two ways that I'm thinking this can go.

  1. How do I write raw HTML so that I can manually iterate through the resultset of the Products table and create the checkboxes. Right now, the HTML I write is shown literally on the page instead of interpreted.

    ViewData["ProductList"] = "<input type='checkbox' id='prod12' checked>";
    // Is escaped or something so that it does not render as an HTML input
    

OR

  1. Is there an HTML helper or some other tool that will do this through class methods or something (so that I can do it in the 'proper' OOP style)?

    ViewData["ProductList"] = new ListOfCheckBoxes(_context.Products, "ProductID"......
    

I have a background in PHP programming, and I rarely used third-party code, so having all these high-level "helpers" is really confusing for me. I just want to write the code!

  • You shouldn't use Entity Framework model classes directly in your MVC models. It can end up getting you in to trouble. – Bradley Uffner Oct 16 '17 at 18:23
  • You didn't supply any of your current attempt to write the view. I think you should review some related examples, like this one https://stackoverflow.com/questions/11316971/foreach-inside-of-foreach-razor Also, are you using the Razor or . Net View Engine? – Greg Oct 16 '17 at 18:31
  • Similiar to Bradley Uffner's comment, I would make the model relate to how you're going to use it on the page, and not how you are going to store it. I'd actually work with a List of Products as part of the view model. Either way, you'll need a list of available products, and knowledge if the product is active for the fundraiser. I'd leave all the html to your view and html helpers. – Greg Oct 16 '17 at 18:37
  • Like I said, if I have a DbSet, is there an HTML helper that can produce a list of checkboxes? OR...how do I SIMPLY use my Controller to write HTML code that will get interpreted by the browser, not just displayed as though it was in a PRE tag? What's the statement? – Jack Thomson Oct 16 '17 at 19:10
  • That is a awful approach - from a db point of view, suggest you read this [Q/A](https://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad) and from a mvc point of view, you will not get string-typed 2-way model binding or validation. –  Oct 17 '17 at 09:51
  • As for how to generate the view correctly, refer [this answer](https://stackoverflow.com/questions/29542107/pass-list-of-checkboxes-into-view-and-pull-out-ienumerable/29554416#29554416) for an example –  Oct 17 '17 at 09:52

0 Answers0