0

I am trying to get my C# MVC to send the data that the model gets as a JSON x-www-form-urlencoded to my web api if I post from postman to the web api it will work but I can't get it to send it through the MVC controller.

I'm trying to do it this way:

Controller:

public ActionResult Update(Models.CaseModel caseModel, int id)
{
    string date = caseModel.DOB.ToString("dd-MM-yyyy");
    string url = "http://10.0.2.31/testwebapi/api/case/UpdateCasePersonal/";

    var json = new JavaScriptSerializer().Serialize(caseModel);

    WebClient wc = new WebClient();

    var data = wc.UploadString(url, json);

    return Redirect(Url.Content("~/Case/Personal/" + id));
}

Model class:

public class CaseModel
{
    public int Caseid { get; set; }
    public string Title { get; set; }
    public string Forename { get; set; }
    public string Surname { get; set; }
    public string Postcode { get; set; }
    public string Telephone { get; set; }
    public string Email { get; set; }
    [DisplayFormat( ApplyFormatInEditMode = true, DataFormatString = "{0:dd'-'MM'-'yyyy}")]
    public DateTime DOB { get; set; }
    public string Mobile { get; set; }
    public string MaritalStatus { get; set; }
    public string LoanPurpose { get; set; }
}

C# Web Api:

[HttpPost]
[Route("UpdateCasePersonal/")]
public object UpdateCasePersonal([FromBody] PersonalModel personal)
{
    using (SqlConnection con = new SqlConnection(conString))
    {
        con.Open();

        var query = $@"UPDATE TestDB.dbo.[crm-data] SET Title=@Title, Forename=@Forename, Surname=@Surname, Telephone=@Telephone, Email=@Email, Mobile=@Mobile, DOB=@DOB, LoanPurpose=@Purpose, MaritalStatus=@Marital WHERE Caseid=" + personal.CaseID;

        using (SqlCommand cmd = new SqlCommand(query, con))
        {
            cmd.Parameters.Add("@Title", SqlDbType.NVarChar, 50).Value = personal.Title;
            cmd.Parameters.Add("@Forename", SqlDbType.NVarChar, 50).Value = personal.Forename;
            cmd.Parameters.Add("@Surname", SqlDbType.NVarChar, 50).Value = personal.Surname;
            cmd.Parameters.Add("@Email", SqlDbType.NVarChar, 50).Value = personal.Email;
            cmd.Parameters.Add("@Telephone", SqlDbType.NVarChar, 50).Value = personal.Telephone;
            cmd.Parameters.Add("@Mobile", SqlDbType.NVarChar, 50).Value = personal.Mobile;
            cmd.Parameters.Add("@DOB", SqlDbType.Date, 50).Value = personal.DOB;
            cmd.Parameters.Add("@Purpose", SqlDbType.NVarChar, 50).Value = personal.LoanPurpose;
            cmd.Parameters.Add("@Marital", SqlDbType.NVarChar, 50).Value = personal.Marital;

            cmd.CommandType = CommandType.Text;

            var dtb = new DataTable();
            var da = new SqlDataAdapter(cmd);
            da.Fill(dtb);

            return "Updated";
        }
    }
}

C# Web Api Model:

public class PersonalModel
{
    [JsonProperty("CaseID")]
    public string CaseID { get; set; }
    [JsonProperty("Title")]
    public string Title { get; set; }
    [JsonProperty("Forename")]
    public string Forename { get; set; }
    [JsonProperty("Surname")]
    public string Surname { get; set; }
    [JsonProperty("Postcode")]
    public string Postcode { get; set; }
    [JsonProperty("Telephone")]
    public string Telephone { get; set; }
    [JsonProperty("Email")]
    public string Email { get; set; }
    [JsonProperty("DOB")]
    public string DOB { get; set; }
    [JsonProperty("Mobile")]
    public string Mobile { get; set; }
    [JsonProperty("Marital")]
    public string Marital { get; set; }
    [JsonProperty("LoanPurpose")]
    public string LoanPurpose { get; set; }
}

I keep getting the error

The remote server returned an error: (415) Unsupported Media Type.

Why is this and how do I fix it?

1
  • Use the "code" button in Postman to get the exact HTTP code it is using and paste it into your question. Commented Sep 4, 2017 at 10:44

1 Answer 1

1

It looks like you haven't set the Content-Type header on your WebClient.

wc.Headers.Add("Content-Type","x-www-form-urlencoded");
Sign up to request clarification or add additional context in comments.

7 Comments

The remote server returned an error: (415) Unsupported Media Type. Is the error im getting now. var data = wc.UploadString(url, json); on this line
What other headers are you providing successfully in Postman? These headers should be replicated in your WebClient code.
the content type is the only one i think it must have something to do with the way that i am encoding the json? but im not sure my postman headers Content-Type:application/x-www-form-urlencoded
its now saying that in my C# web api model that all the values are null and it doesnt know what any of them are
Why are you sending x-www-form-urlencoded to the api? Have you tried setting the header to application/json instead?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.