Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

how to pass controller a model object from a view

$
0
0

hi All,

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.

CDIS.cshtml

@model MvcMovie.Controllers.Rootobject;


@{
    ViewData["Title"] = "CDIS subordinates";
}<h2>@ViewData["Title"] for Group ID: <b>@Html.DisplayFor(model => model.data[0].groupId)</b></h2><h3>@ViewData["Message"]</h3><table class="table"><thead><tr><th>
                @Html.DisplayNameFor(model => model.data[0].id)</th><th>
                @Html.DisplayNameFor(model => model.data[0].fullName)</th><th>
                @Html.DisplayNameFor(model => model.data[0].sourceSystem)</th><th></th></tr></thead><tbody>
        @foreach (var item in Model.data)
        {

            if (item.sourceSystem != "CRYSTAL_GLOBAL")
            {<tr><td>
                        @Html.DisplayFor(modelItem => item.id)</td><td>
                        @Html.DisplayFor(modelItem => item.fullName)</td><td>
                        @Html.DisplayFor(modelItem => item.sourceSystem)</td></tr>
            }

        }

        <tr><td><form asp-controller="CDISRecords" asp-action="CONIS" method="get"><p><input type="hidden" name="GroupID" value="@Html.DisplayFor(model => model.data[0].groupId)"><input type="hidden" name="dataModel" value=@Html.DisplayFor(model => model.data)><input type="hidden" name="dModel" asp-for="@Model.data" /><div><input type="submit" value="GET Consent" class="btn btn-default" />

                            | For Date | :
                            <input type="date" name="consentDate"></div></p></form></td></tr></tbody></table>

CDISController.cs
    

<div>
 public async Task<IActionResult> CONIS(int? GroupID, MvcMovie.Controllers.Datum rootobject, MvcMovie.Controllers.Datum dModel)        {                Console.WriteLine($"City CONIS: " + dModel.city + "Group ID CONIS: " + rootobject.groupId + "Full Name CONIS : " + dModel.fullName);                        return 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.</div> <div></div> <div>On the console, I see the following:</div> <div></div>

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. 


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>