c# - ViewModel data is all null when passed to my controller? -


this question has answer here:

i'm trying follow best practices add data validation ui. want add data validation viewmodel , if data valid, submit form controller. however, data controller receives null values. idea i'm doing wrong? whole mvvc architecture confusing me. had working when submitting form using model can't data validation on model?

controller:

[httppost] [validateantiforgerytoken] public async task<actionresult> createresource(addresourceviewmodel model)         {             if (modelstate.isvalid)             {                 await (//does something);             }             return view();         } 

modelview:

public class addresourceviewmodel {          public string user { get; set; }                public string resourcename { get; set; }         public int resourceid { get; set; }         public string model { get; set; }         public float latitude { get; set; }         public float longitude { get; set; }         public decimal amount { get; set; }         public int unitid { get; set; }         public int primaryesfid { get; set; }         public ienumerable<string> capabilities { get; set; }         public ienumerable<int> additionalesfs { get; set; }          public resource resource { get; set; }   } 

beginning of cshtml form:

@model erms.viewmodel.addresourceviewmodel <form asp-controller="addresource" asp-action="newresource" method="post" class="form-horizontal"> <div class="panel panel-default">     <div class="panel-body">         <form asp-controller="addresource" asp-action="createresource" method="post" class="form-horizontal">              <div asp-validation-summary="all" class="text-danger"></div>             <div class="form-group">                 <label asp-for="resourceid" class="col-md-2 control-label">resource id:</label>                 <div class="col-md-10">                     <input asp-for="resourceid" class="form-control" value="@model.resource.resourceid" readonly />                     <span asp-validation-for="resourceid" class="text-danger"></span>                 </div>             </div> 

replace return view(); return view(model);.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -