简体   繁体   中英

Reading Javascript FormData value in Asp.net Web api Httpresponsemessage

I have a Web api which is decorated with [HttpPost] which is used to upload all information passed in parameter but getting this error

Error

在此处输入图片说明

Controller

  [HttpPost]
   public HttpResponseMessage questionnairesubmit(HttpRequestMessage form)
    {

     //want to get json of "questionlist" which is send from javascript like
      var QuestionnaireList = JsonConvert.DeserializeObject<List<outRightLogos.Areas.client.WebApiModel.AttributeValueTB>>(form["questionlist"]);


    }

Here is javascript sending Json model with name questionlist this I want to fetch in controller.I read FormData() from Here

Javascript

var data = new FormData();
var QuestionnaireList = [];
 QuestionnaireList.push({

            FieldID: $(this).attr("data-fieldid"),
            attributeID: $(this).attr("data-attributeid"),
            attributeValue: $(this).val(),
            websitesID: 2,
            OrderID: $('#orderid').val(),
            orderbitID: $('#orderbidid').val(),
            serviceId: $(this).attr("data-serviceid"),
            subServiceId: $(this).attr("data-subserviceid"),
            IsSubmit: IsSubmit,
        });

data.append("questionlist", JSON.stringify(QuestionnaireList));
$.ajax({
                    type: "POST",
                    url: path,
                    contentType: 'application/json',//"application/json; charset=utf-8",
                    processData: false,
                    dataType: "json",
                    data: data,

                    success: function (result) {

                        if (result.sucess == "save") {
                            alert('Your form has been saved.');

                        }
                        else if (result.sucess == "Submit") {
                            alert('Your form has been submitted.');
                            window.location.href = result.Url;
                        }
                    },
                    error: function (result) {
                        alert('Oh no ');
                    }

                });

Here is a model class AttributeValueTB

Class

 public class AttributeValueTB
{
    public long attributeValueID { get; set; }

    [Required]
    [StringLength(200)]
    public string attributeValueCode { get; set; }

    public int FieldID { get; set; }

    public int attributeID { get; set; }

    public string attributeValue { get; set; }

    public int websitesID { get; set; }

    public long OrderID { get; set; }
    public long orderbitID { get; set; }
    public long serviceId { get; set; }
    public long subServiceId { get; set; }
    public string extra1 { get; set; }
    public string extra2 { get; set; }
    [StringLength(200)]
    public string attributeCode { get; set; }
    public bool isActive { get; set; }

    public bool isShow { get; set; }
    public bool IsSubmit { get; set; }

    [Column(TypeName = "date")]
    public DateTime? createDate { get; set; }

    [Column(TypeName = "date")]
    public DateTime? modifiedDate { get; set; }

    public int? createBy { get; set; }

    public int? modifiedBy { get; set; }

    [Column(TypeName = "timestamp")]
    [MaxLength(8)]
    [Timestamp]
    public byte[] Timestamp { get; set; }
}

You don't need to serialize anything manually, as long as you've provided a model class in C#.

Json.NET is quite smart and serializes your parameters automagically.

[HttpPost]
public HttpResponseMessage QuestionnaireSubmit(AttributeValueTB form)
{
   // Form is serialized and can be used here       

}

If you'd like to read the cookies too, you could use Request.Cookies in this method.

you need to collect information from 'jsoncontent' not from 'form'

HttpContent requestContent = Request.Content;
string jsonContent = requestContent.ReadAsStringAsync().Result;
entityclass obj= JsonConvert.DeserializeObject<entityclass>(jsonContent);

and if you using class object as parameter also work with your context, need to change the method as put like this.

[HttpPut]
public HttpResponseMessage Put(int id)
{
  HttpContent requestContent = Request.Content;
  string jsonContent = requestContent.ReadAsStringAsync().Result;
  entityclass obj= JsonConvert.DeserializeObject<entityclass>(jsonContent);
  ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM