Techonthenet
Techonthenet

Reputation: 157

MVC partial view wtih different model

I have model as

public class MainDataViewModel
{
    [Required]
    [Display(Name = "Select Work Orders")]
    public string[] SelectedWorkOrdersValues { get; set; }
    public MultiSelectList WorkOrderIds { get; set; }

    public IEnumerable<ORDERMASTER> ordersDetails;
}

And Main View as

@model InventoryEasy15.Models.MainDataViewModel
<div class="box-body">
            <div class="col-md-6">
            <div class="form-group">
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })                  
        <label for="fileToUpload">Select the Work Orders</label>
            @Html.ValidationMessageFor(m => m.WorkOrderIds, "", new { @class = "text-danger" })
                    @Html.ListBoxFor(m => m.SelectedWorkOrdersValues, Model.WorkOrderIds as MultiSelectList, new { id = "WorkOrders", @class = "form-control", data_placeholder = "Choose Work Orders..." })
            </div>
            </div>
          </div>
          <!-- /.box-body -->

          <div class="box-footer">
            <input type="submit" value="Get WorkOrder Details" id="btnSubmit" class="btn btn-primary">
          </div>

      </div>
    </div>
</div>


@Html.Partial("MainDataWorkOrderDetails", Model.ordersDetails)

And the Partial view as

@model IEnumerable<InventoryEasy15.ORDERMASTER>
<div id="myDisplayID"><div>

Now I am getting error as

The model item passed into the dictionary is of type 'InventoryEasy15.Models.MainDataViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[InventoryEasy15.ORDERMASTER]'.

Any thoughts.

The controller here is

public async Task<ActionResult> MainDataWorkOrderDetails(MainDataViewModel m)
    {
        var model = new MainDataViewModel();
        var result = await db.ORDERMASTERs.Where(x => x.WOID == "WO7446708").ToListAsync();
        if (result != null)
        {
            model.ordersDetails = result;
        }
        return PartialView(model);
    }

Upvotes: 0

Views: 190

Answers (1)

Nirman
Nirman

Reputation: 6783

You are passing model to the PartialView. Now, the model is of type MainDataViewModel, and your partial view expects the model of type IEnumerable<InventoryEasy15.ORDERMASTER>

return PartialView(model);

I think you should consider passing model.orderDetails to the partial view from your action.

return PartialView(model.orderDetails);

Or else, simply return the View containing the partial view if you want to pass the whole model

Upvotes: 1

Related Questions