c# - Null Entry for NonNullable Type in ASP.Net MVC 5 project -


i have property in model needs non nullable. when submitting form if model state isinvalid, want user redirected same page. whenever modelstate invalid getting exception

exception

the parameters dictionary contains null entry parameter 'referralid' of non-nullable type 'system.int32' method 'system.web.mvc.actionresult referralconfirmation(int32)' in 'bridge.controllers.referrercontroller'. optional parameter must reference type, nullable type, or declared optional parameter. parameter name: parameters

model:

public class referrerinstanceviewmodel     {         [key]         public int id { get; set; }          [filetype("pdf|doc|docx|pdf", errormessage = "file type not valid.")]         [required]         public httppostedfilebase proofdoc { get; set; }          [required]         public int referralid { get; set; }          [required]         public int? referralstatusid { get; set; }         public ienumerable<selectlistitem> referralstatuses { get; set; }      } 

controller action:

    public actionresult referralconfirmation(int referralid)     {         var viewmodel = new referrerinstanceviewmodel         {             referralid = referralid,             referralstatuses = _context.referralstatuses.select(x => new selectlistitem             {                 text = x.referralstatustype,                 value = x.referralstatusid.tostring()             })         };          return view(viewmodel);     }      [httppost]     [validateantiforgerytoken]     public actionresult referralconfirmation(referrerinstanceviewmodel viewmodel)     {         if (!modelstate.isvalid)             return redirecttoaction("referralconfirmation", viewmodel.referralid);        // logic          return redirecttoaction("referrercenter");     } 

view:

@using (html.beginform("referralconfirmation", "referrer", null, formmethod.post, new { enctype = "multipart/form-data" })) {     @html.antiforgerytoken()     @html.hiddenfor(m=>m.referralid)      <div class="form-horizontal">         <h4>referrerinstanceviewmodel</h4>         <hr />         @html.validationsummary(false, "", new { @class = "text-danger" })         <div class="form-group">             @html.labelfor(model => model.referralstatusid, htmlattributes: new { @class = "control-label col-md-2" })             <div class="col-md-10">                 @html.dropdownlistfor(model => model.referralstatusid, model.referralstatuses, "reject or refer", new { @class = "form-control" })                 @html.validationmessagefor(model => model.referralid, "*", new { @class = "text-danger" })             </div>         </div>          <div class="form-group">             @html.labelfor(model => model.proofdoc, htmlattributes: new { @class = "control-label col-md-2" })             <div class="col-md-10">                 @html.textboxfor(m => m.proofdoc, new { type = "file" })                 @html.validationmessagefor(model => model.proofdoc, "*", new { @class = "text-danger" })             </div>         </div>          <div class="form-group">             <div class="col-md-offset-2 col-md-10">                 <input type="submit" value="create" class="btn btn-default" />             </div>         </div>     </div> } 

points noted:

  1. when modelstate invalid, calling redirecttoaction("referralconfirmation", passing required referralid value here)
  2. when model state invalid, breakpoint @ httpget referralconfirmation action not getting hit.

i not able understand behavour. what's wrong doing.

kindly note url that's getting generated after modelstate invalid

https://localhost:44328/referrer/referralconfirmation

when calling redirecttoaction object, should pass anonymous object property name matching parameter name of referralconfirmation action method

return redirecttoaction("referralconfirmation",                                     new { referralid = viewmodel.referralid } ); 

this return 302 response location value /referralconfirmation?referralid=100 assuming value in viewmodel.referralid 100

also when model state not valid, there no point in doing redirect. should return same view user can fix view , resubmit.

[httppost] [validateantiforgerytoken] public actionresult referralconfirmation(referrerinstanceviewmodel viewmodel) {     if (modelstate.isvalid)     {        //to : code before redirect goes here          return redirecttoaction("referralconfirmation",                                           new { referralid = viewmodel.referralid });     }     //model validation failed. let's return same view     return view(viewmodel); } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -