参数词典共包含参数无效项 [英] The parameters dictionary contains a null entry for parameter
问题描述
<%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");
}
}
在后一种形式我有这样的错误:参数词典共包含参数非可空类型'System.Int32'的'身份证'的方法无效项System.Web.Mvc.ActionResult OrderDevice(Int32)已在MvcKVteam.Controllers.ImportXMLController。一个可选参数必须是引用类型,可空类型,或声明为可选参数。
参数名:参数
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
如何解决它?
推荐答案
首先,我会建议你使用MVC的参数自动处理,而不是ofpulling出来请求自己。您的控制器动作有一个ID参数,这似乎去理会 - 使用和添加其他类似获得的输入参数
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.
其次,你的HTML看起来有点假。您使用的设备ID,产品ID和订单ID的值的作为输入元素的名称的。这将意味着,而不必的DeviceID = 123安培;的itemId = 456安培;订单ID = 789
你得到 123 = 123安培; 456 = 456安培; 789 = 789
这是不是很实用!尝试改变输入字段是这样的:
Secondly, your HTML looks a bit off. You're using device ID, item ID and order ID values as 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:
<input ... name="deviceId" value="<%= Html.Encode(item.DeviceId) %>" ... />
您可以再有一个参数caled DEVICEID
在你的控制器是这样的:
You can then have a parameter caleddeviceId
in your controller like this:
public ActionResult OrderDevice(int deviceId, int itmId, int orderId)
{
// ...
}
这样做,这样,你就不会定义使用的Request.Form [...]自己的。
Doing it this way, you won't ned to use Request.Form[...] yourself at all.
这篇关于参数词典共包含参数无效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!