为啥我为方法'ActionResult DisplaySearchResults()的不可空类型'System.Int32'的参数'CarLotID'获得空条目
Posted
技术标签:
【中文标题】为啥我为方法\'ActionResult DisplaySearchResults()的不可空类型\'System.Int32\'的参数\'CarLotID\'获得空条目【英文标题】:Why am I am getting null entry for parameter 'CarLotID' of non-nullable type 'System.Int32' for method 'ActionResult DisplaySearchResults()为什么我为方法'ActionResult DisplaySearchResults()的不可空类型'System.Int32'的参数'CarLotID'获得空条目 【发布时间】:2020-07-23 20:20:35 【问题描述】:我正在创建一个 MVC 应用程序,并尝试在单击按钮后根据多个下拉列表的选定值在我的表上显示数据。我想我正在分配代码中的所有值,但我是编码新手,所以我不确定我是否遗漏了什么。
这是我的看法
@model IgnitionHub2._0.Models.Car
@
ViewBag.Title = "Car Search Page";
<h2>Cars</h2>
<div class="center-div">
<div class="form-inline">
@html.DropDownListFor(model => model.CarLotID, new SelectList(Model.CarLotList, "CarLotID", "LotName"), "Select Car Lot", new @class = "form-control" )
@Html.DropDownListFor(model => model.Model.MakeID, new SelectList(Model.MakeList, "MakeID", "Name"), "Select Make", new @class = "form-control" )
@Html.DropDownListFor(model => model.ModelID, new SelectList(Model.ModelList, "ModelID", "Name"), "Select Model", new @class = "form-control" )
<button id="search">Search</button>
</div>
</div>
<div id="searchResults">
</div>
@section Scripts
@Scripts.Render("~/bundles/jqueryval")
<script src="~/Scripts/jquery-3.4.1.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<script type="text/javascript">
$(document).ready(
function ()
var makeUrl = '@Url.Action("GetCarDetails")';
var models = $('#ModelID')
$('#Model_MakeID').change(function ()
models.empty();
$.getJSON(makeUrl, MakeID: $(this).val() ,function(data)
if (!data)
return;
models.append($('<option></option>').val('').text('Please select'));
$.each(data, function(index, item)
models.append($('<option></option>').val(item.Value).text(item.Name));
);
);
)
)
$(document).ready(function ()
var url = '@Url.Action("DisplaySearchResults","Car")';
$('#search').click(function ()
var carLotID = $('#CarLotID').val();
var makeID = $('#Model_MakeID').val();
var modelID = $('#ModelID').val();
alert(modelID);
$('#searchResults').load(url, CarLotID: carLotID, MakeID: makeID, ModelID: modelID );
)
)
这是我的局部视图
@model IEnumerable<IgnitionHub2._0.Models.Car>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Year)
</th>
<th>
@Html.DisplayNameFor(model => model.Color)
</th>
<th>
@Html.DisplayNameFor(model => model.Mileage)
</th>
<th>
@Html.DisplayNameFor(model => model.BodyType)
</th>
<th>
@Html.DisplayNameFor(model => model.Drive)
</th>
<th>
@Html.DisplayNameFor(model => model.Available)
</th>
<th>
@Html.DisplayNameFor(model => model.Model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.CarLot.LotName)
</th>
<th></th>
</tr>
@foreach (var item in Model)
<tr>
<td>
@Html.DisplayFor(modelItem => item.Year)
</td>
<td>
@Html.DisplayFor(modelItem => item.Color)
</td>
<td>
@Html.DisplayFor(modelItem => item.Mileage)
</td>
<td>
@Html.DisplayFor(modelItem => item.BodyType)
</td>
<td>
@Html.DisplayFor(modelItem => item.Drive)
</td>
<td>
@Html.DisplayFor(modelItem => item.Available)
</td>
<td>
@Html.DisplayFor(modelItem => item.Model.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.CarLot.LotName)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new id=item.CarID ) |
@Html.ActionLink("Details", "Details", new id=item.CarID ) |
@Html.ActionLink("Delete", "Delete", new id=item.CarID )
</td>
</tr>
</table>
这是我的控制器
public ActionResult Index()
var cars = db.Cars.Include(c => c.Model).Include(c => c.CarLot);
var makeList = db.Makes.ToList();
var modelList = db.Models.ToList();
var carLotList = db.CarLots.ToList();
var ViewModel = new Car
CarList = cars,
MakeList = makeList,
ModelList= modelList,
CarLotList = carLotList
;
return View(ViewModel);
public ActionResult DisplaySearchResults(int CarLotID, int MakeID, int ModelID)
var model = db.Cars.Where(c => c.Model.MakeID == MakeID && c.ModelID == ModelID &&
c.CarLotID == CarLotID).ToList();// build list based on parameter searchText
return PartialView("_Index", model);
public ActionResult _Index()
var cars = new List<Car>();
return PartialView(cars);
public JsonResult GetCarDetails(int MakeID)
db.Configuration.ProxyCreationEnabled = false;
var data = GetModels(MakeID).ToList();
//data = data.Where(x => x.Model.MakeID == MakeID).ToList();
return Json(data, JsonRequestBehavior.AllowGet);
请帮忙!提前谢谢!
【问题讨论】:
你在哪里得到这个错误。您能指出代码中出现异常的那一行吗? @gilliduck 当我点击提交按钮时发生错误,不确定是哪一行导致了问题,因为程序没有崩溃,它只是不显示表格或任何结果。 【参考方案1】:根据文档,https://api.jquery.com/load/、.load()
使用 POST http 请求如果您将对象作为参数传递,您现在正在这样做;
// CarLotID:carLotID,MakeID:makeID,ModelID:modelID is an object
$('#searchResults').load(url, CarLotID: carLotID, MakeID: makeID, ModelID: modelID );
选项 1-- 由于您没有将操作方法编写为 POST,因此您只需将 .load()
配置为使用 GET。
使用下面的代码 .load()
;
$('#searchResults').load(url+"?CarLotID="+carLotID+"&MakeID="+makeID+"&ModelID="+modelID);
然后改变
@Html.DropDownListFor(model => model.ModelID, new SelectList(Model.ModelList, "ModelID", "Name"), "Select Model", new @id="ModelID", @class = "form-control" )
在您的循环中重新填充下拉列表,请改用它;
$.each(data, function(index, item)
models.append($('<option value="+item.MakeID+"></option>').text(item.Name));
);
更新脚本:
$('#Model_MakeID').change(function()
models.empty();
$.getJSON(makeUrl, MakeID: $(this).val() , function(data)
if (!data)
return;
models.append($('<option></option>').val('').text('Please select'));
// update this
$.each(data, function(index, item)
models.append($('<option value="+item.Value+"></option>').text(item.Name));
);
);
);
或选项 2 - 只需将 [HttpPOST]
属性添加到您的控制器操作。
[HttpPost] // add this
public ActionResult DisplaySearchResults(int CarLotID, int MakeID, int ModelID)
...
【讨论】:
@SmileyRod 你会调试你的 JavaScript 还是只是添加alert(modelID)
看看你是否真的得到了它的价值?
然后尝试将参数更改为字符串public ActionResult DisplaySearchResults(int CarLotID, int MakeID, string ModelID)
我可以添加代码,但现在当我选择下拉菜单时,我得到以下信息:<option value="+item.Value+">Civic</option>
顺便说一句,非常感谢您帮助我。
糟糕,抱歉,请将其替换为单引号。这是更新的代码行; models.append($('<option value='+item.Value+'></option>').text(item.Name));
@SmileyRod 好的,将其还原。我很困惑你提到你只有 MakeID 和 Name,你应该使用它而不是 Value。 models.append($('<option value="+item.MakeID+"></option>').text(item.Name));
以上是关于为啥我为方法'ActionResult DisplaySearchResults()的不可空类型'System.Int32'的参数'CarLotID'获得空条目的主要内容,如果未能解决你的问题,请参考以下文章
如何在服务中调用异步任务<ActionResult> 方法?