The parameter dictionary contains a null entry for the 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");
}
}
While submitting the form, I have this error: the parameter dictionary contains a null entry for the nonzero type "id" parameter, "System.Int32" for the "System.Web.Mvc.ActionResult OrderDevice (Int32)" method in 'MvcKVteam.Controllers .ImportXMLController. The optional parameter must be a reference type, type null, or declared as an optional parameter. Parameter name: parameters
How to fix it?
a source to share
First, I suggest you use the automatic handling of MVC parameters instead of distracting them from the request yourself. Your controller has an id parameter that seems to be ignored - use that and add others like it to get the input parameters.
Second, your HTML looks a little bit. Input element names are device IDs, product ID, and order ID. This will mean that instead deviceId=123&itemId=456&orderId=789
you get 123=123&456=456&789=789
, which is not very useful! Try changing your input fields to something like:
<input ... name="deviceId" value="<%= Html.Encode(item.DeviceId) %>" ... />
Then you can specify a parameter deviceId
in your controller like this:
public ActionResult OrderDevice(int deviceId, int itmId, int orderId)
{
// ...
}
By doing this, you will not be using Request.Form [...] yourself.
a source to share
The error indicates that the parameter you are sending to the controller from the view does not have 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 'id' parameter is passed from the view. but the actual parameter name is "propId". changing the name will correct this error.
a source to share
This error means that mvc canoot will find a value for your ID property, which must be passed as an argument. To enable this, enable the following on your controller.
public ActionResult OrderDevice (row 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 the assumption that your program contains a method that looks for Device.Hope. It helps.
a source to share