0
votes

I am trying to implement search functionality in MVC view. I have view called 'ReturnResults' and partial view (_Find) in it. _Find is object that I use to find/filter results.

Is this the correct way to implement search? I am getting error The model item passed into the dictionary is of type 'MyModels.ReturnResults', but this dictionary requires a model item of type 'MyModels.Find'.

Controller Method:

public ActionResult Locate(Find find)
{
    List<ReturnResults> returnV = new List<ReturnResults>();
    returnV = CallDB.GetList(find)
    return View(returnV);
}

View (ReturnResults):

@model MyModels.ReturnResults

@{
    ViewBag.Title = "List Results";
}

@Html.Partial("_Find", Model)

Partial View (_Find):

@model MyModels.Find

@{
    ViewBag.Title = "Find";
}

@using (Html.BeginForm("Locate", "LocateCource", FormMethod.Post, new { name = "frmLocate", id = "frmLocate"}))
{
    <td>
        @Html.LabelFor(m => m.Number)
        @Html.TextBoxFor(m => m.Number)
    </td>
    <td>
        @Html.LabelFor(m => m.Course)
        @Html.TextBoxFor(m => m.Course)
    </td>   
    <td>
        @Html.LabelFor(m => m.Location)
        @Html.TextBoxFor(m => m.Location)
    </td>
    </td>   
    <td>
        @Html.LabelFor(m => m.Condition1)
        @Html.TextBoxFor(m => m.Condition1)
    </td>
    <td>
        @Html.LabelFor(m => m.Condition2)
        @Html.TextBoxFor(m => m.Condition2)
    </td>   
        <td>
        @Html.LabelFor(m => m.Condition3)
        @Html.TextBoxFor(m => m.Condition3)
    </td>   
}

Partial View:

public class Find
{
    [Display(Name = "Number ")]
    public int Number { get; set; }

    [Display(Name = "Course Name")]
    public string Course { get; set; }

    [Display(Name = "Course Name")]
    public string Location { get; set; }

    [Display(Name = "Condition1")]
    public string Condition1 { get; set; }  

    [Display(Name = "Condition2")]
    public string Condition3 { get; set; }  

    [Display(Name = "Condition3")]
    public string Condition3 { get; set; }      
}

View: (with Partial View)

public class ReturnResults
{
    [Display(Name = "Number ")]
    public int Number { get; set; }

    [Display(Name = "Course Name")]
    public string Course { get; set; }

    [Display(Name = "Return1")]
    public string Return1 { get; set; }

    [Display(Name = "Return2")]
    public string Return2 { get; set; }

    [Display(Name = "Return3")]
    public string Return3 { get; set; }

    [Display(Name = "Return4")]
    public string Return4 { get; set; }

    [Display(Name = "Return5")]
    public string Return5 { get; set; }

    [Display(Name = "Return6")]
    public string Return6 { get; set; } 
}
2

2 Answers

0
votes

In your ReturnResults view you are passing in Model to the partial view. This Model object is of type ReturnResults, but the partial view is expecting a model of type Find. This is what your error message is telling you. One way you can solve this is to add a property to your ReturnResults view model Class of type Find, then when you call your partial view pass it in.

@Html.Partial("_Find", Model.Find);

Also, in your controller, you are creating a List of ReturnResults; , but your ReturnResult.cshtml view is expecting a Scalar ReturnResult object.

0
votes

Since you are building a search feature, and will not require any editing of existing values, you could try @Html.Partial("_Find", new MyModels.Find())

Cheers