淘汰赛未能发布表格,无法弄清楚原因

时间:2014-04-10 03:07:54

标签: javascript asp.net knockout.js typescript

我正在使用Knockout,Typescript开发一个包含ASP.NET MVC和API的页面,代码如下:JSFiddler

服务器代码:

// 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);
    }

业务对象:

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; }
}

这个例子不会起作用,因为没有一个要测试的终点。 我的第一个问题是为什么赢得数据绑定工作呢? 其次,最重要的是,我不断收到以下错误消息:


来自网页的消息

[object Object] parsererror

SyntaxError:无效字符 - 无法创建业务

确定

我不知道为什么会这样,我已成功对代码进行了多次更改。有什么想法吗?

我使用的值: CNPJ = 123.321.321-25 名称=测试业务 monthlyLimit = 20000 SubscriptionStart = 01/01/2014 monthContractCount = 24

提前致谢

编辑:

createBusiness = (formElement) => {}

我将方法上的签名更改为如上所示,尝试使用&#34;&#34;解决范围问题。关键词。仍然没有工作。

新编辑:

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.BaseUri更改为&#39; / Api / Empresas /&#39;由于&#34;这个&#34;的范围,这种变化是必要的,所以这是一个修复但不是真的。那里有什么想法吗?

1 个答案:

答案 0 :(得分:0)

变化

var formserialized = $(formElement).serialize();

var formserialized = JSON.stringify($(formElement).serialize());