asp.net-mvc 参数字典包含参数的空条目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2863776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The parameters dictionary contains a null entry for parameter
提问by Ognjen
<%using (Html.BeginForm("OrderDevice", "ImportXML", FormMethod.Post))
{ %>
<table id="OrderDevices" class="data-table">
<tr>
<th>
DeviceId
</th>
<th>
Id
</th>
<th>
OrderId
</th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<input readonly="readonly" class="" id="DeviceId" type="text"
name="<%= Html.Encode(item.DeviceId) %>"
value="<%= Html.Encode(item.DeviceId) %>" style="width: 61px" />
</td>
<td>
<input readonly="readonly" class="" id="c" type="text"
name= "<%= Html.Encode(item.Id) %>" value="
<%= Html.Encode(item.Id) %>"
style="width: 50px" />
</td>
<td>
<input readonly="readonly" class="" id="OrderId" type="text"
name= " <%= Html.Encode(item.OrderId) %>"
value="<%= Html.Encode(item.OrderId) %> " style="width: 49px" />
</td>
</tr>
<% } %>
</table>
<input type="submit" value="Create"/>
<%} %>
My controller action:
我的控制器动作:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult OrderDevice(int id)
{
try
{
// TODO: Add insert logic here
orderdevice ord = new orderdevice();
ord.Id = System.Convert.ToInt32(Request.Form["Id"]);
ord.OrderId = System.Convert.ToInt32(Request.Form["OrderId"]);
ord.DeviceId = System.Convert.ToInt32(Request.Form["DeviceId"]);
XMLEntities.AddToorderdevice(ord);
XMLEntities.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View("Index");
}
}
When post a form I have this error: The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult OrderDevice(Int32)' in 'MvcKVteam.Controllers.ImportXMLController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters
发布表单时,我遇到此错误:对于“MvcKVteam”中的方法“System.Web.Mvc.ActionResult OrderDevice(Int32)”,参数字典包含不可为空类型“System.Int32”的参数“id”的空条目。 Controllers.ImportXMLController'。可选参数必须是引用类型、可为空类型或声明为可选参数。参数名称:参数
How fix it?
怎么修?
回答by Daniel Renshaw
First, I would suggest you use MVC's automatic handling of parameters instead ofpulling them out of the Request yourself. Your controller action has an id parameter which seems to go ignored - use that and add others like it to get the input parameters.
首先,我建议您使用 MVC 对参数的自动处理,而不是自己将它们从 Request 中拉出来。您的控制器操作有一个似乎被忽略的 id 参数 - 使用它并添加其他类似的参数来获取输入参数。
Secondly, your HTML looks a bit off. You're using device ID, item ID and order ID valuesas the input element names. This will mean that instead of having deviceId=123&itemId=456&orderId=789
you're getting 123=123&456=456&789=789
which isn't very useful! Try changing the input fields to something like:
其次,你的 HTML 看起来有点不对劲。您正在使用设备 ID、项目 ID 和订单 ID值作为输入元素名称。这将意味着,而不是让deviceId=123&itemId=456&orderId=789
你得到123=123&456=456&789=789
这不是很有用!尝试将输入字段更改为以下内容:
<input ... name="deviceId" value="<%= Html.Encode(item.DeviceId) %>" ... />
You can then have a parameter caleddeviceId
in your controller like this:
然后,您可以deviceId
在控制器中调用一个参数,如下所示:
public ActionResult OrderDevice(int deviceId, int itmId, int orderId)
{
// ...
}
Doing it this way, you won't ned to use Request.Form[...] yourself at all.
这样做,您根本不需要自己使用 Request.Form[...] 。
回答by Nalan Madheswaran
The error indicates, the parameter you are sending to the controller from view is not in the same name.
错误表明,您从视图发送到控制器的参数名称不同。
example:
例子:
@Html.ActionLink(item.PropertyId, "View", new { @id= item.PropertyId })
public ActionResult Edit(int propId)
{
var result = _service.GetProperty(propId);
return View("Index", result);
}
In this code the parameter 'id' passed from view. but the actual parameter name is 'propId'. change the name would fix this error.
在此代码中,参数“id”从视图中传递。但实际参数名称是“propId”。更改名称将修复此错误。
回答by user3767650
This error means that mvc canoot find a value for your ID property that must be passed as an argument.To resolve this include the following on your controller.
此错误意味着 mvc 无法找到必须作为参数传递的 ID 属性的值。要解决此问题,请在控制器上执行以下操作。
public ActionResult OrderDevice (string id) {
公共 ActionResult OrderDevice(字符串 ID){
ViewBag.S = id;
if (!string.IsNullOrEmpty(id))
{
int idint = Convert.ToInt16(id);
if (ModelState.IsValid)
{
try
{
// TODO: Add insert logic here
orderdevice ord = new orderdevice();
XMLEntities.AddToorderdevice(ord.Search(idint));
XMLEntities.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View("Index");
}
}
}
return View();
}
Seacrh is for an assumption that your programme contains a method that searches for a Device.Hope this helps.
Seacrh 假设您的程序包含搜索设备的方法。希望这会有所帮助。