Multiple Tables in one View - c#

I have a model (Model2) which contains a definition for all of my SQL tables. I need to create a view in which I display data from multiple different tables. These are the Models I want to join in one view
HEADER_RECORD.cs
namespace BillingApp.Models
{
using System;
using System.Collections.Generic;
public partial class HEADER_RECORD
{
public int HRID { get; set; }
public string TABLE_NUMBER { get; set; }
public string COMPANY { get; set; }
public string STATE_CODE { get; set; }
public string BILL_CODE { get; set; }
public string RECORD_TYPE { get; set; }
public string MASK_EXTENSION_ID { get; set; }
public string OVERPAYMENT_LIMIT { get; set; }
public string UNDERPAYMENT_LIMIT { get; set; }
public string REFUND_ACTION_OVR { get; set; }
public string REFUND_ACTION_PAR { get; set; }
public string REFUND_ACTION_RTN_PRM { get; set; }
public string REFUND_ACTION_CNC { get; set; }
public string EFT_PAC_OPTION { get; set; }
public string EFT_PAC_NOTICE { get; set; }
public string EFT_PAC_NSF_LIMIT { get; set; }
public string PREMIUM_ROUNDING { get; set; }
public string DB_CC_OPTION { get; set; }
public string NSF_CHECK_LIMIT { get; set; }
public string NSF_CHECK_OPTION { get; set; }
public string FIRST_TERM_BILLING { get; set; }
public string CARRY_DATE_OPTION { get; set; }
public string ENDORSEMENT_DAYS { get; set; }
public string DATE_METHOD { get; set; }
public string RENEWAL_OPTION { get; set; }
public string DROP_DAYS { get; set; }
public string MULTI_PAY_IND { get; set; }
public string MINIMUM_INSTALLMENT { get; set; }
public string ENDORSEMENT_ACTION { get; set; }
public string I_OR_S_OPTION_DAYS { get; set; }
public string S_OPTION_PERCENT { get; set; }
public string SERVICE_CHARGE_PREPAID { get; set; }
public string REINSTATE_OPTION { get; set; }
public string CASH_WITH_APPLICATION { get; set; }
public string DB_CC_NOTICE { get; set; }
public string DOWN_PAY_DAYS { get; set; }
public string MONTH_BY_TERM { get; set; }
public string LEAD_MONTHS { get; set; }
public string INITIAL_MONTHS { get; set; }
public string DB_CC_REJECTS { get; set; }
public string RETURN_ENDORSEMENT_OPTION { get; set; }
public string RETURN_SPLIT_OPTION_PERCENT { get; set; }
public string AUTOMATED_REFUND_DAYS { get; set; }
public string RENEWAL_OPTION_BILL_PLAN { get; set; }
public string EFFECTIVE_DATE { get; set; }
public string MISC_DATA { get; set; }
public string MISC_DATA2 { get; set; }
}
}
HEADER_EXTENSION_RECORD.cs
public partial class HEADER_EXTENSION_RECORD
{
public int ERID { get; set; }
public string ETABLE_NUMBER { get; set; }
public string ECOMPANY { get; set; }
public string ESTATE_CODE { get; set; }
public string EBILL_CODE { get; set; }
public string ERECORD_TYPE { get; set; }
public string EMASK_EXTENSION_ID { get; set; }
public string OVERPAYMENT_TOLERANCE_PERCENT { get; set; }
public string UNDERPAYMENT_TOLERANCE_PERCENT { get; set; }
}
}
The top of My View (HeaderRecordTable1)
#model IEnumerable<BillingApp.Models.HEADER_RECORD>
#{
ViewBag.Title = "TABLE 01 DISPLAY";
Layout = "../Shared/Layout2.cshtml";
}
#section featured2 {
Update: I created a class called tbl1join, which has subclasses defining the two tables I want to join. I referenced this class (tbljoin.cs) in the view, however it's no longer recognizing any of the field names I'm calling.
" CS1061: 'BillingApp.Models.tbl1join' does not contain a definition for 'COMPANY' and no extension method 'COMPANY' accepting a first argument of type 'BillingApp.Models.tbl1join' could be found (are you missing a using directive or an assembly reference?)"
tbl1join.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace BillingApp.Models
{
public class tbl1join
{
public partial class HEADER_RECORD
{
public int HRID { get; set; }
public string TABLE_NUMBER { get; set; }
public string COMPANY { get; set; }
public string STATE_CODE { get; set; }
public string BILL_CODE { get; set; }
public string RECORD_TYPE { get; set; }
public string MASK_EXTENSION_ID { get; set; }
public string OVERPAYMENT_LIMIT { get; set; }
public string UNDERPAYMENT_LIMIT { get; set; }
public string REFUND_ACTION_OVR { get; set; }
public string REFUND_ACTION_PAR { get; set; }
public string REFUND_ACTION_RTN_PRM { get; set; }
public string REFUND_ACTION_CNC { get; set; }
public string EFT_PAC_OPTION { get; set; }
public string EFT_PAC_NOTICE { get; set; }
public string EFT_PAC_NSF_LIMIT { get; set; }
public string PREMIUM_ROUNDING { get; set; }
public string DB_CC_OPTION { get; set; }
public string NSF_CHECK_LIMIT { get; set; }
public string NSF_CHECK_OPTION { get; set; }
public string FIRST_TERM_BILLING { get; set; }
public string CARRY_DATE_OPTION { get; set; }
public string ENDORSEMENT_DAYS { get; set; }
public string DATE_METHOD { get; set; }
public string RENEWAL_OPTION { get; set; }
public string DROP_DAYS { get; set; }
public string MULTI_PAY_IND { get; set; }
public string MINIMUM_INSTALLMENT { get; set; }
public string ENDORSEMENT_ACTION { get; set; }
public string I_OR_S_OPTION_DAYS { get; set; }
public string S_OPTION_PERCENT { get; set; }
public string SERVICE_CHARGE_PREPAID { get; set; }
public string REINSTATE_OPTION { get; set; }
public string CASH_WITH_APPLICATION { get; set; }
public string DB_CC_NOTICE { get; set; }
public string DOWN_PAY_DAYS { get; set; }
public string MONTH_BY_TERM { get; set; }
public string LEAD_MONTHS { get; set; }
public string INITIAL_MONTHS { get; set; }
public string DB_CC_REJECTS { get; set; }
public string RETURN_ENDORSEMENT_OPTION { get; set; }
public string RETURN_SPLIT_OPTION_PERCENT { get; set; }
public string AUTOMATED_REFUND_DAYS { get; set; }
public string RENEWAL_OPTION_BILL_PLAN { get; set; }
public string EFFECTIVE_DATE { get; set; }
public string MISC_DATA { get; set; }
public string MISC_DATA2 { get; set; }
}
public partial class HEADER_EXTENSION_RECORD
{
public int ERID { get; set; }
public string ETABLE_NUMBER { get; set; }
public string ECOMPANY { get; set; }
public string ESTATE_CODE { get; set; }
public string EBILL_CODE { get; set; }
public string ERECORD_TYPE { get; set; }
public string EMASK_EXTENSION_ID { get; set; }
public string OVERPAYMENT_TOLERANCE_PERCENT { get; set; }
public string UNDERPAYMENT_TOLERANCE_PERCENT { get; set; }
}
}
}
HeaderRecordTable1.cshtml
model IEnumerable<BillingApp.Models.tbl1join>
#{
ViewBag.Title = "TABLE 01 DISPLAY";
Layout = "../Shared/Layout2.cshtml";
}
#section featured2 {
Update: I now have tbl1join.cs as below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace BillingApp.Models
{
public class tbl1join
{
public HEADER_RECORD HeaderRecord { get; set; }
public HEADER_EXTENSION_RECORD ExtensionRecord { get; set; }
}
}
In the view I have
#model IEnumerable<BillingApp.Models.tbl1join>
#{
ViewBag.Title = "TABLE 01 DISPLAY";
Layout = "../Shared/Layout2.cshtml";
}
#section featured2 {
calling the fields in a foreach
#foreach (var item in Model)
{
<tr>
<td>
<p class="one">#Html.DisplayFor(modelItem => item.HeaderRecord.COMPANY )</p>
</td>
But I'm receiving the following error: "The model item passed into the dictionary is of type 'System.Data.Entity.DbSet1[BillingApp.Models.HEADER_RECORD]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[BillingApp.Models.tbl1join]'."
Update: Still Experiencing this issue - my controller code is below
Controller Code:
public ActionResult HeaderRecordTable1()
{
{
return View(db.HEADER_RECORD);
}
}
public ActionResult HeaderExtensionRecord()
{
{
return View(db.HEADER_EXTENSION_RECORD);
}
}

Take your HEADER_RECORD and HEADER_EXTENSION_RECORD classes out of your tbl1join class. If tbl1join is meant to be your viewmodel, give it the properties it needs and populate them from instances of your HEADER_RECORD and HEADER_EXTENSION_RECORD classes. Otherwise, your tbl1join class could also look like this:
public class tbl1join
{
public HEADER_RECORD HeaderRecord {get;set;}
public HEADER_EXTENSION_RECORD ExtensionRecord {get;set;}
}
Where the HEADER_RECORD and HEADER_EXTENSION_RECORD become properties of your viewmodel and not nested classes.
Make sense?

Related

Cannot Deserialize JSON Object type requires a JSON array (e.g. [1,2,3]) to deserialize correctly

I'm having some issues handling a JSON array, Here is what I've tried. I have tried also using <List<jsonResponse.RootObect> and get the same result.
I'm using JSON.NET
C#:
jsonResponse.RootObject deserializedResponse = JsonConvert.DeserializeObject<jsonResponse.RootObject>(Globals.jsonResponse);
CLASS:
namespace QuantumView
{
[JsonObjectAttribute]
class jsonResponse
{
public class TransactionReference
{
public string CustomerContext { get; set; }
}
public class Response
{
public TransactionReference TransactionReference { get; set; }
public string ResponseStatusCode { get; set; }
public string ResponseStatusDescription { get; set; }
}
public class SubscriptionStatus
{
public string Code { get; set; }
public string Description { get; set; }
}
public class StatusType
{
public string Code { get; set; }
public string Description { get; set; }
}
public class Address
{
public string AddressLine1 { get; set; }
public string City { get; set; }
public string StateProvinceCode { get; set; }
public string PostalCode { get; set; }
public string CountryCode { get; set; }
}
public class Shipper
{
public string Name { get; set; }
public string ShipperNumber { get; set; }
public Address Address { get; set; }
}
public class Address2
{
public string ConsigneeName { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string City { get; set; }
public string StateProvinceCode { get; set; }
public string PostalCode { get; set; }
public string CountryCode { get; set; }
}
public class ShipTo
{
public string AttentionName { get; set; }
public string PhoneNumber { get; set; }
public Address2 Address { get; set; }
}
public class ReferenceNumber
{
public string Number { get; set; }
public string Value { get; set; }
}
public class Service
{
public string Code { get; set; }
}
public class Activity
{
public string Date { get; set; }
public string Time { get; set; }
}
public class Dimensions
{
public string Length { get; set; }
public string Width { get; set; }
public string Height { get; set; }
}
public class UnitOfMeasurement
{
public string Code { get; set; }
}
public class DimensionalWeight
{
public UnitOfMeasurement UnitOfMeasurement { get; set; }
public string Weight { get; set; }
}
public class PackageWeight
{
public string Weight { get; set; }
}
public class ReferenceNumber2
{
public string Number { get; set; }
public string Value { get; set; }
}
public class PackageServiceOptions
{
public string COD { get; set; }
}
[JsonArray]
public class Package
{
public Activity Activity { get; set; }
public Dimensions Dimensions { get; set; }
public DimensionalWeight DimensionalWeight { get; set; }
public PackageWeight PackageWeight { get; set; }
public string TrackingNumber { get; set; }
public List<ReferenceNumber2> ReferenceNumber { get; set; }
public PackageServiceOptions PackageServiceOptions { get; set; }
}
public class BillToAccount
{
public string Option { get; set; }
public string Number { get; set; }
}
[JsonArray]
public class Manifest
{
public Shipper Shipper { get; set; }
public ShipTo ShipTo { get; set; }
public List<ReferenceNumber> ReferenceNumber { get; set; }
public Service Service { get; set; }
public string PickupDate { get; set; }
public string ScheduledDeliveryDate { get; set; }
public string ScheduledDeliveryTime { get; set; }
public string DocumentsOnly { get; set; }
public Package Package { get; set; }
public string ShipmentChargeType { get; set; }
public BillToAccount BillToAccount { get; set; }
}
public class SubscriptionFile
{
public string FileName { get; set; }
public StatusType StatusType { get; set; }
public List<Manifest> Manifest { get; set; }
public object Origin { get; set; }
}
This is where I'm getting the error..
[JsonArray]
public class SubscriptionEvents
{
public string Name { get; set; }
public string Number { get; set; }
public SubscriptionStatus SubscriptionStatus { get; set; }
public List<SubscriptionFile> SubscriptionFile { get; set; }
}
public class QuantumViewEvents
{
public string SubscriberID { get; set; }
public SubscriptionEvents SubscriptionEvents { get; set; }
}
public class QuantumViewResponse
{
public Response Response { get; set; }
public QuantumViewEvents QuantumViewEvents { get; set; }
public string Bookmark { get; set; }
}
public class RootObject
{
public QuantumViewResponse QuantumViewResponse { get; set; }
}
}
}
The problem was with my class not having the correct properties, it also turns out that the UPS api doesn't send a static type response and it can be different each time.. requiring a new class to deserialize into. I have not found a way to make the class flexible

Trouble using all members in a class. Why can I only use the list?

I have the following class object structure:
public class StatusObj3
{
public int status { get; set; }
public DataObj3 data { get; set; }
}
public class DataObj3
{
public string survey_id { get; set; }
public string date_created { get; set; }
public string date_modified { get; set; }
public int custom_variable_count { get; set; }
public List<Custom_VariablesObj> custom_variables { get; set; }
public int language_id { get; set; }
public int num_responses { get; set; }
public int question_count { get; set; }
public string nickname { get; set; }
public TitleObj title { get; set; }
public List<PagesObj> pages { get; set; }
}
public class Custom_VariablesObj
{
public string variable_label { get; set; }
public string question_id { get; set; }
}
public class TitleObj
{
public bool enabled { get; set; }
public string text { get; set; }
}
public class PagesObj
{
string page_id { get; set; }
string heading { get; set; }
string sub_heading { get; set; }
public List<QuestionObj> questions { get; set; }
}
public class QuestionObj
{
public string question_id { get; set; }
public string heading { get; set; }
public string position { get; set; }
public QuestionTypeObj type { get; set; }
public List<AnswerObj> answers { get; set; }
}
public class QuestionTypeObj
{
public string family { get; set; }
public string subtype { get; set; }
}
public class AnswerObj
{
public string answer_id { get; set; }
public int position { get; set; }
public string text { get; set; }
public string type { get; set; }
public bool visible { get; set; }
public int weight { get; set; }
public bool apply_all_rows { get; set; }
public bool is_answer { get; set; }
public List<ItemsObj> items { get; set; }
}
public class ItemsObj
{
public string answer_id { get; set; }
public int position { get; set; }
public string type { get; set; }
public string text { get; set; }
}
I 've deserialized json into this object via:
var results_SurveyDetails = deserializer.Deserialize<StatusObj3>(json_returned);
I'm trying to loop thru the pages by:
foreach (var page in results_SurveyDetails.data.pages)
However, not all members of page are available to use.
Only the page.questions is there and not page_id, heading and subheading.
What am I doing wrong?
You are missing public on your other properties in class PagesObj.

Calling Multiple SQL Tables In The Same View

I have multiple tables I need to display in a single view. This seems to be a problem because the view only allows for one model definition from what I can tell. I've tried implementing a workaround solution, but have been unsuccessful.
Specifically, I get the error message: "The model item passed into the dictionary is of type 'System.Data.Entity.DbSet1[BillingApp.Models.HEADER_RECORD]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[BillingApp.Models.tbl1join]'."
The View
#model IEnumerable<BillingApp.Models.tbl1join>
#{
ViewBag.Title = "TABLE 01 DISPLAY";
Layout = "../Shared/Layout2.cshtml";
}
#section featured2 {
<html>
<body>
~excluding tables because there are too many fields~
</body></html>
}
class joining two of the tables (tbl1join.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace BillingApp.Models
{
public class tbl1join
{
public HEADER_RECORD HeaderRecord { get; set; }
public HEADER_EXTENSION_RECORD ExtensionRecord { get; set; }
}
}
The Model Definitions:
HEADER_RECORD.cs
namespace BillingApp.Models
{
using System;
using System.Collections.Generic;
public partial class HEADER_RECORD
{
public int HRID { get; set; }
public string TABLE_NUMBER { get; set; }
public string COMPANY { get; set; }
public string STATE_CODE { get; set; }
public string BILL_CODE { get; set; }
public string RECORD_TYPE { get; set; }
public string MASK_EXTENSION_ID { get; set; }
public string OVERPAYMENT_LIMIT { get; set; }
public string UNDERPAYMENT_LIMIT { get; set; }
public string REFUND_ACTION_OVR { get; set; }
public string REFUND_ACTION_PAR { get; set; }
public string REFUND_ACTION_RTN_PRM { get; set; }
public string REFUND_ACTION_CNC { get; set; }
public string EFT_PAC_OPTION { get; set; }
public string EFT_PAC_NOTICE { get; set; }
public string EFT_PAC_NSF_LIMIT { get; set; }
public string PREMIUM_ROUNDING { get; set; }
public string DB_CC_OPTION { get; set; }
public string NSF_CHECK_LIMIT { get; set; }
public string NSF_CHECK_OPTION { get; set; }
public string FIRST_TERM_BILLING { get; set; }
public string CARRY_DATE_OPTION { get; set; }
public string ENDORSEMENT_DAYS { get; set; }
public string DATE_METHOD { get; set; }
public string RENEWAL_OPTION { get; set; }
public string DROP_DAYS { get; set; }
public string MULTI_PAY_IND { get; set; }
public string MINIMUM_INSTALLMENT { get; set; }
public string ENDORSEMENT_ACTION { get; set; }
public string I_OR_S_OPTION_DAYS { get; set; }
public string S_OPTION_PERCENT { get; set; }
public string SERVICE_CHARGE_PREPAID { get; set; }
public string REINSTATE_OPTION { get; set; }
public string CASH_WITH_APPLICATION { get; set; }
public string DB_CC_NOTICE { get; set; }
public string DOWN_PAY_DAYS { get; set; }
public string MONTH_BY_TERM { get; set; }
public string LEAD_MONTHS { get; set; }
public string INITIAL_MONTHS { get; set; }
public string DB_CC_REJECTS { get; set; }
public string RETURN_ENDORSEMENT_OPTION { get; set; }
public string RETURN_SPLIT_OPTION_PERCENT { get; set; }
public string AUTOMATED_REFUND_DAYS { get; set; }
public string RENEWAL_OPTION_BILL_PLAN { get; set; }
public string EFFECTIVE_DATE { get; set; }
public string MISC_DATA { get; set; }
public string MISC_DATA2 { get; set; }
}
}
HEADER_EXTENSION_RECORD.cs
namespace BillingApp.Models
{
using System;
using System.Collections.Generic;
public partial class HEADER_EXTENSION_RECORD
{
public int ERID { get; set; }
public string ETABLE_NUMBER { get; set; }
public string ECOMPANY { get; set; }
public string ESTATE_CODE { get; set; }
public string EBILL_CODE { get; set; }
public string ERECORD_TYPE { get; set; }
public string EMASK_EXTENSION_ID { get; set; }
public string OVERPAYMENT_TOLERANCE_PERCENT { get; set; }
public string UNDERPAYMENT_TOLERANCE_PERCENT { get; set; }
}
}
The Controller (BillingController.cs)
public ActionResult HeaderExtensionRecord()
{
{
return View(db.HEADER_EXTENSION_RECORD);
}
}
public ActionResult HeaderRecordTable1()
{
{
return View(db.HEADER_RECORD);
}
}
Update:
Added tbl1join as the return type in the controller, but gives an error saying it's a type being used as a variable.
public ActionResult HeaderRecordTable1()
{
{
return View(IEnumerable<tbl1join>);
}
}
Wrap it into a ViewModel
public class RecordVM
{
public HEADER_RECORD header { get; set; }
public HEADER_EXTENSION_RECORD ext { get; set; }
}
return View(new RecordVM { header = db.HEADER_RECORD, ext = db.HEADER_EXTENSION_RECORD });
The error message indicates that you are passing in the wrong type, you need to query the database and create the suitable model that your view is expecting. It looks like you are trying to pass 2 models to the view using the view model tbl1join.
You could populate the view model as follows for a specified id value:
public ActionResult ViewModelExtension(int id)
{
var viewModel = new tbl1join();
viewModel.HEADER_RECORD = db.HEADER_RECORD.Where(h => h.id == id).SingleOrDefault;
viewModel.HEADER_EXTENSION_RECORD = db.HEADER_EXTENSION_RECORD.Where(h => h.id == id).SingleOrDefault;
return View(viewModel);
}
I would be surprised if there isn't some relationship between the 2 models. If this was specified within your 2 models then the above could be done differently.

JSON Web Request string to parsed data with JSON.NET

I am using .net to call to a webservice then parse it to usable data.
Right now I am experimenting with this call: http://www.reddit.com/r/all.json
Which returns: http://pastebin.com/AbV4yVuC
This is put in to a string, which I called jsontxt.
I am using JSON.NET to parse the information but it doesn't seem to be working. I initially tried to deserialize it as an object and it didn't work as nothing is put in to the variable
Then I tried to deserialize it as a dataset and I'm having no luck again. My error was
An unhandled exception of type 'Newtonsoft.Json.JsonException' occurred in Newtonsoft.Json.dll
MY CODE:
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace tutorialresult1
{
class Program
{
static void Main(string[] args)
{
using (var webClient = new System.Net.WebClient())
{
var jsontxt = webClient.DownloadString("http://www.reddit.com/r/all.json");
// Console.Write(json);
// -----Deserializing by Object--------------
//MediaEmbed account = JsonConvert.DeserializeObject<MediaEmbed>(jsontxt);
//Console.WriteLine(account.width); //COMES OUT TO NULL
// -----Deserializing by DataSet--------------
DataSet dataSet = JsonConvert.DeserializeObject<DataSet>(jsontxt);
DataTable dataTable = dataSet.Tables["Children"];
Console.WriteLine(dataTable.Rows.Count);
}
}
public class MediaEmbed
{
public string content { get; set; }
public int width { get; set; }
public bool scrolling { get; set; }
public int height { get; set; }
}
.... //rest of classes here for each json which were generated with http://json2csharp.com/
}
}
I'm just trying to make the JSON easily accessible by parsing it.
Using json2charp I generated the following set of classes. Using those you should be able to deserialize the JSON into RootObject using JSON.NET.
var account = JsonConvert.DeserializeObject<RootObject>(jsontxt);
.
public class MediaEmbed
{
public string content { get; set; }
public int? width { get; set; }
public bool? scrolling { get; set; }
public int? height { get; set; }
}
public class Oembed
{
public string provider_url { get; set; }
public string description { get; set; }
public string title { get; set; }
public string url { get; set; }
public string type { get; set; }
public string author_name { get; set; }
public int height { get; set; }
public int width { get; set; }
public string html { get; set; }
public int thumbnail_width { get; set; }
public string version { get; set; }
public string provider_name { get; set; }
public string thumbnail_url { get; set; }
public int thumbnail_height { get; set; }
public string author_url { get; set; }
}
public class SecureMedia
{
public Oembed oembed { get; set; }
public string type { get; set; }
}
public class SecureMediaEmbed
{
public string content { get; set; }
public int? width { get; set; }
public bool? scrolling { get; set; }
public int? height { get; set; }
}
public class Oembed2
{
public string provider_url { get; set; }
public string description { get; set; }
public string title { get; set; }
public int thumbnail_width { get; set; }
public int height { get; set; }
public int width { get; set; }
public string html { get; set; }
public string version { get; set; }
public string provider_name { get; set; }
public string thumbnail_url { get; set; }
public string type { get; set; }
public int thumbnail_height { get; set; }
public string url { get; set; }
public string author_name { get; set; }
public string author_url { get; set; }
}
public class Media
{
public string type { get; set; }
public Oembed2 oembed { get; set; }
}
public class Data2
{
public string domain { get; set; }
public object banned_by { get; set; }
public MediaEmbed media_embed { get; set; }
public string subreddit { get; set; }
public string selftext_html { get; set; }
public string selftext { get; set; }
public object likes { get; set; }
public SecureMedia secure_media { get; set; }
public string link_flair_text { get; set; }
public string id { get; set; }
public int gilded { get; set; }
public SecureMediaEmbed secure_media_embed { get; set; }
public bool clicked { get; set; }
public bool stickied { get; set; }
public string author { get; set; }
public Media media { get; set; }
public int score { get; set; }
public object approved_by { get; set; }
public bool over_18 { get; set; }
public bool hidden { get; set; }
public string thumbnail { get; set; }
public string subreddit_id { get; set; }
public object edited { get; set; }
public string link_flair_css_class { get; set; }
public object author_flair_css_class { get; set; }
public int downs { get; set; }
public bool saved { get; set; }
public bool is_self { get; set; }
public string permalink { get; set; }
public string name { get; set; }
public double created { get; set; }
public string url { get; set; }
public object author_flair_text { get; set; }
public string title { get; set; }
public double created_utc { get; set; }
public int ups { get; set; }
public int num_comments { get; set; }
public bool visited { get; set; }
public object num_reports { get; set; }
public object distinguished { get; set; }
}
public class Child
{
public string kind { get; set; }
public Data2 data { get; set; }
}
public class Data
{
public string modhash { get; set; }
public List<Child> children { get; set; }
public string after { get; set; }
public object before { get; set; }
}
public class RootObject
{
public string kind { get; set; }
public Data data { get; set; }
}
You are trying to deserialize a json-string into a dataset-object. But the json-string doesn't have the format of a dataset. So you need to create a class which matches the json or deserialize it into a dictionary or sth. like this.
Have you tried deserialize using the following? Seems to be more robust for me.
using System.Web.Script.Serialization;
...
var x = new JavaScriptSerializer().Deserialize<Obj_type>(jsonstring);
http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer%28v=vs.110%29.aspx
as purplej0kr mentioned you will need to have a .Net model to use for Obj_type (and it will need to match the object you are parsing) or this will not work.
You will need the System.Web.Extensions.dll referenced in your project (right click add reference, dll is usually under 'Assemblies'). Otherwise:
Where can I find the assembly System.Web.Extensions dll?

c# nested classes the type or namespace could not be found

I have a class with nested subclasses:
public class listDevicesModel
{
public int totalCount { get; set; }
public Messages messages { get; set; }
public Devices devices { get; set; }
public class Entry
{
public string key { get; set; }
public object value { get; set; }
}
public class Detail
{
public List<Entry> entry { get; set; }
}
public class Devices
{
public List<Device> device { get; set; }
}
public class Device
{
public string #id { get; set; }
public string uuid { get; set; }
public string principal { get; set; }
public int blockReason { get; set; }
public int clientId { get; set; }
public string comment { get; set; }
public int compliance { get; set; }
public int countryCode { get; set; }
public int countryId { get; set; }
public string countryName { get; set; }
public string createdAt { get; set; }
public string currentPhoneNumber { get; set; }
public List<Detail> details { get; set; }
public int deviceCount { get; set; }
public string easLastSyncAttempt { get; set; }
public string easUuid { get; set; }
public string emailAddress { get; set; }
public string emailDomain { get; set; }
public bool employeeOwned { get; set; }
public string homeOperator { get; set; }
public int languageCountryId { get; set; }
public int languageId { get; set; }
public string lastConnectedAt { get; set; }
public string manufacturer { get; set; }
public bool mdmManaged { get; set; }
public int mdmProfileUrlId { get; set; }
public string model { get; set; }
public string name { get; set; }
public bool notifyUser { get; set; }
public string #operator { get; set; }
public int operatorId { get; set; }
public string platform { get; set; }
public string platformType { get; set; }
public int quarantinedStatus { get; set; }
public int regCount { get; set; }
public string regType { get; set; }
public string registeredAt { get; set; }
public string status { get; set; }
public int statusCode { get; set; }
public string userDisplayName { get; set; }
public string userFirstName { get; set; }
public string userLastName { get; set; }
public int userSource { get; set; }
public string userUUID { get; set; }
public int wipeReason { get; set; }
}
}
In my MVC razor view i try to access the data:
#model PostenNorge.Models.JsonObjectModels.listDevicesModel
<b>#Model.messages.message</b>
<br />
<br />
<ul>
#foreach(Device d in Model.devices.device)
{
<li>#d.name - #d.uuid - #d.currentPhoneNumber</li>
}
</ul>
On Device in the foreach i get "The type or namespace name "Device" could not be found".
How can i access the nested class types in my view?
You need to specify the nested name:
#foreach(listDevicesModel.Device d in Model.devices.device)
Or use implicit typing:
#foreach(var d in Model.devices.device)
Personally I'd avoid using nested classes here anyway, unless you really have to. Even if you do really have to, I'd rename the top-level class to follow normal naming conventions, i.e. make it start with a capital letter.

Categories

Resources