Hi I have Faced With a Problem When Im Trying To Register. This Is My Code :
public ActionResult RegisterButton(Models.Users User)
{
using (MyDbContext db = new MyDbContext())
{
if (ModelState.IsValid == false)
{
return View("Register", User);
}
else
{
db.Users.Add(User);
db.SaveChanges();
Session["UserId"] = User.Id;
//Directory.CreateDirectory(string.Format("~/App_Data/{0}",User.UserName+User.Id.ToString()));
return RedirectToAction("Profile", "Profile",new { User.Id});
}
}
}
And This Is Also My Route Config Code :
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
And I Get This Error : The parameters dictionary contains a null entry for parameter 'UserId' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Profile(Int32)' in 'DigiDaroo.Controllers.ProfileController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Please Help:|
Based on the code you provided, your RegisterButton
method will return a redirect response to the browser with location header value like this
/Profile/Profile/101
Where 101
is replaced with the actual ID of the new User record. With the routing configuration you have, your code would not throw that error message if your action method parameter name is id
. Since you are getting the error message, i assume your action method parameter name is something else. So make sure you are explicitly passing that the routeValue object.
For example, if your action method parameter name is userId
like this
public ActionResult Profile(int userId)
{
// to do : return something
}
your redirect response call should be like this
return RedirectToAction("Profile", "Profile",new { userId = User.Id});
This will send the location header value for the redirect response as /Profile/Profile?userId=101
and browser will use this to make the GET request. since we are explicitly passing the userId
parameter in querystring, your error parameter will be properly filled with the value 101
Another option is to change your action method parameter name to id
public ActionResult Profile(int id)
{
// to do : return something
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.