从表单到Web Api的XML发布请求

时间:2014-08-07 05:52:27

标签: xml asp.net-mvc razor asp.net-web-api

在创建asp.net mvc应用程序时得到一些问题。我如何将XML从剃刀视图表单发布到Web Api控制器 我有这个课程:

namespace webapi.Models
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Runtime.Serialization;
    using System.Xml.Serialization;
    [DataContract(Name = "shepherd")]
    public partial class Shepherd
    {
        public Shepherd()
        {
            this.Sheep = new HashSet<Sheep>();
        }
        [DataMember(Name = "shepherdId")]
        public int Id { get; set; }
        [DataMember(Name = "name")]
        public string Name { get; set; }
        [DataMember(Name = "isDeleted")]
        public bool IsDeleted { get; set; }
        [DataMember(Name = "sheeps")]
        public virtual ICollection<Sheep> Sheep { get; set; }
    }
}
namespace webapi.Models
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.Serialization;
    using System.Xml.Serialization;
    [DataContract(Name = "sheep")]
    public partial class Sheep
    {
        [DataMember(Name = "id")]
        public int Id { get; set; }
        [DataMember(Name = "colour")]
        public string Colour { get; set; }
        [DataMember(Name = "createdon")]
        public DateTime CreatedOn { get; set; }
        public int Shepherd { get; set; }
        public virtual Shepherd Sh { get; set; }
    }
}

和这个剃刀视图

using (Html.BeginForm())
{
    <div id="createform" class="form-horizontal">
        @Html.EditorFor(x => x.Name)
        @Html.EditorFor(x => x.Sheep.FirstOrDefault().Colour)
        <input type="button" id="crate" value="Create" />
    </div>
}

我怎样才能将牧羊人以xml形式发布到这个动作而不是json,只有XML。

[ResponseType(typeof(Shepherd))]
        public async Task<IHttpActionResult> PostShepherd(Shepherd shepherd)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Shepherds.Add(shepherd);
            await db.SaveChangesAsync();

            return CreatedAtRoute("DefaultApi", new { id = shepherd.Id }, shepherd);
        }

1 个答案:

答案 0 :(得分:0)

已修复请求

var xml = '<shepherd xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/webapi.Models">' +
                 '<name>' + $("#Name").val() + '</name>' +
                 '<sheeps>' +
                 '<sheep>' +
                 '<colour>' + $("#Colour").val() + '</colour>' +
                 '</sheep>' +
                 '</sheeps>' +
                 '</shepherd>';
             $.ajax({
                 type: "POST",
                 url: "@Url.Action("Shepherds", "api")",
                 data: xml,
                 dataType: "xml",
                 contentType: "application/xml; charset=utf-8",
                 cache: false,
                 success:
                 function (xml) {
                     alert(xml);
                 },
                 error:
                 function (xml) {
                     alert("error");
                 }
             })