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");
}
}
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
How fix it?
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.
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) %>" ... />
You can then have a parameter caled deviceId
in your controller like this:
public ActionResult OrderDevice(int deviceId, int itmId, int orderId)
{
// ...
}
Doing it this way, you won't ned to use Request.Form[...] yourself at all.
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.
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.
public ActionResult OrderDevice (string 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.
链接地址: http://www.djcxy.com/p/39220.html上一篇: 使用jQuery将空值发布到MVC操作
下一篇: 参数字典包含参数的空项