2

I got this model in my project:

public class ListOfProducts
    {
        public List<string> ProductNames { get; set; }       
        public List<string> ProductUrls { get; set; }

    }

The way I pass it to my view is:

 public ActionResult Index()
        {
            var listOfUrls = GetListOfHrefs();    //Methods for giving values
            var listOfProductNames = GetListOfProductNames();//Methods for giving values
            var model = new ListOfProducts {ProductUrls = listOfUrls, ProductNames = listOfProductNames, ProductGtin = listofGti};


            return View(model);
        }

I would like to pass my model to the view as a list in order to loop through it the way i Want...Can I convert my model to a list before passing it?

Tried model.Tolist() but the intellisense could no find the Tolist-Part

EDIT:

I really need to acess both properties in the same context..In this case the Artice

 <article id="post-62" class="post-62 x-portfolio type-x-portfolio status-publish has-post-thumbnail hentry has-post-thumbnail x-portfolio-5e9a5c5e0b6c2e1342bfccc9cacb084f x-portfolio-3ce959340d6fed7ed4f041884b46387c">

                <div class="entry-wrap cf">
                    <header class="entry-header">
                        <h2 class="entry-title entry-title-portfolio">
                           <p>@product</p>
                        </h2>
                    </header>
                </div>
                <p>@url</p>
                <span class="visually-hidden"><span class="author vcard"><span class="fn">addiadmin</span></span><span class="entry-title">Gluten &#038; Mjölkfri Raggmunk</span><time class="entry-date updated" datetime="2014-05-21T08:23:32+00:00">05.21.2014</time></span>



            </article>
user2915962
  • 2,691
  • 8
  • 33
  • 60

4 Answers4

3

its the same to ask "I wanna send MyClass as list ...", this is a basic OOB(Object-oriented programming) concept question IMO..so send List{MyClass} , you are sending one object name ListOfProducts which contains 2 lists, do you mean List{ListOfProducts} ? you can attach any Model , any parameters to the View as you want to them , modify it(any Model /ViewBag.Something) to your needs as you please , you are the game ruler

 return View(new List<ListOfProducts >{model});
Zakos
  • 1,492
  • 2
  • 22
  • 41
1

You cannot currently convert ListOfProducts to a list as it's has nothing to do with a list. It's a new type which you have defined in your code.

if you want it to be a list, you should inherit from IEnumerable for example or ICollection and implement the required functions.

Actually, to implement IEnumerable, you need a property in ListOfProducts that will contain all your List<String> so you could iterate on that !

jony89
  • 5,155
  • 3
  • 30
  • 40
1

If I assume that each entry in the list ProductNames matches one entry in ProductUrls, then what your really after is a list of Products and the design is missing the 'Product' class.

Create a class like:

public class Product
{
  public string Name {get;set;}
  public string Url {get; set;}
}

Now replace your 'ListOfProducts' with:

List<Product>()

Now you have a list of products. Your class ListOfProducts is not required.

Rob Smyth
  • 1,768
  • 11
  • 19
1

ListOfProducts is an object with properties that are lists.

I think what you are wanting is

@model ListOfProducts

foreach (var item in Model.ProductNames)
{
  ....

foreach (var item in Model.ProductUrls )
{
  ....

EDIT: Following on from your comments, you could do this (but see comments at end)

@for (int i = 0; i < Model.ProductNames.Count; i++)
{
  <article id="post-62"...
    <div class="entry-...
      <header class="entry...
        <h2 class="entry-title...
          <p>Model.ProductNames[i]</p>
        </h2>
      </header>  
    </div>
    <p>Model.ProductUrls[i]</p>
    ....
  </article>
}

but this will only work if both ProductNames and ProductUrls contain exactly the same number of elements, in which case perhaps you should re-think your model

public class Product
{
  public string Name {get; set;}
  public string Url {get; set;}
}

Then create a List<Product> in you `Index' action method and pass that to the view

  • You were absolutley right! This is what i wanted. Sorry everybody for being unclear with what I wanted, hopefullt this can help someone else! – user2915962 May 25 '14 at 08:14
  • 1
    Are you creating html elements based on `ProductNames` and `ProductUrls`? If so it may be better to use a `for (int i = 0; ...)` loop so that you create the correct names for post back. See my answer to [this question](http://stackoverflow.com/questions/23841504/how-do-i-model-bind-a-list-of-listselectitem-using-mvc-net/23842106#23842106) –  May 25 '14 at 08:19
  • Do you mean that it is possible for me to "combine" the two loops in your answe above into 1 for-loop? That would help alot because i would really like to displat the two properties in the same "article".. – user2915962 May 25 '14 at 08:39
  • I was referring to creating inputs for post back (I was not sure if this was a form for post back or not) but if you know that both lists are exactly the same length then use can use a single loop `for (int i = 0; i < Model.ProductNames.Count; i++) {...` and do something with `Model.ProductNames[i]` and `Model.ProductUrls[i]` –  May 25 '14 at 08:44
  • If you have the time, please have a look at my edit, I try to acess both properties in the same Article, Is it maybe possible to acess both properties in the sam loop? – user2915962 May 25 '14 at 08:50