Getting Error An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter
You are passing an object
of chain
but you have not added value of id
in it anywhere. You need to set value
of id
and add it in chain object
.
ANJYR
7 years of extensive experience in Development & Application Maintainance As a .NET FullStackDeveloper. Adept at Product Development, Technical Experimentation, Functional Design Specifications & Cloud Computing in AWS & AZURE. Active interests: AWS Cloud & Information Security.
Updated on July 08, 2022Comments
-
ANJYR almost 2 years
Getting Error after Update data into database
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult UpdateChain(Int32, HCIBE.Models.chain)' in 'HCIBE.Controllers.ChainsController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Here is My Controller
[HttpGet] public ActionResult UpdateChain(int? id) { chain objchain = db.chains.Find(id); if (objchain == null) { return HttpNotFound(); } return View(objchain); } [HttpPost] public ActionResult UpdateChain(int id, [Bind(Include = "name,code,username,password,updated_by,updated_on")] chain chain) { chain _objchain = db.chains.Find(id); try { if(ModelState.IsValid) { _objchain.code = chain.code; _objchain.name = chain.name; _objchain.username = chain.username; _objchain.password = chain.password; _objchain.updated_by = Convert.ToInt32("1"); _objchain.updated_on = DateTime.Now; db.Entry(_objchain).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } } catch (Exception Ex) { ModelState.AddModelError("", "Unable to update data"); } return View(_objchain); }
View
@using (@Html.BeginForm("UpdateChain", "Chains", FormMethod.Post)) { <div class="form-horizontal"> <hr /> <div class="form-group"> <label class="col-sm-2 control-label"> Select Chain </label> <div class="col-md-3"> @Html.DropDownList("ddlchainname", (SelectList)ViewData["chain_name"],"Select Chain", new { onchange = "Action(this.value);", @class = "form-control" }) </div> <label class="control-label"> or @Html.ActionLink("Add New", "Create") </label> </div> <div class="form-group"> <label class="col-sm-2 control-label"> Chain Name </label> <div class="col-md-3"> @Html.TextBox("ChainName", null, new { @class = "form-control" }) </div> <label class="col-sm-2 control-label"> Username </label> <div class="col-md-3"> @Html.TextBox("username", null, new { @class = "form-control" }) </div> </div> <div class="form-group"> <label class="col-sm-2 control-label"> Chain Code </label> <div class="col-md-3"> @Html.TextBox("ChainCode", null, new { @class = "form-control" }) </div> <label class="col-sm-2 control-label"> Password </label> <div class="col-md-3"> @Html.Password("password", null, new { @class = "form-control" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Update" onclick="UpdateChain()" class="btn btn-default" /> </div> </div> </div> } <script type="text/javascript"> function UpdateChain(){ var _vddlChainID = $("#ddlchainname").val(); alert("Your Selected ID = " + _vddlChainID); $.ajax({ url: '@Url.Action("UpdateChain", "Chains")', type: "POST", data: { "id": _vddlChainID } }); } </script>
Through Dropdown Selection i Fill all textboxes, when i submit form then i get Error.
Routing
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 } ); }
Model
public partial class chain { public chain() { this.templates = new HashSet<template>(); this.hotels = new HashSet<hotel>(); } public long chain_id { get; set; } public string name { get; set; } public string code { get; set; } public string username { get; set; } public string password { get; set; } public long created_by { get; set; } public System.DateTime created_on { get; set; } public Nullable<long> updated_by { get; set; } public Nullable<System.DateTime> updated_on { get; set; } public virtual ICollection<template> templates { get; set; } public virtual ICollection<hotel> hotels { get; set; } }
-
Origin over 9 yearsCare to show your routings? I believe that's the problem. The default route requires an ID.
-
Admin over 9 yearsYou have either omitted some code or not much makes sense here. Why are you using AJAX to post as well as submitting the form at the same time (you are making 2 calls - one passing the id value (AJAX) which does nothing and the other passing
chain
(SUBMIT) without theid
-
ANJYR over 9 yearsi haven't too much idea about MVC, may be my way of work is wrong so plz. guide me to correct this. thanks
-
Admin over 9 yearsYou have numerous errors. For instance you are rendering a textbox with
name="ChainName" but your model does not include a property with the namae
ChainName` (at least not according to your[Bind(Include= ...)]
list. Your[Bind(Include= ...)]
also includes properties for which you are not even rendering a control. But the main issue is your making 2 calls to the controller (the javascript is not neccessary). Please post you model so I can guide you. -
ANJYR over 9 years[Bind(Include=.....)] in this context what we pass, Html control name or table column name? i also update my question with model.
-
Admin over 9 yearsIt optional, but yes. If you use it it needs to match the names of your controls which must match the names of you properties (which it currently doesn't), but you should be using a view model in this case. Can you please explain the purpose of the dropdown and what is the script for
onchange = Action(this.value)
-
ANJYR over 9 yearsonchange=Action(this.value) Method call Controller through AJAX to bind all textbox data according to dropdown selection . you can check this stackoverflow.com/questions/26031197/…
-
Admin over 9 yearsLet us continue this discussion in chat.
-
-
ANJYR over 9 yearsAs id i passing dropdown selectedvalue, so how i pass my dropdown value there. @Url.Action("UpdateChain", "Chains",new {id= DropdownValue }) i don't know.
-
ANJYR over 9 yearsI don't know how to pass dropdown selected value into any hidden field or any where in view.
-
Mairaj Ahmad over 9 yearsChange this line @Url.Action("UpdateChain", "Chains") to @Url.Action("UpdateChain", "Chains",new {id=_vddlChainID })
-
ANJYR over 9 yearsit's not working... The name '_vddlChainID' does not exist in the current context.
-
Admin over 9 yearsThis is not necessary, the
id
value is already passed usingdata: { "id": _vddlChainID }
. The problem is 2 calls are being made to the controller. -
Twix over 9 yearsyou can directly pass it to the method like this
string ddlchainname
where string is the type of the value being passed by dropdown ddlchainname is the name of the dropdown