I am trying to delete record from the database using modals, jQuery and javascript but when popup shows I am getting undefined for Gender. How can I represent the value of my enum into a new <td>?
var deleteBtn = null;
function deleteLeague(leagueId, btn) {
deleteBtn = btn;
$.ajax({
type: "POST",
url: "#Url.Action("DeleteLeague", "League")",
data: {
id: leagueId
},
success: function(data) {
$("#leagueTBody").html("");
$("#leagueId").val(leagueId);
$("#leagueTBody").append("<tr><td>" + data.id + "</td>" +
"<td>" + data.leagueType + "</td>" +
" <td> " + data.season.name + "</td>" + " <td> " + data.gender + "</td>" + " <td> " +
data.note + "</td>" +
"</tr >");
},
error: function(req, status, error) {
ajaxErrorHandlingAlert("error", req.status);
}
});
}
Enum:
public enum Gender
{
[Display(Name = "Машки")] Male,
[Display(Name = "Женски")] Female
}
It should be Male or Female in the Gender column.
I'm using OCR API to capture text in an image. I want to deserialize the JSON response and extract each word from each line. No words are showing up when I run JsonConvert.DeserializeObject
Any help is appreciated!
Here's the response:
{
"language": "en",
"textAngle": -2.0000000000000338,
"orientation": "Up",
"regions": [
{
"boundingBox": "462,379,497,258",
"lines": [
{
"boundingBox": "462,379,497,74",
"words": [
{
"boundingBox": "462,379,41,73",
"text": "A"
},
{
"boundingBox": "523,379,153,73",
"text": "GOAL"
},
{
"boundingBox": "694,379,265,74",
"text": "WITHOUT"
}
]
},
{
"boundingBox": "565,471,289,74",
"words": [
{
"boundingBox": "565,471,41,73",
"text": "A"
},
{
"boundingBox": "626,471,150,73",
"text": "PLAN"
},
{
"boundingBox": "801,472,53,73",
"text": "IS"
}
]
},
{
"boundingBox": "519,563,375,74",
"words": [
{
"boundingBox": "519,563,149,74",
"text": "JUST"
},
{
"boundingBox": "683,564,41,72",
"text": "A"
},
{
"boundingBox": "741,564,153,73",
"text": "WISH"
}
]
}
]
}
]
}
There is a NuGet package already available for the Microsoft Azure OCR :
Install-Package Microsoft.Azure.CognitiveServices.Vision.ComputerVision
You can then include the OcrResult model class provided by the package:
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models;
Deserialize the JSON response to the model type and extract the text in each OcrLine object.
OcrResult ocrResult = JsonConvert.DeserializeObject<OcrResult>(JSONResult);
StringBuilder sb = new StringBuilder();
if (!ocrResult.Language.Equals("unk"))
{
foreach (OcrLine ocrLine in ocrResult.Regions[0].Lines)
{
foreach (OcrWord ocrWord in ocrLine.Words)
{
sb.Append(ocrWord.Text);
sb.Append(' ');
}
sb.AppendLine();
}
}
The example looks like
using System;
using Newtonsoft.Json;
namespace Rextester
{
public class Rootobject
{
public string language { get; set; }
public float textAngle { get; set; }
public string orientation { get; set; }
public Region[] regions { get; set; }
}
public class Region
{
public string boundingBox { get; set; }
public Line[] lines { get; set; }
}
public class Line
{
public string boundingBox { get; set; }
public Word[] words { get; set; }
}
public class Word
{
public string boundingBox { get; set; }
public string text { get; set; }
}
public class Program
{
public static void Main(string[] args)
{
string val = "{\n" +
" \"language\": \"en\",\n" +
" \"textAngle\": -2.0000000000000338,\n" +
" \"orientation\": \"Up\",\n" +
" \"regions\": [\n" +
" {\n" +
" \"boundingBox\": \"462,379,497,258\",\n" +
" \"lines\": [\n" +
" {\n" +
" \"boundingBox\": \"462,379,497,74\",\n" +
" \"words\": [\n" +
" {\n" +
" \"boundingBox\": \"462,379,41,73\",\n" +
" \"text\": \"A\"\n" +
" },\n" +
" {\n" +
" \"boundingBox\": \"523,379,153,73\",\n" +
" \"text\": \"GOAL\"\n" +
" },\n" +
" {\n" +
" \"boundingBox\": \"694,379,265,74\",\n" +
" \"text\": \"WITHOUT\"\n" +
" }\n" +
" ]\n" +
" },\n" +
" {\n" +
" \"boundingBox\": \"565,471,289,74\",\n" +
" \"words\": [\n" +
" {\n" +
" \"boundingBox\": \"565,471,41,73\",\n" +
" \"text\": \"A\"\n" +
" },\n" +
" {\n" +
" \"boundingBox\": \"626,471,150,73\",\n" +
" \"text\": \"PLAN\"\n" +
" },\n" +
" {\n" +
" \"boundingBox\": \"801,472,53,73\",\n" +
" \"text\": \"IS\"\n" +
" }\n" +
" ]\n" +
" },\n" +
" {\n" +
" \"boundingBox\": \"519,563,375,74\",\n" +
" \"words\": [\n" +
" {\n" +
" \"boundingBox\": \"519,563,149,74\",\n" +
" \"text\": \"JUST\"\n" +
" },\n" +
" {\n" +
" \"boundingBox\": \"683,564,41,72\",\n" +
" \"text\": \"A\"\n" +
" },\n" +
" {\n" +
" \"boundingBox\": \"741,564,153,73\",\n" +
" \"text\": \"WISH\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" ]\n" +
" }\n" +
" ]\n" +
"}";
Rootobject r = JsonConvert.DeserializeObject<Rootobject>(val);
}
}
}
This is posted at Rextester
I have JSON data that I need to access from C#. The chapters element looks like this:
"chapters": [
[
2,
1416420134.0,
"2",
"546cdb2645b9efbff4582d51"
],
[
1,
1411055241.0,
null,
"541afe8945b9ef69885d3d74"
],
[
0,
1414210972.0,
"0",
"544b259c45b9efb061521235"
]
]
Here are my C# classes that are meant to contain that data:
public class test
{
public string[] chapters { get; set; }
}
public class TChapter
{
public test[] aa { get; set; }
}
How can I parse the JSON to C# objects?
Using Newtonsoft JSON you will want to do something like the following
using System;
using Newtonsoft.Json;
namespace JsonDeserializationTest
{
class Program
{
static void Main(string[] args)
{
var chaptersAsJson = "[" +
" [" +
" 2," +
" 1416420134.0," +
" \"2\"," +
" \"546cdb2645b9efbff4582d51\"" +
" ], " +
" [" +
" 1," +
" 1411055241.0," +
" null," +
" \"541afe8945b9ef69885d3d74\"" +
" ], " +
" [" +
" 0," +
" 1414210972.0," +
" \"0\"," +
" \"544b259c45b9efb061521235\"" +
" ]" +
"]";
var chaptersAsTwoDObjectArray = JsonConvert.DeserializeObject<object[][]>(chaptersAsJson);
// Use the chapters array
foreach (object[] chapter in chaptersAsTwoDObjectArray)
{
// what do you want to do with the object array?
Console.WriteLine(String.Join(", ", chapter));
}
Console.WriteLine("Finished.");
}
}
}
Note that your classes don't line up with your JSON.
How to save Data In Database to the controller.i am using jquery to get data from the textbox. please help me.
Contactcontroller.cs
public ActionResult Create(string Name, string Company, string
Regarding, string Email, string Phone, string Message) {
string body = "Name: " + Name + "<br>" + "\nCompany: " + Company + "<br>" + "Regarding: " + Regarding + "<br>" + "Email: " +
Email + "<br>" + "Phone: " + Phone + "<br>" + "Message: " + Message;
try
{
// code
}
catch
{
}
jquery.js
$("#btnSubmit").click(function (event) {
var data = { Name: $("#txtbxName").val(), Company: $("#txtbxCompany").val(), Regarding:
$("#ddlRegarding").val(), Email: $("#txtbxEmail").val(), Phone: $("#txtbxPhone").val(), Message:
$("#txtarMessage").val()
}
$.ajax({
type: "POST",
url: "/Contact/Create", // the method we are calling
contentType: "application/json; charset=utf-8",
data: JSON.stringify(data),
dataType: "html",
success: function (result) {
$("#txtarMessage").val("");
alert(result);
// Or if you are returning something
alert('I returned... ' + result.WhateverIsReturning);
},
error: function (result) {
alert('Thanks for sending info');
location.reload();
return false;
}
});
});
i am getting data to the textbox using jquery.
now i want to save the whole data in the database through Controller.
Jquery
$.post('/Controller/Create', { Name: $('#Name').val(), Company: "Company", Regarding: "", Email: "",Phone: "",Message: ""}, function (data) {
});
Controller Action
public JsonResult Create(string Name, string Company, string Regarding, string Email, string Phone, string Message)
{
string body = "Name: " + Name + "<br>" + "\nCompany: " + Company + "<br>" + "Regarding: " + Regarding + "<br>" + "Email: " +
Email + "<br>" + "Phone: " + Phone + "<br>" + "Message: " + Message;
return Json(body, JsonRequestBehavior.AllowGet);
}
if (questions[0]) {
$("select[id$=ddlPollQuestions] > option").remove();
$('#ddlPollQuestions').append('<option value="">Choose a question to compare to</option>');
$.each(questions, function(i, question) {
$('#ddlPollQuestions').append('<option value="' + question.QUESTIONID + '">' + question.TEXT + '</option>');
});
} else {
$("select[id$=ddlPollQuestions] > option").remove();
$('#ddlPollQuestions').append('<option value="' + 0 + '">' + 'There are no questions of this type' + '</option>');
}
What this does is it removes all the previous values, But i want my first option, which is "Choose a question ..." to remain, and then display "There are not questions..." as my 2nd option. My code here does not show "Choose a question.." as the first option. Thanks for having a look!
An easier approach:
$('#ddlPollQuestions').children('option:not(:first)').remove();
Use :gt selector.
Try this(Note that I have added a string variable to append the options after the each loop for better performance):
if (questions[0]) {
$("select[id$=ddlPollQuestions] > option:gt(0)").remove();
$('#ddlPollQuestions').append('<option value="">Choose a question to compare to</option>');
var options = "";
$.each(questions, function(i, question) {
options += '<option value="' + question.QUESTIONID + '">' + question.TEXT + '</option>';
});
$('#ddlPollQuestions').append(options);
} else {
$("select[id$=ddlPollQuestions] > option:gt(0)").remove();
$('#ddlPollQuestions').append('<option value="' + 0 + '">' + 'There are no questions of this type' + '</option>');
}
since you are removing all the options in your code, you'll might just want to append again the Choose a question... in your else statement
if (questions[0]) {
$("select[id$=ddlPollQuestions] > option").remove();
$('#ddlPollQuestions').append('<option value="">Choose a question to compare to</option>');
$.each(questions, function(i, question) {
$('#ddlPollQuestions').append('<option value="' + question.QUESTIONID + '">' + question.TEXT + '</option>');
});
} else {
$("select[id$=ddlPollQuestions] > option").remove();
$('#ddlPollQuestions').append('<option value="">Choose a question to compare to</option>');
$('#ddlPollQuestions').append('<option value="' + 0 + '">' + 'There are no questions of this type' + '</option>');
}
example: http://jsfiddle.net/xeYwv/2/
var $PollQuestions = $('#ddlPollQuestions');
if (questions[0]) {
$PollQuestions.children().remove();
$PollQuestions.append('<option value="0">Choose a question to compare to</option>');
$.each(questions, function(i, question) {
$PollQuestions.append('<option value="' + question.QUESTIONID + '">' + question.TEXT + '</option>');
});
} else {
$PollQuestions.children().remove();
$PollQuestions.append('<option value="' + 0 + '">' + 'There are no questions of this type' + '</option>');
}
what you had is already pretty close.