Chavdar Kostov

Greenhorn
+ Follow
since Sep 21, 2018
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Chavdar Kostov

I receive a validation message for textboxfor string field before pressing submit, how to fix this?


Hi, everyone. I am working on a c# mvc application. I validate a string property as Required in a ViewModel. Here is my code from the ViewModel:



Here is the code from the View:



The problem is that when I go to the view, the required error message is shown even though I haven't pressed submit, and I want it to be displayed only if I press Submit and haven't entered anything. How should I fix that?
6 years ago

Is it possible to cast a variable of type: dictionary<int, models.user> to dictionary<int, object> in C#?

 

Hi, everyone. I am working on a c# application where I have about 15 methods that Update entries in the database. So I want to make one method for update with two parameters: Dictionary<int, object> dictionary and string tableName. The 'object' part of the dictionary will accept different ViewModels. Here is my code:



But when I tried to use the above method, I receive the red underline below the parameter "Dictionary<int, user>" and the error message is: cannot convert from 'System.Collections.Generic.Dictionary<int, Models.User> to 'System.Collections.Generic.Dictionary<int, object>.

What I have tried:

I tried the following cast:

(Dictionary<int, object>)(users)

but without success. I looked for help in google and stackoverflow but did not find a similar issue. So can you tell me is it possible to cast a variable of Type: Dictionary<int, Models.User> to Dictionary<int, object> and if the answer is yes, how to do it? Thanks.
6 years ago
Hi, everyone. I work with json object in my c# application. I deserialized the json successfully and what I want is to take only meaningful properties (properties with actual values) from the json and to use them (actually I have to update database tables using these properties).

What I have tried:

For example let's see "TicketsData" class. Here it is:

public class TicketsData
{
public int id { get; set; }
public string ticket_number { get; set; }
public int user_id { get; set; }
public int? origin_id { get; set; }
public int priority_id { get; set; }
public int sla_plan_id { get; set; }
public int status_id { get; set; }
public int? timezone_id { get; set; }
public string is_auto_assigned { get; set; }
public string created_at { get; set; }
public string updated_at { get; set; }
public int? created_by { get; set; }
public string closed_at { get; set; }
public string subject { get; set; }
public string first_assigned_agent_or_team { get; set; }
public string last_assigned_agent_or_team { get; set; }
public string service_fulfill { get; set; }
public int? no_of_workflow_positions { get; set; }
public double? invoice_amount { get; set; }
public object invoiceDetails { get; set; }
}

Part of the json dealing with "TicketsData":

ticketsData
5
updated_at "2018-09-20 14:21:40"
29 {…}

I use reflection in my code:

public void UpdateEntriesFrom_tbl_ts_TicketsData(Dictionary<int, TicketsData> ticketsData)
{
foreach (KeyValuePair<int, TicketsData> entry in ticketsData)
{
Type t = entry.Value.GetType();
PropertyInfo[] props = t.GetProperties();
Dictionary<string, object> dict = new Dictionary<string, object>();
foreach (PropertyInfo prp in props)
{
object value = prp.GetValue(entry.Value, new object[] { });
dict.Add(prp.Name, value);
}

foreach (var item in dict)
{
if (item.Value != null)
{
UpdateEntry("tbl_ts_TicketsData", item.Key, item.Value,
entry.Key);
}
}
}
}

The problems I am facing are two. First problem is that the reflection gets all properties of "TicketsData" class including the "id" and what I need is all properties without "id". The second problem is that if I use the above code, I make a check:

if (item.Value != null)
{
UpdateEntry("tbl_ts_TicketsData", item.Key, item.Value,
entry.Key);
}

but in my db table "tbl_ts_TicketsData" there are fields like "user_id" which are (int, not null) and when json is as the above snippet, "user_id" comes with default value of "0", passes the check

if (item.Value != null)

and triggers the update method, which is not what I want (I want to use only the meaningful fields from the json). I tried another approach without reflection like this:

foreach (KeyValuePair<int, TicketsData> entry in ticketsData)
{
if (entry.Value.user_id != 0)
{
UpdateEntry("tbl_ts_TicketsData", "user_id", entry.Value.user_id, entry.Key);
}
}

but the problem with that approach is thah "0" is a valid digit for "id" column of "User" table and I can miss a case if I use the code above.
Can you help me to get only the meaningful properties (properties with real not defalut values) from the deserialized json so I to be able to use them?