I am developing a page with ASP.NET MVC and API, using Knockout, Typescript, and the code is as: JSFiddler
The server code:
// POST: api/Empresas
[ResponseType(typeof(Business))]
public async Task<IHttpActionResult> PostBusiness(Business business)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Businesses.Add(business);
await db.SaveChangesAsync();
return CreatedAtRoute("DefaultApi", new { id = business.Id }, business);
}
Business Obj:
public class Business
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[ScaffoldColumn(false)]
public Guid Id { get; set; }
[Display(Name = "CNPJ", Description = "Cadastro Nacional de Pessoa Juridica")]
public string CNPJ { get; set; }
[Display(Name = "Nome", Description = "Razão Social")]
public string Name { get; set; }
[Display(Name = "Limite Mensal", Description = "Limite mensal de uso")]
public int MonthlyLimit { get; set; }
[Display(Name = "Mensal Atual", Description = "Uso Mensal Atual")]
public int MonthlyCurrent { get; set; }
[Display(Name = "Inicio Assinatura", Description = "Data inicial da assinatura")]
public DateTime SubscriptionStart { get; set; }
[Display(Name = "Meses Contratados", Description = "Numero de meses contratados")]
public int MonthContractCount { get; set; }
}
The example is not gonna work since there is not an end point to test against.
My first question is why won't the data-bind work for this?
Second and most important, I keep getting the following error message:
Message from webpage
[object Object]
parsererror
SyntaxError: Invalid character - failed to create business
OK
I have no idea why this is happening, and I have done multiple changes to the code with success. Any ideas?
Values I was using:
CNPJ = 123.321.321-25
Name = Test Business
monthlyLimit = 20000
SubscriptionStart = 01/01/2014
monthContractCount = 24
Thanks in advance
EDIT:
createBusiness = (formElement) => {}
I changed the signature on the method to look like the above, with attempt to solve the scope problem with "this" key word. Still not working though.
NEW EDIT:
createBusiness = (formElement) => {
$(formElement).validate();
if ($(formElement).valid()) {
var formserialized = $(formElement).serialize();
$.post('/Api/Empresas/', formserialized, null, "json")
.done((result) => {
this.Business.Id = result.Id;
this.Business.Name(result.Name);
this.Business.CNPJ(result.CNPJ);
this.Business.MonthlyLimit(result.MonthlyLimit);
this.Business.SubscriptionStart(result.SubscriptionStart);
this.Business.MonthContractCount(result.MonthContractCount);
this.Business.MonthlyCurrent(result.MonthlyCurrent);
})
.fail((x, y, z) => {
alert(x + '\n' + y + '\n' + z + ' - failed to create business');
});
};
}
This is the last change, and it worked now, but as you can see I had to change the this.BaseUri to '/Api/Empresas/' this change was necessary due to scope of "this", so this is a fix but not really. Any ideas there?
change
var formserialized = $(formElement).serialize();
to
var formserialized = JSON.stringify($(formElement).serialize());
Related
Is it possible to restrict access to certain parts of a view based on a Access Restriction? Currently I can limit users from getting to pages by doing this in the controller
[RestrictAccess(restriction = AccessRestrictions.Admin)]
But is it possible for me restrict a certain aspect from the page while still allowing other users access to the page?
Here is model that checks if user has access
public enum AccessRestrictions
{
[Display(Name = "Admin")]
Admin
}
public class userAccess
{
[Key]
public int ID { get; set; }
public AccessRestrictions restriction { get; set; }
public bool allow { get; set; }
public int userID { get; set; }
}
public class configDetails
{
public int ID {get; set;}
public string Name {get; set;}
public string Value {get;set;}
public bool deleted {get;set;}
public DateTime updateTime { get; set; }
}
public class Config
{
public int ID { get; set; }
[Display(Name = "Configuration Date")]
public DateTime TargetDate { get; set; }
[Display(Name = "Enable Access Restrictions")]
public bool restrictAccess { get; set; }
}
Let's say you have follow view
<div id="CallList">
#Html.Action("List","Call",new {id=Model.id})
</div>
<div class="Order">
#Html.Action("Create","Order",new {id=Model.id})
</div>
With follow actions:
[Authorize(Roles = "manager, caller")] //if a user is not 'manager' or 'caller'
public PartialViewResult List() // nothing is shown
{
//...private
return PartialView();
}
[Authorize(Roles = "manager, admin")]
public PartialViewResult Create()
{
//...private
return PartialView();
}
Then you can limit access via this ext. method
public static MvcHtmlString ActionBaseRole(this HtmlHelper value, string actionName, string controllerName, object routeValues , IPrincipal user)
{
bool userHasRequeredRole = false;
Type t = Type.GetType((string.Format("MyProject.Controllers.{0}Controller",controllerName))); // MyProject.Controllers... replace on you namespace
MethodInfo method = t.GetMethod(actionName);
var attr = (method.GetCustomAttribute(typeof(AuthorizeAttribute), true) as AuthorizeAttribute);
if (attr != null)
{
string[] methodRequeredRoles = attr.Roles.Split(',');
userHasRequeredRole = methodRequeredRoles.Any(r => user.IsInRole(r.Trim())); // user roles check in depends on implementation authorization in you site
// In a simple version that might look like
}
else userHasRequeredRole = true; //method don't have Authorize Attribute
return userHasRequeredRole ? value.Action(actionName, controllerName, routeValues) : MvcHtmlString.Empty;
}
Example of usage:
#Html.ActionBaseRole("List","Call",new {id=Model.id},User)
Note your security attributes are different but I think you can easy replace it.
I'm a novice in javascript, and a junior developper in OOP.
After many attempts and many google search I dind't make it to solve it.
I have a DropDownList and a Partial View. I want to give the selected value to the partial view controller. It works when I write the value directly in, but it doesn't if i try to catch the DropDownList value. For the moment the value returned is always empty.
Model
public partial class Clients
{
public int ClientID { get; set; }
public string Code { get; set; }
public string Nom { get; set; }
public string Adresse1 { get; set; }
public string Adresse2 { get; set; }
public string CP { get; set; }
public string Ville { get; set; }
public Nullable<System.DateTime> DateCreation { get; set; }
public Nullable<System.DateTime> DateModification { get; set; }
}
View
#Html.DropDownList("id", (IEnumerable<SelectListItem>)ViewData["oas"], new { #id = "ClientID" })
<div id="result"></div>
<script>
$(function () {
$('#ClientID').change(function () {
//var pid = $("#id").val();//$(this).data('id');
$('#result').load('#Url.Action("filter")',
{ id: $("#id").val() } //replacing $("#id").val() by "3" makes it work, but i of course don't a constant value here
);
});
});
Controller
public class OnePageController : Controller
{
Entities db = new Entities();
public ActionResult Index()
{
List<SelectListItem> list = new List<SelectListItem>();
list.Add(new SelectListItem { Text = "-Please select-", Value = "Selects items" });
var clts = (
from c in db.Clients
select c).ToArray();
for (int i = 0; i < clts.Length; i++)
{
list.Add(new SelectListItem
{
Text = clts[i].Nom,
Value = clts[i].ClientID.ToString(),
Selected = (clts[i].ClientID == 1)
});
}
ViewData["oas"] = list;
return View(/*db.Clients.ToList()*/);
}
[HttpPost]
public ActionResult Filter(string id)
{
var contact = from c in db.Contacts
where c.ClientID.ToString() == id
select c;
return PartialView(contact);
}
}
Any idea would be greatly appreciated, also i don't know how to debug javasript, i use the developper tools in my when browser to try to catch the values, but i don't really track the changes..
You should change a bit your script:
$(function () {
// You select the element with id = ClientID
var clientEle = $('#ClientID');
// You register an event handler for the change of the selected value
clientEle.change(function () {
// clientEle.val() would return the selected value
$('#result').load('#Url.Action("filter")',{ id: clientEle.val() });
});
});
Regarding how you should debug JavaScript I would suggest to write the following keyword a few lines before you want to start the debugging:
debugger;
Then open developer tools and refresh your page. When JavaScript engine hits the debugger would stop it's execution and from this moment you could examine you code line by line.
For a thorough understanding in how you could debug JavaScript, you could start by checking the following links
https://developers.google.com/web/tools/chrome-devtools/javascript/
https://developer.mozilla.org/en-US/docs/Mozilla/Debugging/Debugging_JavaScript
https://www.w3schools.com/js/js_debugging.asp
I'm using Knockout 2.2.1, JQuery 1.9.1, and Ryan Niemeyer's Knockout-jqAutoComplete 0.4.3.
The application allows the user to add a contact to a list of contacts. The contact information comes from a list of possible contacts.
We are using a span to display the names of contacts that exist in the database and data model on page load. When the user clicks the name, they have the opportunity to edit or delete the contact. We hide the span and show an input field in its place. This field is the AutoComplete field and it should show the Name of the contact.
The user can also click a button to add a new contact, which adds a new row with an input field which also uses AutoComplete.
Information edited or added by the user is persisted when a save button is clicked.
The Problem
When the user enters editing mode, the input displays the ID instead of the name.
Here's an image of the data displayed on page load, and here's what it looks like when you switch to editing mode. In the second picture, we'd like for it to display "Doe, John" instead of the primary key from the database.
Using an older version of Knockout-jqAutoComplete, this was not an issue. (We updated to the current version because there were other problems.) I can't find a version number on it, but it was used by including a Razor partial, "_jqueryAutoCompleteBinding.cshtml". The binding for this old version was:
//Deprecated, no longer in use.
<input data-bind="jqAuto: { autoFocus: false }, jqAutoSource: optionsProfessionalsAuto, jqAutoQuery: getProfessionals, jqAutoValue: ProfessionalId, jqAutoSourceLabel: 'displayName', jqAutoSourceInputValue: 'Name', jqAutoSourceValue: 'Id'" class="edit-data Name" />
The binding we're now using is:
<input data-bind="jqAuto: { source: getProfessionals, value: ProfessionalId, valueProp: 'ProfessionalId', labelProp: 'Name', inputProp: 'Name', options: { autoFocus: false } }"class="edit-data Name" />
And here is the relevant portion of the AppViewModel:
function AppViewModel() {
//{...}
self.addProfessionalContact = function () {
var skip = this.ProfessionalContacts().length * 14 - 1; // rows * columns - 1
this.ProfessionalContacts.push({
Name: ko.observable(''),
Specialty: ko.observable(''),
City: ko.observable(''),
State: ko.observable(''),
ProfessionalId: ko.observable(),
optionsProfessionalsAuto: ko.observableArray() //deprecated?
});
WireUpEditGridEvents();
SetDisabled();
RegisterDatePicker();
WireUpPostAddEvents(skip, 'professionalContacts');
$("#professionalContacts tr").last().find(".display-data").hide();
//{...}
}
(Edit 1)
Here is GetProfessionals, as requested:
function getProfessionals(searchTerm, sourceArray) {
try {
$.ajax({
type: "GET"
, contentType: 'application/json; charset=utf-8'
, url: '#Url.Action(MVC.Professional.ActionNames.GetProfessionals, MVC.Professional.Name)?searchTerm=' + searchTerm
, dataType: "json"
, success: function (data) {
var result = [];
for (var i = 0; i < data.length; i++) {
var specialty = data[i].Specialty ? data[i].Specialty + " - " : "";
result.push({ Name: data[i].Name + " (" + specialty + data[i].City + ", " + data[i].State + ")", ProfessionalId: data[i].ProfessionalId });
}
sourceArray(result);
}
}
)
.fail(function () { $.unblockUI(); $.growlUI('Error', 'Failed to get professionals. Unknown error.'); });
} catch (e) {
$.unblockUI();
throw e;
}
(Edit 2)
I think I do need some further clarification.
The getProfessionals function returns a list of type ProfessionalContactEditViewModel. This class contains several properties:
public class ProfessionalContactEditViewModel
{
public int? ClientContactId { get; set; }
public int ProfessionalId { get; set; }
public string Name { get; set; }
public string Specialty { get; set; }
public string Phone { get; set; }
public string Fax { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public bool Send { get; set; }
public bool ReferredBy { get; set; }
public bool Delete { get; set; }
//Added based on RPNiemeyer's response
//public NameIdViewModel ProfessionalNameId
//{
// get
// {
// return new NameIdViewModel()
// {
// Name = this.Name,
// Id = this.ProfessionalId,
// };
// }
// set
// {
// this.Name = value.Name;
// this.ProfessionalId = value.Id ?? default(int);
// }
//}
}
Based upon RP's answer, I added a NameIdViewModel obj. which returns the Name and ProfessionalId. This, however, is not turned into an observable as a nested object, so we're now passing the entire ProfessionalContactViewModel from getProfessionals:
result.push(ko.toJSON(data));
I have removed valueProp from the input binding; it otherwise reads the same as before, but I'm still seeing a number when I edit a contact.
The browser tab also freezes for a few seconds when searching for a contact by name, and displays a completely blank list. The former of these two new issues may be due to the larger amount of data.
I've updated to new version of breeze and ef 6. And after I did this, I get error
newValue.getProperty is not a function
Whenever I try to execute expand query. (for "normal" queries everything is fine )
So here is my model for Mucsle:
public int MuscleId { get; set; }
public string Name { get; set; }
public int MuscleGroupId { get; set; }
public MuscleGroup MuscleGroup { get; set; }
And for MuscleGroup:
public int MuscleGroupId { get; set; }
public string Name { get; set; }
public ICollection<Muscle> Muscles { get; set; }
Here is my DtabaseContext Configuration:
public WebDatabaseContext()
: base("DefaultConnection")
{
Configuration.LazyLoadingEnabled = false;
Configuration.ProxyCreationEnabled = false;
}
And I try to fetch data like this:
Function in dataService:
getAllIncluding: function(controllerAction, including) {
var query = breeze.EntityQuery.from(controllerAction).expand(including);
return manager.executeQuery(query).then(querySucceeded).fail(getFailed);
function querySucceeded(data) {
items = data.results;
return data.results;
}
}
Call of function:
$scope.getAllMuscles = function() {
adminCrudService.getAllIncluding("Muscles", "MuscleGroup")
.then(querySucceeded)
.fail(queryFailed);
};
With older version of breeze and EF5 this works, so I wonder what am I doing wrong ?
EDIT
I believe, I've found what causes problem, when I enter in url:
breeze/Service/Muscles?$expand=MuscleGroup
With "old" (older version of breeze, and EF 5) settings, output is this:
[{"$id":"1","$type":"Test.Domain.Model.MuscleGroupAggregate.Muscle, Test.Domain.Model","MuscleId":1,"Name":"Biceps","NameInLatin":"","ImageUrl":null,"MuscleGroupId":1,"MuscleGroup":null},
{"$id":"2","$type":"Test.Domain.Model.MuscleGroupAggregate.Muscle, Test.Domain.Model","MuscleId":3,"Name":"Triceps","NameInLatin":"","ImageUrl":null,"MuscleGroupId":1,"MuscleGroup":null},
And with EF 6 and latest version of breeze:
[{"$id":"1","$type":"System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.Object, mscorlib]], mscorlib","MuscleGroup":
{"$id":"2","$type":"System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.Object, mscorlib]], mscorlib","MuscleGroupId":1,"Name":"TestMuscleG1","Description":"","ImageUrl":null},"MuscleId":1,"Name":"Test2","NameInLatin":"","ImageUrl":null,"MuscleGroupId":1},
{"$id":"3","$type":"System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.Object, mscorlib]], mscorlib","MuscleGroup":
{"$id":"4","$type":"System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.Object, mscorlib]], mscorlib","MuscleGroupId":1,"Name":"TestMuscleG1","Description":"","ImageUrl":null},"MuscleId":2,"Name":"32r23r","NameInLatin":"","ImageUrl":null,"MuscleGroupId":1}]
So difference is in $type, and in "new" output, even tough, in database I have only two entries, I got 4 items.
Solved it !
So the problem was here:
[HttpGet]
[Queryable(AllowedQueryOptions = AllowedQueryOptions.Supported | AllowedQueryOptions.Expand)]
public IQueryable<Muscle> Muscles()
{
return _contextProvider.Context.Muscles;
}
When I removed this line:
[Queryable(AllowedQueryOptions = AllowedQueryOptions.Supported | AllowedQueryOptions.Expand)]
It worked like a charm,
Jay thanks for all the help :)
I need to visualize a huge set of data inside a timeline. I found 'verite Timeline' looks promising but I don't have any experience with it, using really many data sets. Is here anyone who could tell me about the performance of this tool or tell me about a better solution?
thnx!
As stated in their FAQ ( fourth question ):
How many entries will work best in TimelineJS?
Timeline is optimized for up to 100 entries preferably more like 20-30. Anything else may cause a loading issue.
Now maybe you could not respect their limit, but I do not think that using it with "lots of tons of data" would be a good experience. :) ( well, you could load only 100 entries at a time, but I'll leave this up to you )
I'm not able to suggest you an alternative, sorry.
Sorry ..
again, but in English ....
Verite timeline supports JSON as a data source, I have a project in MVC3 and route the data source to a controller. You obviously can do it differently for instance from a Web Service, or Ria, etc ....
To start the timeline you, put a timeline initial script in the head or body:
var timeline = new VMM.Timeline();
timeline.init("http://localhost:9306/TimeLine/Json");
You create classes to store the data structure, this will allow you to return the instance of the class in the same format json from the driver:
public class json
{
public timeLine timeline
{
get;
set;
}
}
public class timeLine
{
public string headline { get; set; }
public string type { get; set; }
public string startDate { get; set; }
public string text { get; set; }
public asset asset { get; set; }
public List<date> date { get; set; }
}
public class asset
{
public string media { get; set; }
public string credit { get; set; }
public string caption { get; set; }
}
public class date
{
public string startDate { get; set; }
public string endDate { get; set; }
public string headline { get; set; }
public string text { get; set; }
public asset asset { get; set; }
}
The controller:
public ActionResult Json()
{
json j = new json();
j.timeline = new timeLine();
j.timeline.headline = "TimeLine";
j.timeline.type = "default";
j.timeline.startDate = DateTime.Now.Year + "," + DateTime.Now.Month;
j.timeline.text = "<p>Time Line de todo el contenido.</p>";
j.timeline.asset = new asset();
j.timeline.asset.media = "";
j.timeline.asset.credit = "Desarrollador por Mauricio Farias www.ald.cl mfarias#ald.cl";
j.timeline.asset.caption = "Develop By Mauricio Farias";
j.timeline.date = new List<date>();
//for each de tiempos
for (int i = 0; i < 20; i++)
{
date d = new date();
d.startDate = DateTime.Now.Year + "," + DateTime.Now.Month + "," + i;
d.endDate = DateTime.Now.Year + "," + DateTime.Now.Month + "," + (i + 1);
d.headline = "Headline date for date in timeline";
d.text = "<p>text in date for date in timeline</p>";
d.asset = new asset();
d.asset.media = "";
d.asset.credit = "credit in asset for date in timeline";
d.asset.caption = "caption in asset for date in timeline";
j.timeline.date.Add(d);
}
return Json(j, JsonRequestBehavior.AllowGet);
}
At the hearing or in your case may be an aspx or html page (remember to put the css):
<div id="timeline">
</div>
<script src="../../Content/assets/js/timeline-min.js"></script>
<script src="../../Content/assets/js/script.js"></script>