I am trying to set a Profile Property (ComplexID) through the standard Account controller code that comes with the standard MVC3 template. I am using a autocomplete widget from the jquery library to set the value of a hidden field when a value is selected from the autocomplete. The setter is confirmed by an alert I added, but when I try to save the textbox values, I get an error from the Model.IsValid saying the the ComplexID needs to be set. In the Account Model, I have set the ComplexID field as [Required]. What am I doing wrong? Thanks
<div>
<input type="text" name="q" id="complexes" />
#Html.HiddenFor(m => m.ComplexID, new { id="complexid"})
</div>
and the Jscript is:
$("#complexes").autocomplete({
source: function(request, response) {
$.ajax({
url: "/Account/QuickSearch", type: "POST", dataType: "json",
data: { query: request.term },
success: function(data) {
response($.map(data, function(item) {
return { label: item.Name + " , " + item.Address1, value: item.Name, id: item.ComplexID };
}))
}
})
},
minLength: 3,
select: function(event, ui) {
var selecteditem = ui.item;
$("#complexid").text(selecteditem.id);
alert(ui.item ? ("You picked '" + selecteditem.label + "' and the hidden textbox now has a value of " + $("#complexid").text()) : "Nothing selected");
}
});
Change this:
$("#complexid").text(selecteditem.id);
on
$("#complexid").val(selecteditem.id);
Related
I can add the data to the database correctly, but Ajax success function is not working. I always get the error function :
View:
<div id="dvform">
<!-- some data-->
<input type="submit" value="submit" id="btnsubmit" />
</div>
<script>
$(document).ready(function () {
$("#btnsubmit").click(function () {
addAppointment();
});
});
function addAppointment (){
$.ajax({
type: 'POST',
url: '/FuelerAppointments/Create',
contentType: 'application/json; charset=utf-8',
dataType: "html",
data: $("dvform").val(),
success: function (data) {
swal("Done!", "The appointment was saved successfully!", "success");
},
error: function (data) {
swal("Error deleting!", "Please try again", "error");
},
});
}
Controller :
[HttpPost]
public ActionResult Create(FuelerAppointment fuelerAppointment)
{
return Json(fuelerAppointment, JsonRequestBehavior.AllowGet);
}
First you should change dataType: "html", to dataType: "json",
and create FuelerAppointment object before the $.ajax():
var fuelerAppointment = {};
fuelerAppointment.Name = $("#Name").val();
and then the data in ajax method
data: '{fuelerAppointment: ' + JSON.stringify(fuelerAppointment) + '}',
For more information check this link Using AJAX In ASP.NET MVC
change dataType: "html" to dataType: "json"
and in data properties should you write like this
var model = {
prop1: prop1Value,
prop2: prop2Value,
...
};
data: {fuelerAppointment: model}
Or
data: JSON.stringify({
fuelerAppointment: {
prop1: prop1Value,
prop2: prop2Value,
...
}
})
Notes: the fuelerAppointment object should behave a sem proprieties in C# class
My upload process without jconfirm works well, but when I need to use jconfirm library to upload the file, it always says undefined index: myfile.
Here is my code
function upload() {
// body...
$.confirm({
title:'Upload Dental Form',
type:'green',
theme:'material',
content: '<form method="post" id="myform" enctype="multipart/form-data">'
+'<label for="file" id="up"><h3>Select Dental Form</h3></label> <br><br>'
+'<input accept="image/*" type="file" name="myfile" id="myfile" '
+' /> '
+'</form>',
buttons: {
save: {
text:'save',
btnClas: 'btn-blue',
action:function() {
$.ajax({
url:"<?php echo base_url(); ?>Dental/form_upload",
//base_url() = http://localhost/tutorial/codeigniter
method:"POST",
data: $('form').serialize(),
contentType: false,
cache: false,
processData:false,
success:function(data)
{
alert(data);
}
});
}
},
cancel:function() {
}
},
onContentReady: function () {
// bind to events
var jc = this;
this.$content.find('form').on('submit', function (e) {
// if the user submits the form by pressing enter in the field.
e.preventDefault();
jc.$$save.trigger('click'); // reference the button and click it
});
}
});
}
in my controller
function form_upload()
{
echo json_encode(print_r($_FILES["myfile"]["name"]));
}
I'm just echoing for debugging purposes.
The response was undefined index: myfile. Why do I get this error? I have tried using new formData(this) instead of $('form').serialize() but it doesn't work either.
This is hpw i'm filing my dropdown box
$.ajax({
url: "CheckinRelatedMember",
type: "POST",
data: { ClubId: localStorage.getItem("ClubId"), memacctno: localStorage.getItem("memacctno") },
async: false,
success: function (data) {
var RelatedMembers;
RelatedMembers = JSON.parse(data.CheckinRelatedMemberResult);
self.RelMembers(RelatedMembers);
How to get Option value from knockout dropdown list. I had successfully binded an dropdown box. but my problem is while i retrieve value from tag. option value unable to appear.
function FillCheckInRec() {
submemacctno = document.getElementById("RelateMem")[0].value;
i need to pass selected option value to FillCheckInRec() method.
<p>
<label>
Select Member
</label>
<select id="RelateMem" class="input-medium" data-bind="options: RelMembers, optionsText:'Name',optionvalue:'AcctNo', value: 'AcctNo'"></select>
</p>
But while i pass select value into FillCheckinRec() method, value comes like this (""). how to get AcctNo From dropdown list. Please Help Me!
Try something like this
view:
<p>
<label>Select Member</label>
<select data-bind="options: RelMembers, optionsText:'Name',optionsValue:'AccNo', value:AcctNo"></select>
</p>
<hr/>
<b><label data-bind="text:AcctNo"></label></b>
viewModel:
var ViewModel = function () {
var self = this;
self.RelMembers = ko.observableArray();
self.AcctNo = ko.observable();
$.ajax({
url: '/echo/json/',
type: "POST",
data: {},
success: function (data) {
var RelatedMembers = [{
'Name': 'One',
'AccNo': 1
}, {
'Name': 'Two',
'AccNo': 2
}, {
'Name': 'Three',
'AccNo': 3
}]
self.RelMembers(RelatedMembers);
}
});
};
ko.applyBindings(new ViewModel());
working sample here
Well I got fix this issue, Here is my code.
var submemacctno = $("#RelateMem").val();
<select id="RelateMem" class="input-medium" data-bind="options: RelMembers, optionsText:'Name',optionsValue:'AcctNo', value:'AcctNo'"></select>
This is my HTML Code, Rest of everything are same.
Thank You!
How do you get the value of the textbox field and send as data inside of ajaxOptions? I tested my view and it prints out the test variable successfully from my Django views. I am using X-editable for Jquery. Heres what textbox input looks like:
http://jsfiddle.net/xBB5x/197/
views.py
def create_post(request):
print request.POST.get("test", "");
return HttpResponse(json,content_type="application/json")
HTML
<a id="other1" data-pk="First Name" data-name="test">First Name</a>
AJAX
$(document).ready(function() {
$('#other1').editable({
type: 'text',
pk: 1,
url: '/create_post/',
ajaxOptions: {
data: {
csrfmiddlewaretoken: '{{ csrf_token }}',
test: "hi",
},
},
placement: 'top',
title: 'New Expense',
success: function(response, newValue) {
if(response.status == 'error') return response.msg; //ms
},
});
});
Instead of ajaxOptions, use params. If I remember correctly, test and its value will be included in your POST request by x-editable. Try something like this:
Html
<a id="other1" data-pk="1" data-name="test">First Name</a>
AJAX
$(document).ready(function() {
$('#other1').editable({
type: 'text',
url: '/create_post/',
params : function(params) {
params.csrfmiddlewaretoken = '{{ csrf_token }}';
return params;
},
placement: 'top',
title: 'New Expense',
success: function(response, newValue) {
if(response.status == 'error') return response.msg; //ms
},
});
});
I have a Kendo treeview that is built as below codes (see below). Each tree node has a unique data id field (that is employee Id).
I would like to have a text box ( <input type="text" ... /> ) and a button ( <input type="button" ... /> ) so user can input some id and when she hit the button, the button click event handler will let the treeview expand the node whose id matches the input id. How can I do that? Thank you very much.
Details of click event handler or the button:
function buttonExpand_onClick()
{
var id = $("textboxEmployeeId").val();
// ???
// how can I do the following code lines to expand the node with id of "id" to see all its children?
}
Details of the existing Kendo treeview building codes:
<div id="treeviewEmployee">
</div>
<script id="treeview-template" type="text/kendo-ui-template">
#: item.text #
</script>
$(function(
{
var defaultRootSelectedId = 1; // 1 is employee id of the root employee on first loading
$.ajax({
url: '/Employee/AjaxGetEmployeeNodes/?id=' + defaultRootSelectedId,
type: 'GET',
dataType: 'json',
async: false,
success: function (data, textStatus, xhr) {
$("#reeviewEmployee").kendoTreeView({
template: kendo.template($("#treeview-template").html()),
dataSource: data,
select: treeview_onSelect
});
_treeview = $("#treeviewEmployee").data("kendoTreeView");
},
error:
function (xhr, textStatus, errorThrown) {
alert(textStatus);
}
});
});
You can access the datasource on the treeview and find the node by id. I would also like to add that the treeView has a 'findByText()' method as well, in case that is what you want.
HTML
<script id="treeTemplate" type="text/x-kendo-template">
#: item.text #
</script>
<div id="content">
<div id="form">
<label>Node ID:
<input id="nodeId" type="text"/>
</label>
<button id="expandNodeBtn">Expand Node</button>
</div>
<h2>TreeView</h2>
<div id="treeView"/>
</div>
JAVASCRIPT
(function ($) {
$(document).ready(function () {
$("#treeView").kendoTreeView({
dataSource: [
{
text: 'one with id 1',
id: 1,
items: [
{
text: 'one-child-1',
id: 2
},
{
text: 'one-child-2',
id: 3
}
]
},
{
text: 'two with id 4',
id: 4,
items: [
{
text: 'two-child-1',
id: 5
},
{
text: 'two-child-2',
id: 6
}
]
}
]
});
$("#expandNodeBtn").on("click", function(e) {
var val = $("#nodeId").val();
console.log('val: ' + val);
var treeView = $("#treeView").data('kendoTreeView');
var dataSource = treeView.dataSource;
var dataItem = dataSource.get(val); // find item with id = 5
var node = treeView.findByUid(dataItem.uid);
treeView.expand(node);
});
});
})(jQuery);
JSFiddle
I also put together a JSFiddle sample for you to play with: http://jsfiddle.net/jsonsee/D35Q6/
Slightly related, but I came here looking for an answer to this question: How to expand the whole branch when clicking to a parent node in angular treeview? Since I didnt find any answers, I post my solution here. Hope it helps someone.
html
<div id="treeview" kendo-tree-view="tree" k-options="options" k-on-change="selectItem(dataItem)">
</div>
controller
$scope.options = {
dataSource: dummyData,
template: $scope.treeItemTemplate
}
$scope.treeItemTemplate = "<button ng-click='expandRoot(dataItem)'>Blow up</button>";
$scope.expandRoot = function expandRoot(dataItem) {
dataItem.expanded = true;
if (dataItem.hasChildren) {
dataItem.load()
var children = dataItem.children.data();
children.forEach(function (c) {
c.expanded = true;
$scope.expandRoot(c)
});
}
}