MVC中的用户代码未处理HTTP异常

时间:2018-11-07 17:25:59

标签: javascript jquery ajax asp.net-mvc entity-framework

正在收到此错误:

System.Web.dll中发生了'System.Web.HttpException'类型的异常,但未在用户代码中处理 附加信息:数据绑定:“ Final.Models.Hello”不包含名称为“ 700”的属性。

我通过google寻找了一些答案,但我仍然感到迷茫。

模型类:

public Hello () {

           db = new ExtensionDBEntities();
        }

public List<Hello> getID()
        {
            var que = (from rel in db.Table1
                       select new Hello
                       {
                           ID = rel.ID
                       }).ToList();
            return que;
        }

public List<Hello> getStuff()
        {
            var que = (from wre in db.View
                       select new Hello
                       {
                           ID = wre.ID,
                           Summary = wre.Summary,
                           Description = wre.Description

                       }
         }

getHello()与getStuff()完全相同,只是接受一个字符串ID参数。

控制器类:

public ActionResult Index()
        {
           var model = test.getStuff();
           ViewBag.Releases = new SelectList(test.getID(), "", "ID");
           ViewBag.Managers = new SelectList(test.getManagers(), "", "Managers");
            return View("");
        }

        [HttpPost]
        public ActionResult Selection()
        {
            string selectedId = Request["IDText"].ToString();
            string Managers = Request["ManagersText"].ToString();
            var model = test.getStuff();
            ViewBag.Releases = new SelectList(test.getID(), selectedId, "ID");
            ViewBag.Managers = new SelectList(test.getManagers(), Managers, "Managers");
            var que = test.getHello(selectedId, Managers);
            return View(que);
        }

索引视图类:

 $(document).ready(function () {
   $("#Releases").on("change", function () {
                var value = $('#Releases :selected').text()
                $("#IDText").val(value);
            });
            $("#Managers").on("change", function () {

                var value = $('#Managers :selected').text()
                $("#ManagersText").val(value);
            });

});
@using (Html.BeginForm("Selection", "Sample", FormMethod.Post))
 {
    <div class="container" id='div_release'>

        @Html.DropDownList("Releases", ViewBag.Releases as SelectList)
        @Html.DropDownList("Managers", ViewBag.Managers as SelectList)
        @Html.Hidden("IDText", "")
        @Html.Hidden("ManagersText", "")
        <input type="submit" name="Submit" id="btnUploadData" class="btn btn-primary" value="Upload" />
    </div>
 }

选择视图类:

 <div class="container">
        <table id="myTable" align="left">
            <tr>
                <th>@Html.DisplayNameFor(model => model.ID)</th>
                <th>@Html.DisplayNameFor(model => model.Summary)</th>
                <th>@Html.DisplayNameFor(model => model.Description)</th>
            </tr>

     @foreach (var item in Model)
        {
          <tr id="Home">

              <td>@Html.DisplayFor(x => item.ID)</td>
              <td>@Html.DisplayFor(x => item.Summary)</td>
              <td>@Html.DisplayFor(x => item.Description)</td>
           </tr>
         }

 $(document).ready(function () {
       $("#Releases").on("change", function () {
                    var value = $('#Releases :selected').text()
                    $("#IDText").val(value);
                });
                $("#Managers").on("change", function () {

                    var value = $('#Managers :selected').text()
                    $("#ManagersText").val(value);
                });

    });
    @using (Html.BeginForm("Selection", "Sample", FormMethod.Post))
     {
        <div class="container" id='div_release'>

            @Html.DropDownList("Releases", ViewBag.Releases as SelectList) // Getting the error here....
            @Html.DropDownList("Managers", ViewBag.Managers as SelectList)
            @Html.Hidden("IDText", "")
            @Html.Hidden("ManagersText", "")
            <input type="submit" name="Submit" id="btnUploadData" class="btn btn-primary" value="Submit" />
        </div>
     }
  </table>
 </div>

在选择视图类中发生错误。视图类几乎完全相同,唯一的区别在于数据是在选择视图类中基于从下拉列表中选择值。

1 个答案:

答案 0 :(得分:1)

这里是到小提琴的链接,该小提琴将视图合并为一个:https://dotnetfiddle.net/5uCKhI

这应该对您有帮助。请让我知道是否还有其他内容。

控制器/模型

public class caitlinpRealViewModel
{
    public List<SelectListItem> IDList { get; set; }
    public string selectedId { get; set; }
    public List<SelectListItem> ManagerList { get; set; }
    public string selectedManager { get; set; }
}

public class HomeController : Controller
{
    public caitlinpRealViewModel SetupViewModel(caitlinpRealViewModel vm)
    {
        caitlinpRealViewModel viewModel = new caitlinpRealViewModel();

        if (vm != null)
        {
            viewModel.selectedId = vm.selectedId;
        }
        else
        {
            viewModel.selectedId = "1";
        }

        SelectListItem listItem = new SelectListItem() { Text = "1", Value = "1" };
        SelectListItem listItem2 = new SelectListItem() { Text = "2", Value = "2" };
        List<SelectListItem> list = new List<SelectListItem>();
        list.Add(listItem);
        list.Add(listItem2);

        if (vm != null)
        {
            viewModel.selectedManager = vm.selectedManager;
        }
        else
        {
            viewModel.selectedManager = "1";
        }
        SelectListItem listItem3 = new SelectListItem() { Text = "aManager", Value = "1" };
        SelectListItem listItem4 = new SelectListItem() { Text = "bManager", Value = "2" };
        List<SelectListItem> list2 = new List<SelectListItem>();
        list2.Add(listItem3);
        list2.Add(listItem4);

        viewModel.IDList = list;
        viewModel.ManagerList = list2;

        return viewModel;
    }

    [HttpPost]
    public ActionResult Selection(caitlinpRealViewModel vm)
    {
        caitlinpRealViewModel viewModel = SetupViewModel(vm);
        return View(viewModel);
    }

    public ActionResult Tut135()
    {
        caitlinpRealViewModel viewModel = SetupViewModel(null);
        return View(viewModel);
    }

查看1

@model Testy20161006.Controllers.caitlinpRealViewModel
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Tut135</title>
</head>
<body>
    @using (Html.BeginForm("Selection", "Home", FormMethod.Post))
    {
        <div class="container" id='div_release'>
            @Html.DropDownListFor(m => m.selectedId, new SelectList(Model.IDList, "Value", "Text"))
            @Html.DropDownListFor(m => m.selectedManager, new SelectList(Model.ManagerList, "Value", "Text"))
            <input type="submit" name="Submit" id="btnUploadData" class="btn btn-primary" value="Upload" />
        </div>
    }
</body>
</html>

查看2(选择)

@model Testy20161006.Controllers.caitlinpRealViewModel
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Selection</title>
</head>
<body>
    <div class="container">
        <table id="myTable" align="left">
            @using (Html.BeginForm("Selection", "Home", FormMethod.Post))
            {
                <div class="container" id='div_release'>
                    @Html.DropDownListFor(m => m.selectedId, new SelectList(Model.IDList, "Value", "Text"))
                    @Html.DropDownListFor(m => m.selectedManager, new SelectList(Model.ManagerList, "Value", "Text"))
                    <input type="submit" name="Submit" id="btnUploadData" class="btn btn-primary" value="Submit" />
                </div>

            }
        </table>
    </div>
</body>
</html>