I am using the following action to save Nav Bar Items edited from with admin of a content management system:
[HttpPost]
public ActionResult aSaveNavs()
{
aLoggedIn();
NavItemsDataContext navDB = new NavItemsDataContext();
Nav n = navDB.Navs.Select(row => row.ID == Convert.ToInt32(Request.Form["ID"]));
n.NavName = Request.Form["NavName"];
n.NavURL = Request.Form["NavURL"];
n.NavEnabled = (Request.Form["NavEnabled"] == "true" ? true : false);
navDB.SubmitChanges();
return Redirect("/Admin/aHome");
}
I am using the following view:
@{
List<Nav> navList = HtmlHelpers.GetNavList();
}
@foreach (Nav item in navList)
{
<tr>
<td style="width: 150px; text-align: center;">
@item.NavName
</td>
<td style="width: 150px; text-align: center;">
<input id="NavName" name="NavName" type="text" value="@item.NavName" />
</td>
<td style="width: 150px; text-align: center;">
<input id="NavURL" name="NavURL" type="text" value="@item.NavURL" />
</td>
<td>
<input id="ID" name="ID" type="text" readonly="readonly" value="@item.ID" />
</td>
</tr>
}
Obviously the view has a few other bits in but this is the section in question.
When I try and save my changes, I get “System.FormatException: Input string was not in a correct format.” and the following line is highlighted:
Nav n = navDB.Navs.FirstOrDefault(row => row.ID == Convert.ToInt32(Request.Form["ID"]));
Thanks in advance for any help with this.
Convert.ToInt32 may still throw an error, for example when it is empty, therefore use this:
and then in your code: