I am using ASP.NET Core 3.1 MVC, When ever I am sending data to my HTTP Post method api, I am facing Unsupported media type.
I have used [FromBody] in my api.
Here is my api:
- [HttpPost]
- [Route("addCourseWeek")]
- public String AddCourseWeek([FromBody]CourseWeek data)
- {
- data.WeekName = String.Concat(data.WeekName.Where(c => !Char.IsWhiteSpace(c)));
- if (data != null && !string.IsNullOrWhiteSpace(data.WeekName) && data.WeekName.Length > 0)
- {
- data.CreatedAt = DateTime.Now;
- db.CourseWeeks.Add(data);
- }
- else
- {
- return Convert.ToString(ValidationProblem());
- }
-
- try
- {
- db.SaveChanges();
- }
- catch
- {
- return Convert.ToString(NotFound());
- }
- return JsonConvert.SerializeObject(db.CourseWeeks,Formatting.Indented);
- }
Here is my html form:
- @model CourseGamePlay.Models.CourseWeek
- @{
- ViewBag.Title = "Admin Panel - Weeks";
- Layout = "~/Views/Shared/_adminLayout.cshtml";
- }
- <h2>Add Weeks</h2>
- <body>
- <form method="post" action="/api/addCourseWeek">
- <div class="form-horizontal">
- <div class="form-group">
- @Html.LabelFor(cw => cw.CourseId, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-4">
- @Html.EditorFor(cw => cw.CourseId, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(cw => cw.CourseId, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(cw => cw.WeekName, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-4">
- @Html.EditorFor(cw => cw.WeekName, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(cw => cw.WeekName, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(cw => cw.StartDate, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-4">
- @Html.EditorFor(cw => cw.StartDate, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(cw => cw.StartDate, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(cw => cw.EndDate, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-4">
- @Html.EditorFor(cw => cw.EndDate, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(cw => cw.EndDate, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(cw => cw.UpdatedAt, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-4">
- @Html.EditorFor(cw => cw.UpdatedAt, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(cw => cw.UpdatedAt, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- <div class="col-md-offset-2 col-md-15">
- <input type="submit" value="Submit" class="btn btn-primary" />
- </div>
- </div>
- </div>
- </form>
- </body>
Please help me out in it, if anybody knows it's solution. Thanks in advance.