Reputation: 14108
Goal:
I want to achieve two parameters (one string and one bool) to my actionresult Anvandare_Listaa.
//
// GET: /Admin/Anvandare_Listaa
public ActionResult Anvandare_Listaa(string pAnvandaren, bool pBlivenAdministrator)
{
return View("Anvandare_Lista");
}
Problem:
Can't recieve bool value pBlivenAdministrator in my action result. I recieve error message.
The parameters dictionary contains a null entry for parameter 'pBlivenAdministrator' of non-nullable type 'System.Boolean' for method 'System.Web.Mvc.ActionResult Anvandare_Listaa(Boolean)' in 'BokButik1.Controllers.AdminController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters
If I use bool? instead of bool I won't get any value. There is no problem when I use string, it is only problem related to bool in the action result.
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
}
}
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Admin.Master" Inherits="System.Web.Mvc.ViewPage<BokButik1.ViewModels.AllaAnvandareViewModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Användare Lista
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Användare Lista</h2>
<table>
<tr>
<th>Användare</th>
<th>E-post</th>
<th>Administratör</th>
</tr>
<% foreach (var user in Model.AllaAnvandare) { %>
<tr>
<td>
<%: user.Anvandaren %>
</td>
<td>
<%: user.Epost%>
</td>
<td>
<%: user.BlivenAdministrator %>
</td>
<td>
<%: Html.ActionLink(Html.OmAdministratorBehorighet(user.BlivenAdministrator),
"Anvandare_Listaa", "Admin", new {pAnvandaren = user.Anvandaren, pBlivenAdministrator = user.BlivenAdministrator }) %>
</td>
</tr>
<% } %>
</table>
Upvotes: 0
Views: 2954
Reputation: 3224
Make a route that takes 2 arguments. The default route only takes a single argument "id." Create a new route with your arguments pAnvandaren and pAnvandaren.
Upvotes: 1
Reputation: 1038720
When you generate your action link:
<%: Html.ActionLink(
Html.OmAdministratorBehorighet(user.BlivenAdministrator),
"Anvandare_Listaa",
"Admin",
new {
pAnvandaren = user.Anvandaren,
pBlivenAdministrator = user.BlivenAdministrator
}
) %>
you should make sure that the pBlivenAdministrator
parameter is assigned a value so that the generated HTML looks like this:
<a href="/Admin/Anvandare_Listaa?pAnvandaren=abc&pBlivenAdministrator=true">
Some Text
</a>
If the request doesn't contain the pBlivenAdministrator
parameter you will be getting this exception. So it should be either:
/Admin/Anvandare_Listaa?pBlivenAdministrator=true
or:
/Admin/Anvandare_Listaa?pBlivenAdministrator=false
Upvotes: 1