i am using ASP.NET MVC5 and intended to read json data from controller to view in razor page using kendo grid. I am using trial version for time being so i don't have access to kendo server scripting and helper classes... i have jsonResult in controller and i want ajax to call this function from view--> javascript to read and print data...
model class
[Table("FeeZone")]
public class FeeZone
{
public FeeZone()
{
}
[Key]
public int FeeZoneID { get; set; }
[Required]
public string FeeZoneDescription { get; set; }
[StringLength(50, ErrorMessage = "Description Max Length is 50")]
[Required]
public string CurrencyLabel { get; set; }
[StringLength(10, ErrorMessage = "Description Max Length is 10")]
[Required]
public string CurrencySymbol { get; set; }
[StringLength(50, ErrorMessage = "Description Max Length is 50")]
[Required]
public string CurrencyCode { get; set; }
}
Controller class
public ActionResult FreeZone()
{
var query_result = Q_UOF.GetAllFeeZone();
return View(query_result.ToList());
}
public JsonResult GetAllFreeZone()
{
var allFreeZone = Q_UOF.GetAllFeeZone().ToList();
return Json(allFreeZone, JsonRequestBehavior.AllowGet);
}
razor -- view page
model IEnumerable<DatabaseLayer.TableMappings.FeeZone>
#{
ViewBag.Title = "FreeZone";
Layout = "~/Views/Shared/_Layout_Master.cshtml";
}
<script type="text/javascript">
$(document).ready(function () {
//load ALl freeZone from JSON Method
var RemoteJsonData = new kendo.data.DataSource(
{
transport:
{
read: {
type: "Get",
dataType: "json",
url: "Qualification/GetAllFreeZone"
},
pageSize: 4
}
})
//View all FreeZone data in Kendo Grid
$("#FreeZone_ViewAll_Grid").kendoGrid({
columns: [
{ title: "FeeZoneID" },
{ title: "FeeZoneDescription" },
{ title: "CurrencyLabel" },
{ title: "CurrencySymbol" },
{ title: "CurrencyCode" }
],
dataSource: RemoteJsonData
});
})
</script>
<div id="FreeZone_ViewAll_Grid"></div>
$.ajax({
url: '#Url.Action("GetAllFreeZone", "Qualification")',
type: 'POST',
dataType: "json",
success: function (result) {
var Freezone=result.Freezone;
var dataSource = new kendo.data.DataSource({
data:Freezone
});
$('#FreeZone_ViewAll_Grid').data('kendoGrid').dataSource.data(Freezone);
},
async: false
});
Controller function
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult GetAllFreeZone([DataSourceRequest] DataSourceRequest request)
{
var allFreeZone = Q_UOF.GetAllFeeZone().ToList();
return Json(allFreeZone.ToDataSourceResult(request));
}
Related
I can not send a Model that I am manually creating to my controller. When I send the request, it's coming up with empty properties. There is something wrong that is hindering the conversion. Does anyone know how to help me?
var operadoraChamadas = {
Id: 0,
Descricao: 'rssrrssr',
PadraoSistema: true
};
var requestData = { operadoraChamadasViewModel: operadoraChamadas}
$.ajax({
url: "/pessoa-gerenciar/changeFormaContato",
type: "POST",
data: JSON.stringify(requestData),
contentType: "application/json",
dataType: "json",
success: function (result) {
alert('ok');
},
error: function () {
alert("Oops! Algo deu errado.");
console.log(requestData);
}
});
[HttpPost]
[Route("pessoa-gerenciar/changeFormaContato")]
public IActionResult changeFormaContato(OperadoraChamadaViewModel operadoraChamadaViewModel)
{
//ViewBag.indice_new = indice;
//return PartialView("~/Views/Pessoa/PessoaContato/_PessoaContatoAdd.cshtml", _pessoaContatoAppService.CreateNew(pessoaNatureza, formaContatoId));
return null;
}
ViewModel:
public class OperadoraChamadaViewModel
{
[Key]
[DisplayName("ID")]
public int Id { get; set; }
[Required(ErrorMessage = "A Descrição é obrigatória")]
[MaxLength(50)]
[DisplayName("Descricao")]
public string Descricao { get; set; }
[DisplayName("Padrão do Sistema")]
public bool PadraoSistema { get; set; }
}
ASP.NET Core requires to add [FromBody] attribute to parameter to parse application/json content
[HttpPost]
[Route("pessoa-gerenciar/changeFormaContato")]
public IActionResult changeFormaContato([FromBody] OperadoraChamadaViewModel operadoraChamadaViewModel)
I am working on an Asp Core Project and trying to Ajax post a filepicker blob Javascript Object back to a action to upload the image to Amazon S3. I have tried several different ways and haven't been able to get this to work. It appears to be a model binding issue.
This functionality worked when I was only posting the image url to the action with a string parameter, but I need the filename also.
Issue: model object in action is null
Ajax Code:
function pickit() {
//Start the filepicker
filepicker.pick(
{
//cropRatio: 4 / 3,
////cropMin: [200, 100],
//cropForce: true,
mimetype: 'image/*',
//services: ['CONVERT', 'COMPUTER', 'FACEBOOK', 'FLICKR', 'DROPBOX', 'INSTAGRAM', 'BOX', 'EVERNOTE', 'GMAIL', 'PICASA', 'IMAGE_SEARCH', 'FTP', 'GOOGLE_DRIVE', 'SKYDRIVE', 'URL', 'WEBCAM', 'CLOUDDRIVE', 'IMGUR', 'CLOUDAPP'],
conversions: ['crop', 'rotate', 'filter', 'scale']
},
function (Blob) {
$("#imagespinner").removeClass("invisible")
$.ajax({
type: "POST",
url: '#Url.Action("CreateImage","Photos")',
contentType: 'application/json',
dataType: "json",
data: JSON.stringify(Blob),
success: function (data, textStatus, jqXHR) {
$("#ImageURL").val(data);
var img = document.getElementById("imgImageURL");
img.src = data;
$("#imagespinner").addClass("invisible")
},
error: function (jqXHR, textStatus, error) {
alertify.alert(jqXHR.responseText);
$("#imagespinner").addClass("invisible")
}
});
setTimeout(function () { $('#image-loading-message').fadeOut() }, 500);
},
function (FPError) {
},
function (FPProgress) {
}
);
};
Controller Action:
[HttpPost]
public async Task<JsonResult> CreateImage(FilePickerViewModel model)
{
if (!String.IsNullOrWhiteSpace(model.url))
{
//Code to Upload to S3
return Json("ImageURL");
else
{
Response.StatusCode = 400;
return Json("Url is needed");
}
}
ViewModel:
public class FilePickerViewModel
{
public Int32 id { get; set; }
public String url { get; set; }
public String filename { get; set; }
public Int32 size { get; set; }
public String client { get; set; }
public String mimetype { get; set; }
public Boolean isWriteable { get; set; }
}
Thank you for any help
I figured out the issue, the controller action needs to have the declaration of [FromBody] since this is coming from an ajax post
Hi below is my question.
How to send Javascript array object to the MVC5 controller method.
The code successfully hit the action method but the list value param value is null. I tried different combination with the JSON.stringify as well.
//object class
public class MassPayoutItem
{
public string ReciverEmailID { get; set; }
public string Amount { get; set; }
public int ProviderID { get; set;}
public string AppintmentsID { get; set; }
public string TransictionID{get;set;}
public string TransictionStatus { get; set; }
public string ProviderName { get; set;}
}
//Action method
public ActionResult GetProviderMassPaymentDetail(List<MassPayoutItem> PayoutItemList){
return Json(new { Result = true, ResultData = ResaultData, Message = "" }, JsonRequestBehavior.AllowGet);
}
//JavaScript Code
function MassPay() {
alert("called MassPay");
var MassPymentList = new Array();
var objs;
$('.Include_Payment:checked').each(function () {
var ReciverEmailId = $(this).parents('tr').attr("emailid");
var appointmentids = $(this).parents('tr').attr("appointmentids");
var ProviderID = $(this).parents('tr').attr("UserId");
var Amount = $(this).parents('tr').find(".Amount").text();
var ProviderName = $(this).parents('tr').find(".OwnerName").text();
MassPymentList.push({ "ReciverEmailID": ReciverEmailId, "Amount": Amount, "ProviderID": ProviderID, "AppintmentsID": appointmentids, "ProviderName": ProviderName, "TransictionID": "abc", "TransictionStatus": "bcd" });
});
objs = JSON.stringify({ "PayoutItemList": MassPymentList });
debugger;
// _PageUrl.PayMassTransiction
// '#Url.Action("GetProviderMassPaymentDetail","Controller")'
//The call hits the method but the value is null
$.ajax({
Type: "POST"
, url: _PageUrl.PayMassTransiction
, contentType: "application/json,charset=utf-8",
traditional: true
, data: objs,
datatype: "json",
success: function (result) {
debugger;
alert("called");
}
, error: function (result) {
}
});
}
Hi Finally I solve the issue.
public class MassPayoutItem
{
public string ReciverEmailID { get; set; }
public string Amount { get; set; }
public int ProviderID { get; set;}
public string AppintmentsID { get; set; }
public string TransictionID{get;set;}
public string TransictionStatus { get; set; }
public string ProviderName { get; set;}
}
//Action method
**//This is first change i have make.**
[HttpPost, ActionName("GetProviderMassPaymentDetail")]
public ActionResult GetProviderMassPaymentDetail(List<MassPayoutItem> PayoutItemList){
return Json(new { Result = true, ResultData = ResaultData, Message = "" }, JsonRequestBehavior.AllowGet);
}
//JavaScript Code
function MassPay() {
alert("called MassPay");
var MassPymentList = new Array();
var objs;
$('.Include_Payment:checked').each(function () {
var ReciverEmailId = $(this).parents('tr').attr("emailid");
var appointmentids = $(this).parents('tr').attr("appointmentids");
var ProviderID = $(this).parents('tr').attr("UserId");
var Amount = $(this).parents('tr').find(".Amount").text();
var ProviderName = $(this).parents('tr').find(".OwnerName").text();
MassPymentList.push({ "ReciverEmailID": ReciverEmailId, "Amount": Amount, "ProviderID": ProviderID, "AppintmentsID": appointmentids, "ProviderName": ProviderName, "TransictionID": "abc", "TransictionStatus": "bcd" });
});
objs = JSON.stringify({PayoutItemList: MassPymentList });
debugger;
// _PageUrl.PayMassTransiction
// '#Url.Action("GetProviderMassPaymentDetail","Controller")'
//The call hits the method but the value is null
// below is the changed ajax calll change Type to type
$.ajax({
type: "POST"
, url: _PageUrl.PayMassTransiction
, contentType: "application/json,charset=utf-8"
,async: false
,traditional: true
, data: objs,
datatype: "json",
success: function (result) {
debugger;
alert("called");
}
, error: function (result) {
}
});
}
I have this function in my view:
function EditButtonClick(e1) {
var urlString = '#Url.Action( "Create", "Group")';
var groupItem = {
GroupCode: e1.getAttribute("data-GroupCode"),
GroupType: e1.getAttribute("data-GroupType"),
Description: e1.getAttribute("data-Description"),
InternalNotes: e1.getAttribute("data-InternalNotes"),
StatusCode: e1.getAttribute("data-StatusCode"),
Edit: "true"
};
$.ajax({
type: 'POST',
url: urlString,
data: { '': groupItem },
dataType: 'json'
}).fail(function () {
alert('Edit process failed');
});
}
My view model looks like this:
[Serializable]
public class GroupItem : ApplicationModel, IValidatableObject
{
public GroupItem() { }
[DisplayName("Group Code")]
public int GroupCode { get; set; }
public string GroupTypeDescription { get; set; }
[DisplayName("Group Type")]
public int GroupType { get; set; }
[DisplayName("Group Name")]
public string Description { get; set; }
[DisplayName("Internal Notes")]
public string InternalNotes { get; set; }
public string PartialInternalNotes { get; set; }
public string Status { get; set; }
[DisplayName("Status Code")]
public int StatusCode { get; set; }
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy HH:mm:ss}")]
public DateTime EnterTime { get; set; }
public string UserId { get; set; }
public string ActionType { get; set; }
public bool Edit { get; set; }
and, finally, my action looks like this:
[HttpPost]
public ActionResult Create(GroupItem groupItem)
{
if (ModelState.IsValid)
{
_groupService.SaveGroup(groupItem);
return RedirectToAction("List", "Group", new { showAll = false });
}
ViewBag.GroupTypeList = _MasterDataService.GetCodeMasterList((int)Constants.CodeType.GroupType);
ViewBag.StatusList = _MasterDataService.GetCodeMasterList((int)Constants.CodeType.GroupStatus);
if (groupItem.GroupCode > 0)groupItem.Edit = true;
return this.RazorView(groupItem);
}
Now, I put a break point in the view just before the ajax call, and another in the controller at the top of the action. The properties that I have set up in my groupItem object in the view are all populated as expected, according to an inspection just before the ajax call. However, at the breakpoint in the action, all the properties in the GroupItem argument are default values.
In other code in another view, I call this very same Action method via a form submit. In that case, all the properties are populated as expected. What am I missing in my ajax call?
You need to use JSON.stringify to first serialize your object to JSON, and then specify the contentType so your server understands it's JSON data.
So you complete AJAX function call would become,
$.ajax({
type: 'POST',
url: urlString,
data: JSON.stringify(groupItem),
contentType: "application/json",
dataType: 'json'
}).fail(function () {
alert('Edit process failed');
});
try this:
$.ajax({
type: 'POST',
url: urlString,
data: { 'groupItem': groupItem },
dataType: 'json'
}).fail(function () {
alert('Edit process failed');
});
I am trying to pass a simple JSON object to a controller using MVC3 and JSON. The object gets passed but I loose all the properties. I see all the properties in firebug on the request but am not sure why I am loosing them on the server. Do all the properties of the object need to be set in order for the mapping to work? I am using MVC3 so the binding should be build in. What am I missing?
Class:
[Serializable]
public class StoryNote
{
public int Id { get; set; }
public string Note { get; set; }
public Nullable<int> StoryCardId { get; set; }
public string CreatedBy { get; set; }
public Nullable<System.DateTime> CreateDate { get; set; }
public virtual StoryCard StoryCard { get; set; }
}
JSON:
$(document).ready(function () {
$('#newNote').click(function (e) {
e.preventDefault();
var storynote = {
StoryNote: {
Note: $('#Note').val(),
StoryCardId: $('#StoryCard_Id').val(),
CreatedBy: 'Xyz', }
};
$.ajax({
url: '#Url.Action("PostNote")',
type: 'POST',
data: JSON.stringify(storynote),
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function (data) {
$('#previousNotes').append(data.Note + '<br/>' + data.CreatedBy + '<br/><hr/>');
},
});
});
});
Controller:
[HttpPost]
public JsonResult PostNote(StoryNote newStoryNote)
{
StoryNote newNote = new StoryNote { Note = newStoryNote.Note, CreatedBy = newStoryNote.CreatedBy, StoryCardId = newStoryNote.StoryCardId, CreateDate = DateTime.Now };
db.StoryNotes.Add(newStoryNote);
return Json(newStoryNote, JsonRequestBehavior.AllowGet);
}
You have a name mismatch in your code - parameter is named "StoryNote" in Javascript code and "newStoryNote" in Controller. Those names should be equal. I believe if you change
var storynote = {
StoryNote: {
Note: $('#Note').val(),
StoryCardId: $('#StoryCard_Id').val(),
CreatedBy: 'Xyz', }
};
to
var storynote = {
newStoryNote: {
Note: $('#Note').val(),
StoryCardId: $('#StoryCard_Id').val(),
CreatedBy: 'Xyz', }
};
then your code should work.