SingalR Mapping json object to c# object - asp.net-mvc

Does SignlaR automatically map json object sent from to client to c# object? if so, what could i be doing wrong here?
C# Object
public class ChatHub :Hub
{
public void broadcastMessage(CommentModel model)
{
string test = model.Comment;
// Clients.All.writeMessage(jsonString);
}
public class CommentModel
{
[Required]
public string Name { get; set; }
[Required]
public string Comment { get; set; }
[Required]
public string EmailAddress { get; set; }
}
}
JavaScript
$(document).ready(function () {
var chat = $.connection.chatHub;
chat.client.writeMessage = function (t) {
var name = t.Name;
var email = t.Email;
var id = t.id;
var text = name + " " + email + " " + id + " ";
$("#test").append(text);
}
$("form").submit(function (e) {
var jsonObject = JSON.stringify($(this).serializeObject());
chat.server.broadcastMessage(jsonObject);
e.preventDefault();
});
$.connection.hub.start();
});
$.fn.serializeObject = function () {
var o = {};
var a = this.serializeArray();
$.each(a, function () {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};

You seem to be sending a Json string to the app server whereas the server is expecting an object.
Change:
var jsonObject = JSON.stringify($(this).serializeObject());
To:
var jsonObject = $(this).serializeObject();

Related

How do I programmatically add records to an Umbraco v8 form?

I'm looking to add records to an Umbraco v8 form. I know I need the form guid. Is this how I'd do it? Something like this?
public void PostFormData()
{
Guid FormGuid = new Guid("8494a8f0-94da-490e-bd61-7e658c226142");
var form = _formService.Get(FormGuid);
//place for field data into fieldDic
var fieldDic = new Dictionary<Guid, RecordField>();
var firstName = form.AllFields.First(f => f.Alias == "firstName");
var firstNameRecord = new RecordField(firstName);
firstNameRecord.Values = new List<object>() { "Mad Max" };
fieldDic.Add(firstName.Id, firstNameRecord);
var record = new Record()
{
Created = DateTime.Now,
Form = form.Id,
RecordFields = fieldDic,
State = FormState.Submitted,
};
record.RecordData = record.GenerateRecordDataAsJson();
_recordStorage.InsertRecord(record, form);
}
Here's how I do it. Note, I'm hard-coding the Record.UmbracoPageId to -1 while you might want to actually pass in the correct page ID.
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Umbraco.Core;
using Umbraco.Core.Composing;
using Umbraco.Core.Logging;
using Umbraco.Forms.Core.Data.Storage;
using Umbraco.Forms.Core.Models;
using Umbraco.Forms.Core.Persistence.Dtos;
using Umbraco.Forms.Core.Services;
namespace myProject.Services
{
public class FormServiceComposer : IUserComposer
{
public void Compose(Composition composition)
{
composition.Register<IFormService, FormService>(Lifetime.Request);
}
}
public interface IFormService
{
void InsertFormData(Guid formGuid, object formModel, string ipAddress);
}
public class FormService : IFormService
{
private readonly ILogger _logger;
private readonly Umbraco.Forms.Core.Services.IFormService _formService;
private readonly IRecordStorage _recordStorage;
private readonly IRecordFieldStorage _recordFieldStorage;
private readonly IWorkflowService _workflowService;
public FormService(ILogger logger, Umbraco.Forms.Core.Services.IFormService formService, IRecordStorage recordStorage, IRecordFieldStorage recordFieldStorage, IWorkflowService workflowService)
{
_logger = logger;
_formService = formService;
_recordStorage = recordStorage;
_recordFieldStorage = recordFieldStorage;
_workflowService = workflowService;
}
#region IFormService
public void InsertFormData(Guid formGuid, object formModel, string ipAddress)
{
try
{
Form form = _formService.GetForm(formGuid);
Record record = new Record();
foreach (Field field in form.AllFields)
{
string caption = CleanCaption(field.Caption);
if (formModel.GetType().GetProperty(caption) == null) continue;
var propertyValue = formModel.GetType().GetProperty(caption).GetValue(formModel, null);
if (propertyValue != null)
{
List<object> values = ExtractValues(propertyValue);
RecordField recordField = new RecordField
{
Alias = field.Alias,
FieldId = field.Id,
Field = field,
Key = Guid.NewGuid(),
Record = record.Id,
Values = values
};
_recordFieldStorage.InsertRecordField(recordField);
record.RecordFields.Add(recordField.Key, recordField);
}
}
record.Form = formGuid;
record.IP = ipAddress;
record.UmbracoPageId = -1;
record.State = Umbraco.Forms.Core.Enums.FormState.Approved;
record.RecordData = record.GenerateRecordDataAsJson();
_recordStorage.InsertRecord(record, form);
_recordStorage.DisposeIfDisposable();
}
catch (Exception ex)
{
_logger.Error<FormService>(ex, "Failed inserting Umbraco Forms data for {formGuid}");
}
}
#endregion IFormService
#region Private
private string CleanCaption(string caption)
{
Regex rgx = new Regex("[^a-zA-Z0-9 -]");
return rgx.Replace(caption.Trim().Replace(" ", ""), "");
}
private List<object> ExtractValues(object propertyValue)
{
List<object> result = new List<object>();
if (propertyValue is string == false && propertyValue.GetType().GetGenericTypeDefinition() == typeof(List<>))
{
IEnumerable<object> _propertyValue = (IEnumerable<object>)propertyValue;
if (_propertyValue.Any())
{
if (_propertyValue.First().GetType().GetProperties().Count() > 1)
{
JArray _properties = JArray.Parse(JsonConvert.SerializeObject(propertyValue));
foreach (JToken item in _properties)
{
string _value = string.Empty;
foreach (var _property in _propertyValue.First().GetType().GetProperties())
{
string _key = _property.Name;
_value = _value + (_value == "" ? "" : " - ") + item[_key].ToString();
}
result.Add(_value);
}
}
else
{
string _key = _propertyValue.First().GetType().GetProperties().First().Name;
JArray _properties = JArray.Parse(JsonConvert.SerializeObject(propertyValue));
foreach (JToken item in _properties)
{
result.Add(item[_key].ToString());
}
}
}
}
else
{
result.Add(propertyValue);
}
return result;
}
#endregion Private
}
}

Add custom field to contact in Xamarin Android

I'd like to add an custom field to contacts for permission management in my app.
f.e. Contact => Name, Number, Permission ("allowed"/"denied")
In my App I have a part which can receive commands by SMS.
Now I want to add a "Permission Management".
So that SMS from users, which are not allowed to send commands, not would be processed.
The "Permission Management" I'd like to realize with a custom field in the contactlist.
f.e. Contact => Name, Number, Permission ("allowed"/"denied")
Who can I add (programmatically) a custom filed to a contact?
(look at method "AddFieldtoContact" in ContactHelper.cs)
I found a solution where a custom mime type must be added: How to add new field(s) to the contact?
How can I add a custom mime type to add it in a contact?
-----------------------------------------------------------------------------
File StatusInAppPermission.cs
namespace Appname.Models
{
public class StatusInAppPermission
{
public const string Allowed = "allowed";
public const string Denied = "denied";
public const string NotSet = "notset";
public string Status = "";
public StatusInAppPermission(string sta)
{
switch (sta)
{
case Allowed:
Status = Allowed;
break;
case Denied:
Status = Denied;
break;
case NotSet:
Status = NotSet;
break;
default:
Status = NotSet;
break;
}
}
}
}
-----------------------------------------------------------------------------
File Contact.cs
namespace Appname.Models
{
public class Contact
{
public int Id { get; set; }
public string DisplayName { get; set; }
public string Number { get; set; }
public StatusInAppPermission StatusPermission { get; set; }
}
}
-----------------------------------------------------------------------------
File ContactHelper.cs
using Android.Provider;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Appname.Interfaces;
using Appname.Models;
using Xamarin.Forms;
[assembly: Xamarin.Forms.Dependency(typeof(Appname.Droid.ContactHelper))]
namespace Appname.Droid
{
class ContactHelper : IContactHelper
{
public async Task<List<Contact>> GetDeviceContactsAsync()
{
Contact selectedContact = new Contact();
List<Contact> contactList = new List<Contact>();
var uri = ContactsContract.CommonDataKinds.Phone.ContentUri;
//TODO: Add element for custom field "AppnamePermission"
string[] projection = { ContactsContract.Contacts.InterfaceConsts.Id, ContactsContract.Contacts.InterfaceConsts.DisplayName, ContactsContract.CommonDataKinds.Phone.Number /*, custom field "AppnamePermission" */ };
var cursor = Forms.Context.ContentResolver.Query(
uri,
projection,
null,
null,
ContactsContract.ContactsColumns.DisplayName + " ASC");
if (cursor.MoveToFirst())
{
do
{
// Get StatusInAppPermission from contact if saved
string status = "";
try
{
//TODO: Get custom field "AppnamePermission" value if exists
status = cursor.GetString(cursor.GetColumnIndex(projection[3]));
}
catch (Exception ex) { }
// Create new contact-object and add it to a List
contactList.Add(new Contact()
{
Id = cursor.GetInt(cursor.GetColumnIndex(projection[0]))
,DisplayName = cursor.GetString(cursor.GetColumnIndex(projection[1]))
,Number = cursor.GetString(cursor.GetColumnIndex(projection[2])).Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", "")
,StatusPermission = new StatusInAppPermission(status)
});
} while (cursor.MoveToNext());
}
return contactList;
}
public async Task<Contact> GetDeviceContactByNumber(string Number)
{
Contact selectedContact = new Contact();
List<Contact> contactList = new List<Contact>();
contactList = await GetDeviceContactsAsync();
foreach (Contact c in contactList)
{
if (c.Number.Equals(Number))
{
selectedContact = c;
}
}
return selectedContact;
}
public void AddFieldtoContact(Contact contact, string fieldname, string fieldvalue)
{
int ContactId = contact.Id;
// f.i. add fieldvalue by ContactId and fieldname
// fieldname => mimetype
// replace "clsContacts.FORMALITY_MIMETYPE" with mimetype
// the following code is adapted from: https://stackoverflow.com/questions/2733589/how-to-add-new-fields-to-the-contact
try
{
ContentValues values = new ContentValues();
int mod = Forms.Context.ContentResolver.Update(
uri
,values
,Data.InterfaceConsts.RawContactId + "=" + ContactId + " AND "
+ Data.InterfaceConsts.Mimetype + "= '"
+ clsContacts.FORMALITY_MIMETYPE + "'", null);
if (mod == 0)
{
values.Put(Data.InterfaceConsts.RawContactId, ContactId);
values.Put(Data.InterfaceConsts.Mimetype, clsContacts.FORMALITY_MIMETYPE);
Forms.Context.ContentResolver.Insert(uri, values);
}
}
catch (Exception e)
{
}
}
public string GetFieldFromContact(Contact contact, string fieldname)
{
int ContactId = contact.Id;
string fieldvalue = "";
// f.i. get fieldvalue by ContactId and fieldname
return fieldvalue;
}
}
}

How to send message/notification to browser/client in Kendo Mvc Ajax Bound Grid (confirm update or create) ( by cookies)

how to send notification to client in kendo mvc ajax bound ,
i do not know if this is good way ..
of course you can add ModelState.AddModelError("notification","….msg.")
and …. But it tell to datasource ajax action failed.. So..
Addclass:
public class AjaxErrorMsg
{
public AjaxErrorMsg()
{
CssClass = "gray";
AutoHide = false;
AutoHideDelay = 200;
Kind = "error";
Title = "error";
}
public string Msg { get; set; }
public string Kind { get; set; }
public Boolean AutoHide { get; set; }
public Int32 AutoHideDelay { get; set; }
public string CssClass { get; set; }
public string Title { get; set; }
}
In controller
public void StoreMessageInCookie(string msg, Boolean isError = false, string msgDivClass = "", string preMessage = "", string msgTitle = "")
{
StoreMessageInCookie(new Exception(msg), isError, msgDivClass, preMessage, msgTitle);
}
public void StoreMessageInCookie(Exception ee, Boolean isError = false, string msgDivClass = "", string preMessage = "", string msgTitle = "")
{
List<AjaxErrorMsg> rs;
var js = new JavaScriptSerializer();
if (this.Response.Cookies[Common1.MsgCookie].HasKeys )
{
try
{
rs = js.Deserialize<List<AjaxErrorMsg>>(this.Response.Cookies[Common1.MsgCookie].Value);
}
catch
{
rs = new List<AjaxErrorMsg>();
}
}
else
{
rs = new List<AjaxErrorMsg>();
}
var msg0 = new AjaxErrorMsg();
if (isError)
{
msgDivClass = msgDivClass == "" ? "DivAlarmStyle6 fontMitra" : msgDivClass;
msg0.Kind = "error";
msg0.AutoHide = false;
msg0.Title = "Error";
}
else
{
msg0.Kind = "alert";
msg0.AutoHide = true;
msg0.Title = "Warning";
}
msg0.Title = msgTitle == "" ? msg0.Title : msgTitle;
msg0.Msg = preMessage + cm2.FilterMessageText(ee, IsAdminUser);
if (msgDivClass.Trim() != "")
msg0.Msg = Common1.WrapTextInDivWithClass(msg0.Msg, msgDivClass);
rs.Add(msg0);
this.Response.Cookies.Add(new HttpCookie(Common1.MsgCookie, js.Serialize(rs)));
}
cm2.FilterMessageText is function to remove database object name from error message string ..also include any inner exception message
Then when you what to send message use command
StoreMessageInCookie("Message 1 2 ! ");
And in your MainProject.js
Add all of code below
function createCookie(name, value, days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
}
else expires = "";
document.cookie = name + "=" + value + expires + "; path=/";
}
function eraseCookie(name) {
createCookie(name, "", -1);
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ')
c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0)
return c.substring(nameEQ.length, c.length);
}
return null;
}
function ReadAnShowCookieForDeliverMessages() {
try {
var listMsg = readCookie("CookieForDeliverMessages")
if (!!listMsg) {
eraseCookie("CookieForDeliverMessages");
ShowAndLogMessageListOfAjaxError($.parseJSON(listMsg));
}
} catch (ee) {
alert(" error in ReadAnShowCookieForDeliverMessages")
}
}
function ShowAndLogMessageListOfAjaxError(ListOfAjaxError) {
if (ListOfAjaxError == undefined)
return;
ListOfAjaxError.forEach(function (AjaxError) {
ShowAndLogMessageByAjaxErrorClass(AjaxError)
})
}
function ShowAndLogMessageByAjaxErrorClass(AjaxError) {
if ((AjaxError != undefined) && (AjaxError.Msg != "")) {
// I used freeow => https://github.com/pjdietz/Freeow
$("#freeow").freeow(AjaxError.Title, AjaxError.Msg, { classes: [AjaxError.CssClass, AjaxError.Kind], autoHide: AjaxError.AutoHide, autoHideDelay: AjaxError.AutoHideDelay });
// LogMsg(AjaxError.Msg, AjaxError.Title); function to log message on client
}
}
$(function () {
MyAjaxSetting();
ReadAnShowCookieForDeliverMessages();
}
function MyAjaxSetting() {
$(document).ajaxComplete(function (o) {
ReadAnShowCookieForDeliverMessages()
});
}

Json to List<object> not binding

I have a problem with object send in json format, that is not binded in Action Controller
Here the json parts:
function filterTxAjustement(arrTx) {
return {
dateDebut: $("#TxAjustementDateDebut").val(),
dateFin: $("#TxAjustementDateFin").val(),
ufCode: $("#TxAjustementUf").val(),
tx: JSON.stringify(arrTx)
};
}
function SaveTx() {
var arrTx = [];
$.each(Day, function (i, v) {
var $this = $('#' + v + 'Tx');
var tx = new Object();
tx.Day = v;
tx.Nb = parseInt($this.val());
tx.UfCode = parseInt($('#TxAjustementUf').val());
tx.Total = 0;
arrTx.push(tx);
});
$.ajax({
url: EasilyRelativeUrl("Lit/SetTxAjustementByDateAndUf"),
data: filterTxAjustement(arrTx),
type: 'POST',
dataType: 'json'
}).done(function (data) {
$('#TxAjustementGrid').data("kendoGrid").dataSource.read();
});
}
My array is well populated:
dateDebut:16/12/2013
dateFin:22/12/2013
ufCode:21124
tx:[{"Day":"Sunday","Nb":1,"UfCode":21124,"Total":0},{"Day":"Monday","Nb":2,"UfCode":21124,"Total":0},{"Day":"Tuesday","Nb":3,"UfCode":21124,"Total":0},{"Day":"Wednesday","Nb":0,"UfCode":21124,"Total":0},{"Day":"Thursday","Nb":0,"UfCode":21124,"Total":0},{"Day":"Friday","Nb":0,"UfCode":21124,"Total":0},{"Day":"Saturday","Nb":0,"UfCode":21124,"Total":0}]
But when it hit Action Controller, tx is null
public ActionResult SetTxAjustementByDateAndUf(DateTime dateDebut, DateTime dateFin, string ufCode, List<TauxAjustement> tx)
(Here the TauxAjustement object)
public class TauxAjustement
{
public string Day { get; set; }
public int Nb { get; set; }
public int Total { get; set; }
public int UfCode { get; set; }
}
I have tried with TauxAjustement[], but same issue. I have added Total = 0 and parseInt to have exact definition of C# object, but same...
What have I missed ? I do make a CustomBinderModel for this ?
Thanks for your help.
You need to define the tx argument as an array rather than a List:
public ActionResult SetTxAjustementByDateAndUf(DateTime dateDebut,
DateTime dateFin,
string ufCode,
TauxAjustement[] tx)
{}

How create a MultipartFormFormatter for ASP.NET 4.5 Web API

These links didn't help me:
Way 1
Way 2
Example:
//Model:
public class Group
{
public int Id { get; set; }
public File File { get; set; }
}
//Controller:
[HttpPost]
public void SaveGroup([FromBody]Group group) {}
//Formatter:
public class MultipartFormFormatter : MediaTypeFormatter
{
private const string StringMultipartMediaType = "multipart/form-data";
public MultipartFormFormatter()
{
this.SupportedMediaTypes.Add(new MediaTypeHeaderValue(StringMultipartMediaType));
}
public override bool CanReadType(Type type)
{
return true;
}
public override bool CanWriteType(Type type)
{
return false;
}
public async override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
{
//Implementation? What here should be?
}
}
What should the method ReadFromStreamAsync return?
How do I make it so that you can properly transmit parameter to the action?
public class MultipartFormFormatter : FormUrlEncodedMediaTypeFormatter
{
private const string StringMultipartMediaType = "multipart/form-data";
private const string StringApplicationMediaType = "application/octet-stream";
public MultipartFormFormatter()
{
this.SupportedMediaTypes.Add(new MediaTypeHeaderValue(StringMultipartMediaType));
this.SupportedMediaTypes.Add(new MediaTypeHeaderValue(StringApplicationMediaType));
}
public override bool CanReadType(Type type)
{
return true;
}
public override bool CanWriteType(Type type)
{
return false;
}
public override async Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
{
var parts = await content.ReadAsMultipartAsync();
var obj = Activator.CreateInstance(type);
var propertiesFromObj = obj.GetType().GetRuntimeProperties().ToList();
foreach (var property in propertiesFromObj.Where(x => x.PropertyType == typeof(FileModel)))
{
var file = parts.Contents.FirstOrDefault(x => x.Headers.ContentDisposition.Name.Contains(property.Name));
if (file == null || file.Headers.ContentLength <= 0) continue;
try
{
var fileModel = new FileModel(file.Headers.ContentDisposition.FileName, Convert.ToInt32(file.Headers.ContentLength), ReadFully(file.ReadAsStreamAsync().Result));
property.SetValue(obj, fileModel);
}
catch (Exception e)
{
}
}
foreach (var property in propertiesFromObj.Where(x => x.PropertyType != typeof(FileModel)))
{
var formData = parts.Contents.FirstOrDefault(x => x.Headers.ContentDisposition.Name.Contains(property.Name));
if (formData == null) continue;
try
{
var strValue = formData.ReadAsStringAsync().Result;
var valueType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;
var value = Convert.ChangeType(strValue, valueType);
property.SetValue(obj, value);
}
catch (Exception e)
{
}
}
return obj;
}
private byte[] ReadFully(Stream input)
{
var buffer = new byte[16 * 1024];
using (var ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
}
public class FileModel
{
public FileModel(string filename, int contentLength, byte[] content)
{
Filename = filename;
ContentLength = contentLength;
Content = content;
}
public string Filename { get; set; }
public int ContentLength { get; set; }
public byte[] Content { get; set; }
}
Please see below link for detail implementation:
https://github.com/iLexDev/ASP.NET-WebApi-MultipartDataMediaFormatter
Nuget:
https://www.nuget.org/packages/MultipartDataMediaFormatter/
I actually need to do "multipart/form-data" file upload and model binding today, I tried above lib from nuget and turns out it works as my expectation. Validation on model also works fine. Hopefully it helps to answer your question.

Resources