Foreach循环将数据库搜索结果返回到cshtml中的视图

时间:2018-03-08 21:51:34

标签: c# asp.net-mvc

  

无法隐式转换类型   CourseSearchResultModeIEnumerable。存在显式转换(是   你错过了一个演员?)

控制器:

namespace RedPandaCourses.Controllers
{
    public class SearchController : Controller
    {
        [HttpGet]
        public ActionResult CourseSearchView()
        {
            var courses = new CourseSearchResultModel();
            return View(courses);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult CourseSearchView(CourseSearchResultModel vm)
        {
            var courses = new CourseSearchResultModel();
            return View(courses);
        }

2型号:

namespace RedPandaCourses.Models
{
    public class CourseSearchModel
    {
        public CourseSearchModel() { }
        public string courseName { get; set; }
        public int courseNumber { get; set; }
        public string courseInstructor { get; set; }
        public List<CourseSearchResultModel> results { get; set; }
    }
}

namespace RedPandaCourses.Models
{
    public class CourseSearchResultModel
    {
        public CourseSearchResultModel() { }
        public decimal courseID { get; set; }
        public string courseName { get; set; }
        public string courseNumber { get; set; }
        public string courseInstructorFirstName { get; set; }
        public string courseInstructorLastName { get; set; }
        public string courseInstructor { get; set; }
        public string courseSchedule { get; set; }

    }
}

它会将文本框文本发送到数据库,从select类中搜索它,并使用foreach循环将结果返回到表中,但是foreach循环会抛出上面的错误。 视图:

@Model RedPandaCourses.Models.CourseSearchResultModel;
@using RedPandaCourses.Models;

@{
    ViewBag.Title = "CourseSearchView";
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
}

<div class="container-fluid no-main-border">
    <div class="row-fluid main-bg">
        <div class="span12">
            <h2 class="labelHide">&nbsp;</h2>

            <div class="row-fluid">
                <div class="negMargin">
                    <div class="span9 inner-container">
                        <div class="streamlined-subhead">
                            <h1>Search Courses</h1>
                        </div>
                        @using (Html.BeginForm("CourseSearchView", "Search"))
                        {
                            var model = new CourseSearchModel();
                            <div id="the_id" class="question">
                                <div class="control-group" id="the_id_control-group">
                                    <fieldset>
                                        <div id="the_id_1_control-group" class="control-group">
                                            <label class="control-label" for="courseName">Course Name: </label>
                                            <div id="the_id_1_controls" class="controls">
                                                @Html.TextBoxFor(m => model.courseName)
                                            </div>
                                        </div>
                                        <div id="the_id_2_control-group" class="control-group">
                                            <label class="control-label" for="courseNumber">Course Number: </label>
                                            <div id="the_id_2_controls" class="controls">
                                                @Html.TextBoxFor(m => model.courseNumber)
                                            </div>
                                        </div>
                                        <div id="the_id_3_control-group" class="control-group">
                                            <label class="control-label" for="instructor">Instructor: </label>
                                            <div id="the_id_3_controls" class="controls">
                                                @Html.TextBoxFor(m => model.courseInstructor)
                                            </div>
                                        </div>
                                        <div class="previous-next-bar">
                                            <button type="submit" class="btn btn-primary" id="courseSearch" onclick="location.href='@Url.Action("CourseSearchView","Search")'">Search</button>
                                        </div>
                                    </fieldset>
                                </div>
                            </div>
                        }
                        <div>
                            <h2>Search Results</h2>
                            <table id="example" class="table table-bordered table-striped table-hover dataTable width80">
                                <thead class="tableHeader" role="rowgroup">
                                    <tr role="row">
                                        <th class="sorting" role="columnheader"><a href="#">Course Title</a></th>
                                        <th class="sorting" role="columnheader"><a href="#">Course Number</a></th>
                                        <th class="sorting" role="columnheader"><a href="#">Instructor</a></th>
                                        <th class="sorting" role="columnheader"><a href="#">Schedule</a></th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @if (Model == null)
                                    {
                                        <tr>
                                            <td colspan="4">no results</td>
                                        </tr>                                       
                                    } else { 
                                    foreach (var item in Model)
                                    {
                                        Html.AntiForgeryToken();
                                        <tr class="clickableRow" onclick="location.href='@Url.Action("InstructorCourseDetailView", "Instructor")'">
                                            <td>@Html.Encode(item.courseName)</td>
                                            <td>@Html.Encode(item.courseNumber)</td>
                                            <td>@Html.Encode(item.courseInstructor)</td>
                                            <td>@Html.Encode(item.courseSchedule)</td>
                                        </tr>
                                    }
                                    }
                                    <tr>
                                        <td colspan="4">
                                            <button class="btn btn-default" onclick="location.href='@Url.Action("AdminAddCourseView","Admin")'">Add Course</button>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row-fluid">
                <div class="span12">
                    <div class="previous-next-bar">
                        <button type="submit" class="btn btn-default" onclick="location.href='@Url.Action("AdminHome","Admin")'">Return</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

我到处寻找,包括本网站上的一堆问题,在这种情况下找不到任何帮助我的东西。我是编码的新手,这是我为新工作所做的培训应用,我们将不胜感激!

1 个答案:

答案 0 :(得分:1)

To start with, the form should sending a CourseSearchModel not a CourseSearchResultModel.

Second, in the action handling the post, you should query the database and put the result in the results property of the search model you received as an argument. Here is how to do it :

[HttpPost]
[ValidateAntiForgeryToken]
//Change the type of the object bound from the from 
public ActionResult CourseSearchView(CourseSearchModel vm)
{
    //Query the db and put the outcome in the results prop of the vm
    vm.results = QueryTheDb();
    //return the vm to the view
    return View(vm);
}

Third, you give that object to the View method in the return line.

Fourth, change this

@Model RedPandaCourses.Models.CourseSearchResultModel; 

with this

@Model RedPandaCourses.Models.CourseSearchModel;

in your view.