K.Z
K.Z

Reputation: 5075

Pass model and modellist data together to a partial view - ASP.NET MVC 5

I am working on an ASP.NET MVC 5 app. I need to create form in partial view. In this form I am passing ViewModel which hold all related model class instances. Now one of the model classes, I need to pass data list so that I can print in foreach loop in razor code.

Now what I need is to pass the model of few classes and list data of one model to view...

Many thanks

View model:

public class QualificationViewModel : LEO.DAL.ViewModels.IQualificationViewModel
{
    public Qualification _Qualification { get; set; }
    public QualificationType _QualificationType { get; set; }
    public Subject _Subject { get; set; }
    public ComponentScheme _ComponentScheme { get; set; }
}

Controller:

  [HttpGet]
  public ActionResult CreateNewQualification()
  {
        var model = new QualificationViewModel();

        var ComponentList = //imagin this is list of components that i need to send along with viewModel             ??????????????????????

        return PartialView("PartialQualification_Create", model);
    }

View (need to fix this part (display list data here )

@model LEO.DAL.ViewModels.QualificationViewModel

 @*<div class="_FormGrid_block_1">
                <table class="table">
                    <tr>
                        <th>
                            @Html.DisplayNameFor(model => model._ComponentScheme.ComponentTitle)
                        </th>                         
                        <th>head1</th>
                    </tr>

                   @foreach (var item in Model._ComponentScheme)
                    {
                        <tr>
                            <td>
                                @Html.DisplayFor(modelItem => modelItem._ComponentScheme.ComponentTitle)
                            </td>

                            <td>
                                aaaaaaaaaaaaaa
                            </td>
                        </tr>
                    }

                </table>
            </div>*@

Upvotes: 0

Views: 1872

Answers (3)

K.Z
K.Z

Reputation: 5075

i have managed to loop list data in foreach loop with thanks to JTMon help

  @if (Model._ComponentScheme !=null)
                { 
                <table class="table">
                    <tr>
                        <th>
                            @Html.DisplayName("abvc")
                        </th>                         
                        <th>head1</th>
                    </tr>

                  @foreach (var item in Model._ComponentScheme)
                    {
                        <tr>
                            <td>
                                @Html.DisplayFor(modelitem => item.ComponentTitle)
                            </td>

                            <td>
                                aaaaaaaaaaaaaa
                            </td>
                        </tr>
                    }

                </table>
                }

Upvotes: 0

JTMon
JTMon

Reputation: 3199

If I understand you correctly you want to send the ViewModel to the partial view and in some cases send another list (ComponentList) to the same view? if that is what you want you have many ways:

Create a new view model that holds two properties: QualificationViewModel and a list of the type you want to send to the view and then bind your view to that new model

public class ExtendedQualificationViewModel
{

    public QualificationViewModel OldViewModel { get; set; }

    public IEnumerable<SomeType> ComponenetList {get;set;}
}

and in the view

@model LEO.DAL.ViewModels.ExtendedQualificationViewModel

Or you can do the same with extension of the original model like this:

public class ExtendedQualificationViewModel : QualificationViewModel
{
    public IEnumerable<SomeType> ComponenetList {get;set;}
}

and do the same binding in the view.

Lastly you can add the list into the ViewData and then retrieve it in the view.

Upvotes: 1

Ishtiaq
Ishtiaq

Reputation: 1058

Beside of knowing your question, the datamember ComponentScheme of 'QualificationViewModel` is not a list. So you can not iterate it using foreach loop.

Upvotes: 0

Related Questions