I've got this task requiring building a parent/child view in html. NOT USING ANY PLUGINS!
I've created a database and a model (H_Table), and a class (Element) in controller for child nodes.
But how do I get this to work together. So, that it retrieves data from the model passes it to the class and returns to view as model. I'm not really sure whether I explaied it right. Ask away.
My Model:
namespace Tree_List.Models
{
using System;
using System.Collections.Generic;
public partial class H_Table
{
public int ID { get; set; }
public string NAME { get; set; }
public Nullable<int> PARENT_ID { get; set; }
}
}
My Controller:
using System.Collections.Generic;
using System.Web.Mvc;
using Tree_List.Models;
namespace Tree_List.Controllers
{
public class ListController : Controller
{
// GET: List
private Models.ListDBEntities1 db_connection = new Models.ListDBEntities1();
public ActionResult Index()
{
var Items = db_connection.H_Table;
return View(Items);
}
}
public partial class Element
{
public int ID { get; set; }
public string NAME { get; set; }
public List<Element> CHILDS { get; set; }
}
}
If I get your question right you want to render this hierarchical data of yours in a view. To do that you have to use Javascript and jQuery to render this data there are tons of ready libraries and open source examples on how to do this like:
https://www.codeproject.com/tips/696985/how-to-create-treeview-in-mvc
https://www.mindstick.com/Articles/1119/using-treeview-in-asp-dot-net-mvc
You can see them and write one yourself like them. Also take a look at more abstracted tool from Telerik.
Hope it helps :)
Related
Getting results back from Shopify's graphql which it not in a standard structure to be able to deserialize simply.
Here's the result, but note the list of item1, item2, etc which can be from 1 to 100 items returned and this is the part I'm not sure how to deserialize and is my main question. Specifically, to a strongly typed List<Item> collection of items such that I can they query for any UserErrors, i.e. something like: lstItems.Any(l => l.UserErrors.Any()).
The second issue is that data will not always have these contents...other GraphQL queries will have different responses. Perhaps in this case, I should rename data in the string to another class name that will have these contents, then deserialize?
{
"data":{
"item1":{
"userErrors":[
]
},
"item2":{
"userErrors":[
]
}
},
"extensions":{
...
}
}
Here's what QuickType comes up with, but again, it assumes a discrete list of Items:
namespace QuickType
{
using System;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
public partial class InventoryUpdateResult
{
[JsonProperty("data")]
public Data Data { get; set; }
[JsonProperty("extensions")]
public Extensions Extensions { get; set; }
}
public partial class Data
{
[JsonProperty("item1")]
public Item Item1 { get; set; }
[JsonProperty("item2")]
public Item Item2 { get; set; }
}
public partial class Item
{
[JsonProperty("userErrors")]
public UserError[] UserErrors { get; set; }
}
public partial class UserError
{
[JsonProperty("field")]
public string[] Field { get; set; }
[JsonProperty("message")]
public string Message { get; set; }
}
public partial class Extensions
{
....
}
}
Thanks #Charlieface for pointing me in the right direction. Here's what I ultimately did. The other part I was trying to figure out, which I didn't know and articulate at the time of writing the question, was how to separate out the data portion of the response, which had this issue from the extensions portion, which is structured properly and can be simply deserialized to a class.
So to split out the data portion, you parse the string to a JObject
Then deserialize just that portion to the Dictionary as suggested
Then you can iterate through the Dictionary to get all the standard class objects and do as you with with them (add them to their own list, etc.)
code:
var jsonObject = JObject.Parse(response);
Dictionary<string, InventoryUpdateResult> dicRawResults;
var data = (JObject)jsonObject["data"];
if (data != null)
{
dicRawResults = JsonConvert.DeserializeObject<Dictionary<string, InventoryUpdateResult>>(data.ToString());
foreach(var result in dicRawResults)
{
InventoryUpdateResult inventoryUpdateResult = result.Value;
}
}
Then I extracted the extensions portion and converted that to the class object as such:
jsonObject = JObject.Parse(shopifyResponse.Value);
var jExtension = (JObject)jsonObject["extensions"];
if (jExtension != null)
{
queryCost = jExtension.ToObject<GraphExtensions>();
.....
}
So when Working on a JSON Deserializer, I wanted to do something like this get the Values retrieved from a REST Webservice and made it into a Json Object.
So this works perfect, no troubles
So far, My the Json retrieved Looks like this :
{\"Fullname\":\"John Snow\",\"Telephone\":\"08147720192\",\"gender\":\"Male\",\"email\":\"john.snow#gmail.com\",\"date_ofbirth\":\"1985-06-22T00:00:00\",\"nationalID\":\"JS834788US\",\"accountnumber\":\"0034773291\",\"salary\":800000.00}
Now here is the worry, I would want to retrieve the values from a Deserialized Json and then Set the values, So it can be used in another application. I am building it in form of a Class Library.
Now like I said before I have been able to Make one, Which returns the Data into Json. Now i want to Deserialize and then use the values gotten from the REST api in another web application
The Code for the Deseializer looks like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Deserializer
{
public class EmpPersonalInfo
{
public string Fullname { get; set; }
public string Telephone { get; set; }
public string gender { get; set; }
public string email { get; set; }
public string date_ofbirth { get; set; }
public string nationalID { get; set; }
public string accountnumber { get; set; }
public decimal salary { get; set; }
}
public class RESTJSONDeserializer
{
public static void Deserializer(string JSON)
{
EmpPersonalInfo account = JsonConvert.DeserializeObject<EmpPersonalInfo>(JSON);
//Get and set the Values here
}
}
}
The primary challenge is I have never worked on something like this before. So for the sake of clarity, I would need some Explanation and guidance as to code something like this
Thanks
public class RESTJSONDeserializer
{
public static EmpPersonalInfo Deserializer(string JSON)
{
return JsonConvert.DeserializeObject<EmpPersonalInfo>(JSON);
//Get and set the Values here
}
}
this class should like this and when u need the deserialize object u can simply call this method
EmpPersonalInfo model= RESTJSONDeserializer.Deserializer("JsonFromApi");
My question is pretty much basic but I am not getting idea to do it. Please check the code bellow. My basic goal is to make a public class which will return some static data under a list. The example class model is provided bellow. See in class PaymentMethodDetials has two properties and I want to set value of this two property from class PaymentMethodList as a list then I will be using those list values outside this whole c# class model publically. Now my problem is paymentList.Add() visual studio not allowing me to do Add method. How can I fix that? Thanks in advance
namespace Test.Helpers
{
public class PaymentMethodList
{
List<PaymentMethodDetials> paymentList = new List<PaymentMethodDetials>();
paymentList.Add()//i want to insert data to "PaymentMethodDetials" this class like using "Add" which allowing now
}
public class PaymentMethodDetials
{
public int Id { get; set; }
public string Name { get; set; }
}
}
Try this , hope this helps
namespace Test.Helpers
{
public class PaymentMethodList
{
List<PaymentMethodDetials> paymentList = new List<PaymentMethodDetial();
public PaymentMethodList()
{
paymentList.Add(new PaymentMethodDetials
{
Id=1,
Name="xyz"
});
}
}
public class PaymentMethodDetials
{
public int Id { get; set; }
public string Name { get; set; }
}
}
I am new to ASP.net having been a PHP coder for 10 years. The MVC makes sense to be as I find it quite similar to Laravel.
I am trying to find the best solution for storing and serving meta data to my web pages in my ASP.net MVC 4 project. From what I have read I have concluded to use the following method.
Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace project.Models
{
public class Meta
{
public String Title { get; set; }
public String Description { get; set; }
public String Keywords { get; set; }
public String H1 { get; set; }
public String H2 { get; set; }
public String Robots { get; set; }
}
}
Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace project.Controllers
{
public class AboutController : BaseController
{
public ActionResult Index()
{
ViewData["metaTitle"] = "About us";
ViewData["metaDescription"] = "";
ViewData["metaKeywords"] = "";
ViewData["pageH1"] = "About";
ViewData["PageH2"] = "";
return View();
}
}
}
View
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>project | #ViewData["metaTitle"]</title>
<meta name="description" content="#ViewData["metaDescription"]">
<meta name="keywords" content="#ViewData["metaKeywords"]">
<meta name="robots" content="#ViewData["metaRobots"]">
The problem I have actually comes in one of my other views where I am trying to build a breadcrumb trail. At the moment I can get the controller/action name to appear in the breadcrumb, however I wish to access the view data for those controller/actions.
I can't find anywhere that explains how to do it/if it is possible.
Therefore;
Either I am going about storing meta data wrong, if so tell me a better way to do it.
else please outline a way to access a controllers ViewData
Please provide code samples or resources that accurately explain how to do it
Many thanks for your time.
I would do something like below,
1) Create a base class model and this base model will be derived in other models.
public class BaseModel
{
public String Title { get; set; }
public String Description { get; set; }
public String Keywords { get; set; }
public String H1 { get; set; }
public String H2 { get; set; }
public String Robots { get; set; }
}
About Model class
public class AboutModel : BaseModel
{
public string AboutDescription { get; set; }
}
2) Create a Intialize method in BaseController class which will set the properites for Title and description for your model.
public class BaseController : Controller
{
public BaseModel Initialize(BaseModel model)
{
model.Title = "Title";
model.Description = "Desc";
return model;
}
}
3) In About controller, you would need to call the initialize method like below.
public class AboutController : BaseController
{
public ActionResult Index()
{
//create instance for About Model
var model = new AboutModel();
Initialize(model);
model.AboutDescription = "About Description";
return View(model);
}
}
4) In _Layout.cshtml class you need to use the BaseModel class
#model <YourNameSpace>.BaseModel
<title>#Model.Title</title>
5) Other views ex : About.cshtml
#model <YourNameSpace>.AboutModel
I would recommend not to use ViewData for handling Common site properties like Title / Meta tags. This way your code will be much cleaner...
Since you are deriving the base class model to the other views and Initializing the Initialize method in all the ActionResults method you should get the title description properties across the pages / views.
Hey Guys I hava a question. I know questions like this are asked often, but I worked on a solution for several hours and read many answers but I couldnt find the right one. I am doing an application using ASP.NET MVC 4 Razor. I´m rather new to this system. I created a .edmx Data Model using Entity Framework 5 (Database-First Approach). This is how my auto-generated Context class looks like:
namespace KSM3.Models
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Linq;
public partial class kontrollsystemEntities : DbContext
{
public kontrollsystemEntities()
: base("name=kontrollsystemEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
[EdmFunction("kontrollsystemEntities", "udf_GetReportsByController")]
public virtual IQueryable<udf_GetReportsByController_Result> udf_GetReportsByController(string controller_account)
{
var controller_accountParameter = controller_account != null ?
new ObjectParameter("controller_account", controller_account) :
new ObjectParameter("controller_account", typeof(string));
return ((IObjectContextAdapter)this).ObjectContext.CreateQuery<udf_GetReportsByController_Result>("[kontrollsystemEntities].[udf_GetReportsByController](#controller_account)", controller_accountParameter);
}
}
}
and my Model class looks like this:
namespace KSM3.Models
{
using System;
public partial class udf_GetReportsByController_Result
{
public int ID { get; set; }
public string ProviderID { get; set; }
public int VertragID { get; set; }
public System.DateTime Leistungszeitraum_von { get; set; }
public System.DateTime Leistungszeitraum_bis { get; set; }
public string ReportklasseID { get; set; }
public int Version { get; set; }
public string Status { get; set; }
}
}
When I now click on "Add Controller" and select my classes, I get the error message:
"Unable to retrieve Metadata for KSM3.Models.udf_GetReportsByController_Result.cs"
Note: I am using Entity Framework to retrieve information from a user-defined function, not from a table! If I try the same procedure with a table, it works!
What do I have to prepare or change in order to make this work?
Thank you for all answers!
I have solved my problem, thanks!
I had to call the udf_GetReportsByController(string controller_account) method in the controller and hand the IQueryable-Result to my view.
My Controller looks like these (Note: Beginner´s mistake)
public class ReportController : Controller
{
private kontrollsystemEntities db = new kontrollsystemEntities();
//
// GET: /Report/
public ActionResult Index()
{
IQueryable<udf_GetReportsByController_Result> result = db.udf_GetReportsByController(User.Identity.Name);
return View(result.ToList());
}
}
}