connectseekers.com

Tuesday, 15 July 2014

How to Set Asp.net MVC Controller Post Method Parameter Default Value Using C#.Net

7/15/2014 - By Pranav Singh 0

 In this article I will show you how you can set the default value to a controller post method parameter in an mvc application. We always face some situation where we don’t need to pass the parameter value but post back event take place. So in this case we will get below mention error.
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Index(Int32)' in 'PartialViewInMvc.Controllers.HomeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters

So this error occurring because we are not passing any value to parameter due to which it’s getting null. So for preventing this error we will assign some default value to parameter. In this case if it’s get value then parameter value will be the passed value otherwise it will assign the default value which we have assign.
So the get method with no parameter.

[HttpGet
public ActionResult Index()
     {
        return View();
     } 

Now httpost method with default parameter value.

  [HttpPost]
  public ActionResult Index(int id=0)
     {
        return View();
         }


Enjoy the code. Happy coding J

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top