Calling Multiple SQL Tables In The Same View - c#

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.

Related

The class the virtual method refers to, returns null ASP.Net Core MVC

I am coming with question.
Why my class returns null value?
As You can see on this picture, class is returning null, but fields inside are not null.
This is Post model where I place virtual method to class Category, and the class is null
Why is that happening?
I checked every reference, and it's all good.
This may be caused by Windows Update or IIS/VisualStudio?
Please help
Post Model
using System;
using System.Collections.Generic;
namespace collector_forum.Data.Models
{
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public DateTime Created { get; set; }
public virtual ApplicationUser User { get; set; }
public virtual Category Category { get; set; }
public virtual IEnumerable<PostReply> Replies { get; set; }
}
}
Category Model:
using System;
using System.Collections.Generic;
namespace collector_forum.Data.Models
{
public class Category
{
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public DateTime Created { get; set; }
public string ImageUrl { get; set; }
public virtual IEnumerable<Post> Posts { get; set; }
}
}
Index and BuildHomeIndex methods from HomeController:
public IActionResult Index()
{
var model = BuildHomeIndexModel();
return View(model);
}
public HomeIndexModel BuildHomeIndexModel()
{
var latestPosts = _postService.GetLatestPosts(10);
var posts = latestPosts.Select(post => new PostListingModel
{
Id = post.Id,
Title = post.Title,
AuthorId = post.User.Id,
AuthorName = post.User.UserName,
AuthorRating = post.User.Rating,
DatePosted = post.Created.ToString(),
RepliesCount = post.Replies.Count(),
Category = GetCategoryListingForPost(post)
});
return new HomeIndexModel
{
LatestPosts = posts,
SearchQuery = ""
};
}
PostListingModel if needed:
using collector_forum.Models.Category;
namespace collector_forum.Models.Post
{
public class PostListingModel
{
public CategoryListingModel Category { get; set; }
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public string AuthorName { get; set; }
public int AuthorRating { get; set; }
public string AuthorId { get; set; }
public string DatePosted { get; set; }
public int CategoryId { get; set; }
public string CategoryName { get; set; }
public string CategoryImageUrl { get; set; }
public int RepliesCount { get; set; }
}
}
CategoryListingModel if needed too:
using collector_forum.Models.Post;
using System.Collections.Generic;
namespace collector_forum.Models.Category
{
public class CategoryListingModel
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string ImageUrl { get; set; }
public int NumberOfPosts { get; set; }
public int NumberOfUsers { get; set; }
public bool HasRecentPost { get; set; }
public PostListingModel Latest { get; set; }
}
}

c# convert nested sub classes to json

I am trying to (serialise?) a class in to JSON and am getting a null reference exception - please can you help me understand why and possible fix this?
I wrote a class which contains several nested classes (the JSON structure was provided by the UK carrier DPD).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace DPDJSONLibrary
{
public class DPD_JSON
{
// insert shipment request
/// <summary>
/// Root Object. Insert Shipment Request
/// </summary>
public class IS
{
public string job_id { get; set; }
public bool collectionOnDelivery { get; set; }
public IS_Invoice invoice { get; set; }
public string collectionDate { get; set; }
public bool consolidate { get; set; }
public IS_Consignment consignment { get; set; }
}
public class IS_Address
{
public string addressId { get; set; }
public string countryCode { get; set; }
public string countryName { get; set; }
public string county { get; set; }
public string locality { get; set; }
public string organisation { get; set; }
public string postcode { get; set; }
public string street { get; set; }
public string town { get; set; }
}
public class IS_ContactDetails
{
public string contactName { get; set; }
public string telephone { get; set; }
}
public class IS_PickupLocation
{
public IS_Address address { get; set; }
public bool allowRemotePickup { get; set; }
public string pickupLocationCode { get; set; }
}
public class IS_NotificationDetails
{
public string email { get; set; }
public string mobile { get; set; }
}
public class IS_ParcelProduct
{
public string countryOfOrigin { get; set; }
public Int32 numberOfItems { get; set; }
public string productCode { get; set; }
public string productFabricContent { get; set; }
public string productHarmonisedCode { get; set; }
public string productItemsDescription { get; set; }
public string productTypeDescription { get; set; }
public decimal unitValue { get; set; }
public decimal unitWeight { get; set; }
}
public class IS_InvoiceItem
{
public string countryOfOrigin { get; set; }
public string itemCode { get; set; }
public string itemDescription { get; set; }
public int numberOfItems { get; set; }
public decimal unitValue { get; set; }
}
public class IS_InvoiceBillingDetails
{
public IS_Address address { get; set; }
public IS_ContactDetails contactDetails { get; set; }
public string vatNumber { get; set; }
}
public class IS_Invoice
{
public string countryOfOrigin { get; set; }
public IS_InvoiceBillingDetails invoiceBillingDetails { get; set; }
public string invoiceCustomsNumber { get; set; }
public string invoiceExportReason { get; set; }
public bool invoiceIsDeclared { get; set; }
public IS_InvoiceItem invoiceItem { get; set; }
public string invoiceTermsOfDelivery { get; set; }
public int invoiceType { get; set; }
public int totalItems { get; set; }
public decimal totalValue { get; set; }
public decimal totalWeight { get; set; }
}
public class IS_DeliveryDetails
{
public IS_Address address { get; set; }
public IS_ContactDetails contactDetails { get; set; }
public IS_NotificationDetails notificationDetails { get; set; }
public IS_PickupLocation deliveryDetails { get; set; }
}
public class IS_CollectionDetails
{
public IS_Address address { get; set; }
public IS_ContactDetails contactDetails { get; set; }
}
public class IS_Parcels
{
public bool isVoided { get; set; }
public string labelNumber { get; set; }
public int packageNumber { get; set; }
public string parcelNumber { get; set; }
public IS_ParcelProduct parcelProduct { get; set; }
public string parcelnetBarcode { get; set; }
public string parcelnetData { get; set; }
public string parcelnetLabelNumber { get; set; }
}
public class IS_Consignment
{
public IS_CollectionDetails collectionDetails { get; set; }
public string consignmentNumber { get; set; }
public string consignmentRef { get; set; }
public decimal? customsValue { get; set; }
public IS_DeliveryDetails deliveryDetails { get; set; }
public string deliveryInstructions { get; set; }
public bool liability { get; set; }
public decimal liabilityValue { get; set; }
public string networkCode { get; set; }
public int numberOfParcels { get; set; }
public IS_Parcels parcel { get; set; }
public string parceldescription { get; set; }
public string shippingRef1 { get; set; }
public string shippingRef2 { get; set; }
public string shippingRef3 { get; set; }
public decimal totalWeight { get; set; }
}
}
}
I have another class containing public variables which I assign values to from code, and a method/function to instantiate my JSON class and pull in the values from the public variables.
I am getting a null reference exception on the line:
NewShipmentObject.consignment.consignmentNumber = null;
The error reads:
error System.NullReferenceException: Object reference not set to an instance of an object
The method I cam calling and getting the error in is below:
using System;
using System.Text;
using System.Net;
// include
using System.IO;
using System.Web.UI.WebControls;
using DPDJSONLibrary;
using System.Web;
using Newtonsoft.Json;
namespace DPDAPILibrary
{
public class DPD_API
{
#region public class variables
public string BusinessUnit;
public string DeliveryDirection;
public string NumberOfParcels;
public string ShipmentType;
public string TotalWeight;
public string CollectionDate;
public string ColName;
public string ColPhone;
public string ColOrganisation;
public string ColCountryCode;
public string ColPostCode;
public string ColStreet;
public string ColLocality;
public string ColTown;
public string ColCounty;
public string DelName;
public string DelPhone;
public string DelOrganisation;
public string DelCountryCode;
public string DelPostcode;
public string DelStreet;
public string DelLocality;
public string DelTown;
public string DelCounty;
public string DelNotificationEmail;
public string DelNotificationMobile;
public string NetworkCode;
public string ShippingRef1;
public string ShippingRef2;
public string ShippingRef3;
public string CustomsValue;
public string DeliveryInstructions;
public string ParcelDescription;
public string LiabilityValue;
public string Liability;
#endregion
public Boolean insertShipment(out string JSONData)
{
try
{
DPD_JSON.IS NewShipmentObject = new DPD_JSON.IS();
NewShipmentObject.job_id = null;
NewShipmentObject.collectionOnDelivery = false;
NewShipmentObject.invoice = null;
NewShipmentObject.collectionDate = CollectionDate;
NewShipmentObject.consolidate = false;
NewShipmentObject.consignment.consignmentNumber = null;
NewShipmentObject.consignment.consignmentRef = null;
NewShipmentObject.consignment.parcel = null;
NewShipmentObject.consignment.collectionDetails.contactDetails.contactName = ColName;
NewShipmentObject.consignment.collectionDetails.contactDetails.telephone = ColPhone;
NewShipmentObject.consignment.collectionDetails.address.organisation = ColOrganisation;
NewShipmentObject.consignment.collectionDetails.address.countryCode = ColCountryCode;
NewShipmentObject.consignment.collectionDetails.address.postcode = ColPostCode;
NewShipmentObject.consignment.collectionDetails.address.street = ColStreet;
NewShipmentObject.consignment.collectionDetails.address.locality = ColLocality;
NewShipmentObject.consignment.collectionDetails.address.town = ColTown;
NewShipmentObject.consignment.collectionDetails.address.county = ColCounty;
NewShipmentObject.consignment.deliveryDetails.contactDetails.contactName = ColName;
NewShipmentObject.consignment.deliveryDetails.contactDetails.telephone = DelPhone;
NewShipmentObject.consignment.deliveryDetails.address.organisation = DelOrganisation;
NewShipmentObject.consignment.deliveryDetails.address.countryCode = DelCountryCode;
NewShipmentObject.consignment.deliveryDetails.address.postcode = DelPostcode;
NewShipmentObject.consignment.deliveryDetails.address.street = DelStreet;
NewShipmentObject.consignment.deliveryDetails.address.locality = DelLocality;
NewShipmentObject.consignment.deliveryDetails.address.town = DelTown;
NewShipmentObject.consignment.deliveryDetails.address.county = DelCounty;
NewShipmentObject.consignment.deliveryDetails.notificationDetails.email = DelNotificationEmail;
NewShipmentObject.consignment.deliveryDetails.notificationDetails.mobile = DelNotificationMobile;
// default output value
JSONData = "";
JSONData = Convert.ToString(JsonConvert.SerializeObject(NewShipmentObject));
}
catch (Exception ex)
{
JSONData = Convert.ToString(ex);
return false;
}
}
}
}
You'll need to initialize all objects (including child entities) before you can use them*, as they will otherwise adopt their default values (which will be null for objects). i.e. replace
NewShipmentObject.consignment.consignmentNumber = null;
with:
NewShipmentObject.consignment = new IS_Consignment();
NewShipmentObject.consignment.consignmentNumber = null;
You can save yourself a lot of typing - instead of line-by-line setting of each field, you can do so using Object Initializer Syntax:
var NewShipmentObject = new DPD_JSON.IS
{
job_id = null,
collectionOnDelivery = false,
consignment = new IS_Consignment
{
consignmentNumber = null,
... more consignment settings here
}
... etc.
It should be noted that you don't need to explicitly set uninitialized variables to their default value, i.e. both
job_id = null,
collectionOnDelivery = false
are redundant, as these should be the default values in any event.
* (unless this initialization is done automatically in the constructor)

JsonConvert.DeserializeObject not working sometimes

I'm trying to Deserialize some json using JsonConver.DeserializeObject. however it's not working on some json from the api I'm using. here is a link that does not work: https://api.pokemontcg.io/v1/cards?setCode=smp
and here is a link that does work. https://api.pokemontcg.io/v1/cards?setCode=sm2
I'm not sure why it sometimes works and sometimes not. The data that comes out of it is very similar to each other, just different cards.
Here is the code:
public static async Task<T> GetDataAsync<T>(this HttpClient client, string address, string querystring)
where T : class
{
var uri = address;
if (!string.IsNullOrEmpty(querystring))
{
uri += querystring;
}
var httpMessage = await client.GetStringAsync(uri);
var jsonObject = JsonConvert.DeserializeObject<T>(httpMessage);
return jsonObject;
}
Now my card class
namespace CardAppReal.Lib.Models
{
public class Card
{
public string id { get; set; }
public string name { get; set; }
public string imageUrl { get; set; }
public string imageUrlHiRes { get; set; }
public string supertype { get; set; } // if pokemon, trainer or energy
public string setcode { get; set; }
public int number { get; set; }
public string set { get; set; }
}
}
With a root
using System.Collections.Generic;
using CardAppReal.Lib.Models;
namespace CardAppReal.Lib.Entities
{
public class RootCard
{
public List<Card> cards { get; set; }
}
}
If u could help me out I would find it amazing.
I have tested your json.
this is the Model
namespace Test
{
public class Attack
{
public List<string> cost { get; set; }
public string name { get; set; }
public string text { get; set; }
public string damage { get; set; }
public int convertedEnergyCost { get; set; }
}
public class Weakness
{
public string type { get; set; }
public string value { get; set; }
}
public class Resistance
{
public string type { get; set; }
public string value { get; set; }
}
public class Ability
{
public string name { get; set; }
public string text { get; set; }
public string type { get; set; }
}
public class Card
{
public string id { get; set; }
public string name { get; set; }
public int nationalPokedexNumber { get; set; }
public string imageUrl { get; set; }
public string imageUrlHiRes { get; set; }
public string subtype { get; set; }
public string supertype { get; set; }
public string hp { get; set; }
public List<string> retreatCost { get; set; }
public string number { get; set; }
public string artist { get; set; }
public string rarity { get; set; }
public string series { get; set; }
public string set { get; set; }
public string setCode { get; set; }
public List<string> types { get; set; }
public List<Attack> attacks { get; set; }
public List<Weakness> weaknesses { get; set; }
public List<Resistance> resistances { get; set; }
public string evolvesFrom { get; set; }
public Ability ability { get; set; }
public List<string> text { get; set; }
}
public class RootObject
{
public List<Card> cards { get; set; }
}
}
And this is how I call the Deserialize
RootObject root = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(RootObject.WRONG_JSON);
(NB WRONG_JSON is your json string...)

Multiple Tables in one View

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?

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?

Categories

Resources