I'm currently learning about mvc with asp.net. But I got problem when trying to post data from controller.
Here's my code:
[HttpPost]
[ValidateInput(false)]
public ActionResult Create(FormCollection collection)
{
PostCareersViewModel career = new PostCareersViewModel
{
Title = collection["Title"],
Description = collection["Description"],
Photo = collection["Photo"],
CareerStatus = int.Parse(collection["CareerStatus"]),
JobDescription = collection["JobDescription"],
Contact = collection["Contact"],
MainImage = Encoding.ASCII.GetBytes(collection.Get("Photo"))
};
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("Title", career.Title.ToString()),
new KeyValuePair<string, string>("Description", career.Description.ToString()),
new KeyValuePair<string, string>("Photo", career.Photo.ToString()),
new KeyValuePair<string, string>("CareerStatus", career.CareerStatus.ToString()),
new KeyValuePair<string, string>("JobDescription", career.JobDescription.ToString()),
new KeyValuePair<string, string>("Contact",career.Contact.ToString()),
new KeyValuePair<string, string>("MainImage",career.MainImage.ToString())
});
try
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(UrlAddressHelper.Base_Url);
//var responseTask = client.PostAsync(PostCareerString, new FormUrlEncodedContent(
// collection.
// AllKeys.ToDictionary(
// k => k, v => collection[v])));
var responseTask = client.PostAsync(PostCareerString, content);
responseTask.Wait();
var result = responseTask.Result;
if (result.IsSuccessStatusCode)
{
return RedirectToAction("Index");
}
}
ModelState.AddModelError(string.Empty, "Server Error. Please contact administrator.");
return RedirectToAction("Index");
}
catch
{
return View();
}
}
for the view, I'm using this code:
<section class="ftco-section">
<div class="container">
@using (Html.BeginForm("Create", "PostCareers", FormMethod.Post, new { @class = "bg-light p-5 contact-form" }))
{
@Html.AntiForgeryToken()
<h4>Post New Job Vacancy</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" }, id = "edit" })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Photo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.Photo, new { type = "file", placeholder = Html.DisplayNameFor(model => model.Photo), @class = "form-control" })
@Html.ValidationMessageFor(model => model.Photo, "", new { @class = "text-danger" })
@*@Html.EditorFor(model => model.Photo, new { htmlAttributes = new { @class = "form-control" } })*@
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CareerStatus, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("CareerStatus", new SelectList(ViewBag.CSListItem, "Value", "Name"), new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CareerStatus, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.JobDescription, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.JobDescription, new { htmlAttributes = new { @class = "form-control" } })
@*@Html.ValidationMessageFor(model => model.JobDescription, "", new { @class = "text-danger" })*@
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Contact, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Contact, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Contact, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-primary py-3 px-5" />
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
</div>
</section>
I got error bad request 400.
It works perfectly if I use this responseTask
var responseTask = client.PostAsync(PostCareerString, new FormUrlEncodedContent(
collection.
AllKeys.ToDictionary(
k => k, v => collection[v])));
well PostAsJson Async need FormUrlEndcodedContent as parameter and PostAsync need httpcontent as parameret. But it formurlendcoded content works perfectly with PostAsync, And why FormUrlEndcodedContent not running with PostAsJsonAsync?
but I can't convert that MainImage Encoding.ASCII.GetBytes cause I wont store it to my api as file (Actually I don't know if that method works or not to upload file).
Is there any way to deal with this? Am I doing the correct things like convert FormCollection to model to FormUrlEncodedContent (well it is still give me 400 badrequest) ?
I'm really appreciate every answer that posted. Thanks in advance:)



