Iman Jalali
Iman Jalali

Reputation: 149

Two tables in a view with ViewModel

I built a class in the model folder with this name XMLbetViewModel and then I putted one of two tables which I had as a property like below:

using System;
using System.Collections.Generic;    
using System.Linq;    
using System.Web;    
using DataLayer;

namespace League2.Models    
{    
    public class XMLbetViewModel    
    {    
        public IEnumerable<XML> XMLs {get;set;}    
        public  IEnumerable<Bet> Bets { get; set; }    
    }    
}

Then in “view” I called back that model like below:

@model League2.Models.XMLbetViewModel

The table names are XML BET The problem is when I write @model, it is just show me the name of tables not the content. So what should I do for accessing to the contents of this model at the same time in one “view”, cause finally I want to save this information in my data base What I want to do is: there is a form in one view and some of these contents(fields) are read from xml table and fill the form and then the user fill other empty (contents)fields and then finally by pressing a key, information are saved in Bet table. I have a bank like these: codeFirst , migration

Upvotes: 1

Views: 84

Answers (2)

Rajdeep
Rajdeep

Reputation: 802

You can use some looping or lambda expression for achieving iterations.

Lets say you have Holidays in your viewModel. something like

  public IList<Holiday> Holidays
    {
        get
        {
            return this.holidays;
        }
    }

You can use below like syntax to iterate over it and show it in your view.

  @if (Model.Holidays != null)
            {
                var grouppedHoliday = Model.Holidays.GroupBy(summary => summary.Date.Month);
                if (grouppedHoliday != null)
                {
                    foreach (var holidaysummary in grouppedHoliday)
                    {
                        if (holidaysummary != null)
                        {
                            <div class="holiday-summary-subTitle">@holidaysummary.FirstOrDefault().Date.ToString("MMMM", CultureInfo.InvariantCulture)</div>

                            foreach (var holiday in holidaysummary)
                            {
                                <div title="@holiday.Remarks" class="multiple-options">

                                    @if (holiday.IsGone == true)
                                    {
                                        <span class="is-gone-holiday-summary-day">@holiday.Date.ToString("dd", CultureInfo.InvariantCulture)</span>
                                        <span>-</span> <span class="is-gone-holiday"> @holiday.Subject</span>

                                    }
                                    else
                                    {
                                        <span class="holiday-summary-day">@holiday.Date.ToString("dd", CultureInfo.InvariantCulture)</span>
                                        <span>-</span><span class="holiday-summary-subject"> @holiday.Subject</span>

                                    }
                                </div>                                 
                            }
                        }                        
                    }
                }
            }

Upvotes: 1

Rajan Mishra
Rajan Mishra

Reputation: 1178

As much i can understand your problem . @model is used to strictly bind the model to a view for table content. You need objects like htmlhelpers and you can also create the object of that model using lmbda expression.

Upvotes: 2

Related Questions