I have a razor view "CDIS.cshtml" which is using a model object to render content. Now in this view, i have a submit button which calls an action method of the controller. In this action method, i need to use the same model object, which was used to render data in the previous view. My controller action method somehow gets the model object from the view, but it is empty. My target in the controller action is to use the same model object's values, which were used in the last view.
In the above controller action, i have tried different ways to pass the model from the view, but each time, the values *-> City CONIS: " + dModel.city + "Group ID CONIS: " + rootobject.groupId + "Full Name CONIS : " + dModel.fullName *, are empty.
On the console, I see the following:
MvcMovie> Executing action method MvcMovie.Controllers.CDISRecordsController.CONIS (MvcMovie) with arguments (39256600, MvcMovie.Controllers.Datum, MvcMovie.Controllers.Datum) - ModelState is Valid
MvcMovie> City CONIS: Group ID CONIS: 39256600Full Name CONIS :
Perhaps my approach is incorrect, or I am passing the model reference incorrectly, because "City CONIS" is empty and Full Name CONIS is also empty. Somehow, Group ID CONIS is showing value. I have alos tried, for City and Name with "rootobject" like $"City CONIS: " + rootobject.city, but empty.
Any help shall highlly be appreciated. In nutshell, I need the values (appearing the table inside a view) to be passed to the controller to do further action. The values in the view are not fixed number of textfields rather values that i am showing in the table, hence i need the object to be passed further so that i can iterate on the object.
10 Answers, 1 is accepted
Your question is not related to Kendo, so maybe you're better up asking these questions in StackOverflow.
First of all use Html.HiddenFor instead of <input type="hidden" />
second model.data seems to be a list/array and if you do DisplayFor(m => m.data) it will proably just call m.data.ToString() and you'll the the type name of model.data -> can't work.
(and why is your model in the namespace controller? possible but not advised)
you would be probablly better up to serialize m.data to on the server to json and the put the json version into a hidden field, and then deserialize it back on the controller.
Many thanks for your reply, Guiseppe.
I am very much new to asp.net core and c# and kind of lost:(
In my view now, I have added: @Html.HiddenFor(model => model.data)
Now how can I get the model.data as a list/array in my controller?
I have tried in the controller action with the argument: IFormCollection collection
But now not sure how to get the model out of the collection?
On the other hand, I have before hand put my model in the json object as:
Now how can i put the json into a hidden field in my view, so that i can deserialize it back on the controller?
in my last statement i meant to say is that i am getting the http response as json, i deserialize it and show the results in my view, now from this view i need to pass the same results back to my controller. so that i can do further processing. The problem is ho can i pass this deserialzed object back to my controller.
I'm using a simplified model to show how it works:
Hope that helps.
i am now facing problems in iterating through my Deserialized json response.
My JSON and classes structure are stated below. The code I am using to iterate is as follows:
But I get this error: NullReferenceException: Object reference not set to an instance of an object.
at: ...foreach (CONISSubObject cSO in cRO.Properties)...
Any hint why is that the case? Thanks.
I don't know buddy. Your Json doesn't have any Properties, so i guess they are not serialized in the first place?
Thanks for asking, but the problem got resolved!
Another problem now i am facing is to combine 2 views in the main view. It seems it is not possible to use 2 models in one view!
So how can I render my 2 views together in one page/view?
Based on the input from the first view, i want to show the 2nd view at the bottom of the same view.
A possible approach in order to pass multiple models to a single view is to create a common model for all needed models. In the View page, you need to reference only the common model.
As the problem is a general MVC Core issue and is not related to Kendo UI or Telerik UI for ASP.NET Core, I would suggest you to take a look of the following links where such issue has been discussed:
In case you need to show additional info, when a form is submitted, you can achieve that by using AJAX call. Similar issue is discussed in the following thread in StackOverflow:
If you have questions concerning specific Kendo UI functionality, widgets/MVC wrappers configuration or API, etc. we will be happy to assist you.