0

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);
    }
5
  • The error means that model.ordersDetails is null. Suggest you add a default constructor to initialize it, or add a else block and include else { model.orderDetails = new List<ORDERMASTER> }. Side note: You can just use Model.WorkOrderIds in the ListBoxFor() method (no need for the as MultiSelectList) Commented Dec 21, 2015 at 2:55
  • I changed like this, if (result != null) { model.ordersDetails = result; } and it still fails else { model.ordersDetails = new List<ORDERMASTER>(); } Commented Dec 21, 2015 at 3:30
  • Its unclear what you doing here. Why does your GET method have a parameter for a model (bad practice) and why is it returning a partial view?. But I repeat - the error means that that when you call @Html.Partial("MainDataWorkOrderDetails", Model.ordersDetails) the value of Model.ordersDetails is null. Debug your code! Commented Dec 21, 2015 at 3:33
  • I am trying to show search results using Ajax, and the List box selection will be used for filtering the model. And before the debug hits the get method, it failed with the error message. And more question, I have force to select any of the list box selection, how to do that? Commented Dec 21, 2015 at 3:50
  • Just add a default constructor as per my first comment so that ordersDetails will not be null when you first render the view - public MainDataViewModel() { ordersDetails = new List<ORDERMASTER>(); }` Commented Dec 21, 2015 at 3:53

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.