This is a migrated thread and some comments may be shown as answers.

ho

10 Answers 76 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Suleman
Top achievements
Rank 1
Suleman asked on 09 Feb 2018, 07:30 PM

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

<p><br>@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> <br></p><p></p>

CDISController.cs
    

 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.

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

Sort by
0
Giuseppe
Top achievements
Rank 1
answered on 10 Feb 2018, 06:56 AM

Hi

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.

 

 

 

0
Suleman
Top achievements
Rank 1
answered on 10 Feb 2018, 10:05 AM

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:

.if (response.IsSuccessStatusCode)
            {
               string st = await response.Content.ReadAsStringAsync();
 
                 Console.WriteLine($"Response: " + st);
 
...

 

Now how can i put the json into a hidden field in my view, so that i can deserialize it back on the controller?

Many thanks.

0
Suleman
Top achievements
Rank 1
answered on 10 Feb 2018, 10:12 AM

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.

Rootobject datalist = Newtonsoft.Json.JsonConvert.DeserializeObject<Rootobject>(st);
               
              //Console.WriteLine($"ID: " + datalist.data[0].id + "Group ID : " + datalist.data[0].groupId + "Full Name : " + datalist.data[0].fullName);
 
              foreach (Datum d in datalist.data)
              {
                  Console.WriteLine($"ID: " + d.id + "Group ID : " + d.groupId + "Full Name : " + d.fullName);
              }

 

 

0
Giuseppe
Top achievements
Rank 1
answered on 11 Feb 2018, 04:31 PM

I'm using a simplified model to show how it works:

model/controller code:

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
 
 
 
public class HomeController : Controller
{
    public IActionResult Index()
    {
 
        var p = new Person();
        p.FirstName = "Elon";
        p.LastName = "Musk";
        ViewBag.SerializedPerson = Newtonsoft.Json.JsonConvert.SerializeObject(p);
        return View();
    }
 
 
    public IActionResult PostData(string jsonPerson)
    {
        var p = Newtonsoft.Json.JsonConvert.DeserializeObject<Person>(jsonPerson);
 
        return View("Index");
 
    }
}

 

View:

<form asp-action="PostData" asp-controller="Home">
    @Html.Hidden("jsonperson", (string)ViewBag.SerializedPerson)
    <input type="submit" value="submit" />
</form>

 

Hope that helps.

Giuseppe

0
Suleman
Top achievements
Rank 1
answered on 12 Feb 2018, 08:15 AM
Many thanks for your help, Giuseppe.
0
Suleman
Top achievements
Rank 1
answered on 12 Feb 2018, 06:35 PM

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.

var CONISRootObjectList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<CONISRootObject>> (conisResponse);
                               
                foreach (CONISRootObject cRO in CONISRootObjectList)
                {                    
                    foreach (CONISSubObject cSO in cRO.Properties)
                    {
 
                        Console.WriteLine($" Predicate : " + cSO.predicate + " Object Type : " + cSO.objectType);
 
                        foreach (ConsentData cData in cSO.consentData)
                        {
                            Console.WriteLine($" Key : " + cData.key + " Value : " + cData.value);
 
                        }
                    }
                }

 

public class CONISRootObject
    {
        public CONISSubObject[] Properties { get; set; }
    }
 
    public class CONISSubObject
    {
        public string predicate { get; set; }
        public string objectType { get; set; }
        public ConsentData[] consentData { get; set; }
    }
 
    public class ConsentData
    {
        public string key { get; set; }
        public string value { get; set; }
    }
[
    {
        "predicate": "AGREES_COMMUNICATE",
        "objectType": "PersonalData",
        "consentData": [
            {
                "key": "objCreatedBy",
                "value": "IMKRY"
            },
            {
                "key": "mailAddress",
                "value": "0b6cbeb86e42c6191671b34b7c9808e31c9e5f5acaecaf9ee4cfbd379abcdba9"
            },
            {
                "key": "objCreatedAt",
                "value": "2018-02-07 14:58:43"
            }
        ]
    },
    {
        "predicate": "AGREES_STORAGE",
        "objectType": "PersonalData",
        "consentData": [
            {
                "key": "objCreatedBy",
                "value": "IMKRY"
            },
            {
                "key": "mailAddress",
                "value": "0b6cbeb86e42c6191671b34b7c9808e31c9e5f5acaecaf9ee4cfbd379abcdba9"
            },
            {
                "key": "objCreatedAt",
                "value": "2018-02-07 14:58:43"
            }
        ]
    },
    {
        "predicate": "HAS_RAWDATA",
        "objectType": "RawDemoMail1Consent",
        "consentData": [
            {
                "key": "PersonalID",
                "value": "0012400000ayGEaAAM"
            },
            {
                "key": "objCreatedBy",
                "value": "IMKRY"
            },
            {
                "key": "MailAddress",
                "value": "0b6cbeb86e42c6191671b34b7c9808e31c9e5f5acaecaf9ee4cfbd379abcdba9"
            },
            {
                "key": "GivenDate",
                "value": "2017-11-02"
            },
            {
                "key": "Domain",
                "value": "aspirin.com"
            },
            {
                "key": "ConsentID",
                "value": "aWe7-12373"
            },
            {
                "key": "objCreatedAt",
                "value": "2018-02-07 14:33:33"
            },
            {
                "key": "SourceSystem",
                "value": "VEEVA_FR"
            }
        ]
    },
    {
        "predicate": "IS_VALID_FOR",
        "objectType": "Context",
        "consentData": [
            {
                "key": "objCreatedBy",
                "value": "IMKRY"
            },
            {
                "key": "webDomain",
                "value": "aspirin.com"
            },
            {
                "key": "objCreatedAt",
                "value": "2018-02-06 15:05:00"
            }
        ]
    },
    {
        "predicate": "IS_VALID_TO_DATE",
        "objectType": "TimeTree,Day",
        "consentData": [
            {
                "key": "objCreatedBy",
                "value": "imkry"
            },
            {
                "key": "dateStr",
                "value": "2018-11-01"
            },
            {
                "key": "month",
                "value": "11"
            },
            {
                "key": "year",
                "value": "2018"
            },
            {
                "key": "objCreatedAt",
                "value": "2018-02-05 14:44:09"
            },
            {
                "key": "day",
                "value": "1"
            }
        ]
    },
    {
        "predicate": "IS_VALID_FROM_DATE",
        "objectType": "TimeTree,Day",
        "consentData": [
            {
                "key": "objCreatedBy",
                "value": "imkry"
            },
            {
                "key": "dateStr",
                "value": "2017-11-02"
            },
            {
                "key": "month",
                "value": "11"
            },
            {
                "key": "year",
                "value": "2017"
            },
            {
                "key": "objCreatedAt",
                "value": "2018-02-05 14:44:09"
            },
            {
                "key": "day",
                "value": "2"
            }
        ]
    }
]

 

0
Giuseppe
Top achievements
Rank 1
answered on 13 Feb 2018, 09:34 AM

I don't know buddy. Your Json doesn't have any Properties, so i guess they are not serialized in the first place?

 

0
Suleman
Top achievements
Rank 1
answered on 13 Feb 2018, 02:30 PM

Thanks for asking, but the problem got resolved!

List<CONISSubObject> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<CONISSubObject>>(conisResponse);
 
             foreach (CONISSubObject cSO in list)
             {
                 Console.WriteLine($" Predicate : " + cSO.predicate);
                 Console.WriteLine($" Object Type : " + cSO.objectType);
 
                 Console.WriteLine($" Consent Data : [ ");
                 foreach (ConsentData cData in cSO.consentData)
                 {
                     Console.WriteLine($" Key : " + cData.key);
                     Console.WriteLine($" Value : " + cData.value);
                 }
                 Console.WriteLine($"]");
             }
             return View(list);
0
Suleman
Top achievements
Rank 1
answered on 13 Feb 2018, 02:33 PM

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?

For example,

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="post">
                    <p>
                        <input type="hidden" name="GroupID" value="@Html.DisplayFor(model => model.data[0].groupId)">
                        @Html.Hidden("jsonDatalist", (string)ViewBag.SerializedDatalist)
                        <div>
                            <input type="submit" value="GET Consent" class="btn btn-default" />
                            | For Date | :
                            <input type="date" id="dd" name="consentDate" required pattern="(?:19|20)[0-9]{2}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9])|(?:(?!02)(?:0[1-9]|1[0-2])-(?:30))|(?:(?:0[13578]|1[02])-31))">
                            <script type="text/javascript">
                                $(document).ready(function () {
                                    var date = new Date();
 
                                    var day = date.getDate();
                                    var month = date.getMonth() + 1;
                                    var year = date.getFullYear();
 
                                    if (month < 10) month = "0" + month;
                                    if (day < 10) day = "0" + day;
 
                                    var today = year + "-" + month + "-" + day;
                                    $("#dd").attr("value", today);
                                });
                            </script>
 
                        </div>
                    </p>
                </form>
            </td>
        </tr>
    </tbody>
</table>
<div>
    <a asp-action="Index">Back to List</a>
</div>

 

CONIS.cshtml

@model List<MvcMovie.Controllers.CONISSubObject>
 
@if (Model.Count != 0)
{
 
    {
        ViewData["Title"] = "CONIS Details for CDIS Subordinate ID(s): " + (string)ViewBag.SerializedQueryString + " and Date: " + (string)ViewBag.SerializedQueryStringDate;
    }
 
    <h2>@ViewData["Title"]</h2>
    <h3>@ViewData["Message"]</h3>
 
 
    <table class="table">
        <thead>
            <tr>
                <th>
                    Predicate
                </th>
                <th>
                    Object Type
                </th>
                <th>
                    Consent Data
                </th>
 
                <th></th>
            </tr>
        </thead>
        <tbody>
 
            @foreach (var cm in Model)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(model => cm.predicate);
                    </td>
                    <td>
                        @Html.DisplayFor(model => cm.objectType);
                    </td>
                    <td>
                        <table class="table">
                            <thead>
                                <tr>
                                    <th>
                                        Key
                                    </th>
                                    <th>
                                        Value
                                    </th>
                                    <th></th>
                                </tr>
                            </thead>
 
                            <tbody>
 
                                @foreach (var cmm in cm.consentData)
                                {
                                    <tr>
 
 
                                        <td>
                                            @Html.DisplayFor(model => cmm.key);
                                        </td>
                                        <td>
                                            @Html.DisplayFor(model => cmm.value);
                                        </td>
                                    </tr>
                                }
 
 
                            </tbody>
                        </table>
                    </td>
 
                </tr>
            }
 
    </table>
 
}
else
{
 
    <h2>No Consent(s) found for CDIS Subordinate ID(s): <b>@ViewBag.SerializedQueryString</b> and Date: <b>@ViewBag.SerializedQueryStringDate</b></h2>
 
}
 
 
<div>
    <a asp-action="CDIS" asp-route-GroupID=@ViewBag.SerializedGroupID>Back to List</a>
</div>

 

Based on the input from the first view, i want to show the 2nd view at the bottom of the same view.

Any pointers?

Thanks.

0
Neli
Telerik team
answered on 14 Feb 2018, 03:57 PM
Hello Suleman,

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:
https://stackoverflow.com/questions/4764011/multiple-models-in-a-view
http://www.c-sharpcorner.com/UploadFile/ff2f08/multiple-models-in-single-view-in-mvc/
https://www.codeproject.com/Articles/1108855/ways-to-Bind-Multiple-Models-on-a-View-in-MVC

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:
- https://stackoverflow.com/questions/29624853/show-result-in-same-view-after-submit-mvc

If you have questions concerning specific Kendo UI functionality, widgets/MVC wrappers configuration or API, etc. we will be happy to assist you.

Regards,
Neli
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
General Discussions
Asked by
Suleman
Top achievements
Rank 1
Answers by
Giuseppe
Top achievements
Rank 1
Suleman
Top achievements
Rank 1
Neli
Telerik team
Share this question
or