Ajax ASP. NET ответ перенаправляет на другой URL и отображает JSON файл - PullRequest
0 голосов
/ 23 марта 2020

Я просто пытаюсь получить данные от пользователя, и когда пользователь нажимает кнопку сохранения, данные сохраняются в БД с использованием AJAX, и, если ответ успешен, отображают сохраненную информацию на той же странице.

пока данные сохраняются в дБ, но когда я отправляю ответ JSON обратно на страницу, он перенаправляет на другой URL и отображает JSON файл.

Education.cs html ВИД

@using (Ajax.BeginForm("AddOrUpdateEducation", "Resume", null, new AjaxOptions { HttpMethod = "POST", OnSuccess = "OnSuccess", OnFailure = "OnFailure" })) { //Title
<div class="form-group col-10">
  <label class="font-weight-bold" for="FirstName">Title</label> @Html.EditorFor(model => model.TitleOfDiploma, new { htmlAttributes = new { @class = "form-control form-control-sm", @placeholder = "Title" } })
</div>

//institute
<div class="form-group col-10">
  <label class="font-weight-bold" for="Institute">Institute Name</label> @Html.EditorFor(model => model.InstituteUniversity, new { htmlAttributes = new { id = "Institute", @class = "form-control form-control-sm", @placeholder = "Institution Name" } })
</div>

//Degree
<div class="form-group col-8">
  <label class="font-weight-bold" for="Degree">Degree</label> @Html.EditorFor(model => model.Degree, new { htmlAttributes = new { id = "Degree", @class = "form-control form-control-sm", @placeholder = "Degree" } })
</div>

//Duration
<div class="col-10">
  <div class="form-row ">
    <div class="form-group col-5">
      <label class="font-weight-bold" for="FromYear">From Year</label> @Html.EditorFor(model => model.FromYear, new { htmlAttributes = new { type = "date", id = "FromYear", @class = "form-control form-control-sm", @placeholder = "From Year" } })
    </div>
    <div class="form-group col-5">
      <label class="font-weight-bold" for="ToYear">To Year</label> @Html.EditorFor(model => model.ToYear, new { htmlAttributes = new { type = "date", id = "ToYear", @class = "form-control form-control-sm", @placeholder = "To Year" } })
    </div>
  </div>
</div>


//City
<div class="form-group col-10">
  <label class="font-weight-bold" for="City">City</label><br /> @Html.EditorFor(model => model.City, new { htmlAttributes = new { @id = "City", @class = "form-control form-control-sm", @placeholder = "City" } })
</div>

//Country
<div class="form-group col-10">
  <label class="font-weight-bold" for="Country">Country</label> @Html.DropDownListFor(model => model.Country, Model.ListOfCountry, new { id = "Country", @class = "form-control form-control-sm", @placeholder = "Country" })
</div>
<div class="form-group col-10">
  <input type="submit" id="save" value="Save" class="btn btn-primary mt-2 float-right px-5">
</div>
}
</div>

Контроллер

 [HttpPost]
    public ActionResult AddOrUpdateEducation(EducationVM education)
    {

        //Based upon some conditions

        return Json(new { success = true, Title = education.TitleOfDiploma, degree = education.Degree, institute = education.InstituteUniversity }, JsonRequestBehavior.AllowGet);
    }

Ответ перенаправляет на

http://localhost: 57666 / Resume / AddOrUpdateEducation

Redirected response

...