9

我是 ASP.net(和一般编程)的新手,我在构建 Web API 时遇到了麻烦。更具体地说,我在这两个方面需要帮助:

  1. 如何配置我DOCcontroller发布新文档(DOC 表)。
  2. 如何制作实际的 ajax 帖子——我无法传递EXT_GUID参数。就目前而言,当我尝试发布时出现错误。“无法将多个参数(doc 和 parentOwner)绑定到请求的内容。”

本质上这是一个简单的文档管理系统。我希望通过让用户从外部数据库(EXT_GUID 字段)提供一个 GUID 作为过滤器/参数来获取/发布文档 (DOC)。每个文档可以有多个 EXT_GUID,每个 EXT_GUID 可以有多个文档 (DOC)。您可以假设我们在 http 帖子之前填充了 EXT_GUID 字段。

这是 DOC 控制器代码

//POST api/DOC
public HttpResponseMessage PostDOC(DOC doc, List<string> parentOwners)
{
    if (ModelState.IsValid)
    {            
        var parents = db.BIMs.Where(bx => parentOwners.Contains(bx.EXT_GUID));

        foreach (var p in parents)
        doc.Owners.Add(p);

        db.DOCs.Add(doc);
        db.SaveChanges();

        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, doc);
        response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = doc.Id }));
        return response;
    }
    else
    {
        return Request.CreateResponse(HttpStatusCode.BadRequest);
    }
}

这是我的模型设置——EntityFramework codefirst 的东西

public class EXT
{
    public int Id { get; set; }
    public string EXT_GUID { get; set; }
    public int ProjectID { get; set; }
    public virtual ICollection<DOC> DOCs { get; set; }
}

public class DOC
{
    public int Id { get; set; }
    public int ProjectID { get; set; }
    public string Subject { get; set; }
    public string Link { get; set; }
    public virtual ICollection<EXT> EXTs { get; set; }
}

这是更多的存储模型...

public StoreDBContext() : base("name=StoreDBContext")
{
}
public DbSet<EXT> EXTs { get; set; }
public DbSet<DOC> DOCs { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    //Set FLUENT API config for many to many here
    modelBuilder.Entity<EXT>()
    .HasMany(a => a.DOCs)
    .WithMany()
    .Map(x =>
    {
        x.MapLeftKey("EXT_Id");
        x.MapRightKey("DOC_Id");
        x.ToTable("EXTsDOCs");
    });
}

AJAX 代码

function AddDOC() {
    var parentOwner = "{\"" + $('#txtaddEXT').val() + "\"}";
    jQuery.support.cors = true;
    var DOC = {
        ProjectId: ProjectID,
        Subject: $('#txtaddDOCSubject').val(),
        Link: $('#txtaddDOCLink').val(),
            parentOwner: parentOwner
    };

    $.ajax({
        url: "http://localhost:54171/api/DOC/",
        type: 'POST',
        data: JSON.stringify(DOC),
        contentType: "application/json;charset=utf-8",
        success: function (data) {
            WriteResponse(data);
        },
        error: function (x, y, z) {
            alert(x + '\n' + y + '\n' + z);
        }
    });
}
4

1 回答 1

0

您从客户端收到的内容和您将保存在数据库中的内容是两件不同的事情。您的 doc 对象没问题:

var DOC = {
    ProjectId: ProjectID,
    Subject: $('#txtaddDOCSubject').val(),
    Link: $('#txtaddDOCLink').val(),
    parentOwner: parentOwner
};

现在您需要更改服务器逻辑。制作这样的模型:

public class DocReceivedModel 
{
    public int ProjectID { get; set; }
    public string Subject { get; set; }
    public string Link { get; set; }
    public List<string> parentOwner { get; set; }
}

那么您的 PostDOC 方法将是:

            public HttpResponseMessage PostDOC(DocReceivedModel docReceived)
            {
                if (ModelState.IsValid)
                {
                    Doc newDoc = new Doc();
                    newDoc.ProjectID = docReceived.ProjectID
                    newDoc.Subject = docReceived.Subject
                    newDoc.Link = docReceived.Link

                    var parents = db.BIMs.Where(bx => docReceived.parentOwners.Contains(bx.EXT_GUID));
                    foreach (var p in parents)
                        newDoc.Owners.Add(p);
                            // I not see in your model Owners, maybe this is EXTs but I suppose you catch the idea

                    db.DOCs.Add(newDoc);
                    db.SaveChanges();

                    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, newDoc);
                    response.Headers.Location = new Uri(Url.Link("DefaultApi", new {id = newDoc.Id}));
                    return response;
                }
                else
                {
                    return Request.CreateResponse(HttpStatusCode.BadRequest);
                }
            }
于 2013-09-14T07:39:59.360 回答