Ajax BeginForm Model validation Error: 500 (Internal Server Error) in ASP.Net MVC

Last Reply one year ago By dharmendr

Posted one year ago

I am not really sure why this isn't working. I have the exact same thing in a different part of the project, Im getting a POST 500 (Internal Server Error)

I am simply just trying to get @using (Ajax.BeginForm( Validation to work on a modal. Like I said I have this exact thing working on a different part of the project and it works. My modal populates successfully but when I click the submit button I get the error above. In my view both the modal and controls of the modal

I have HtmlHelper.UnobtrusiveJavaScriptEnabled = true;

I have all of these libraries In my code.

<script src="/Scripts/jquery-3.3.1.js"></script>
<script src="/Scripts/jquery-ui-1.12.1.js"></script>
<script src="/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="/Scripts/jquery.validate.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.js"></script>
<script src="/Scripts/bootstrap.js"></script>
<script src="/Scripts/respond.js"></script>

@using (Ajax.BeginForm("PartNumberUpdate", "Parts", new AjaxOptions() { HttpMethod = "POST", UpdateTargetId = "PartNumControls", OnSuccess = "ajaxPartUpdate" }))
{
    <div class="modal" id="modalPNUpdate" tabindex="-1" role="dialog" aria-labelledby="lblPNUpdate" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h3 class="modal-title">Part Number Details</h3>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body" id="PartNumControls">
                    @Html.Partial("PNControls")
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                    <input type="submit" class="btn btn-primary" value="Save" />
                </div>
            </div>
        </div>
    </div>
}

 

[HttpPost]
[ValidateAntiForgeryToken]
[HandleError]
public ActionResult PartNumberUpdate(FindPartModel model)
{
    if (ModelState.IsValid)
    {
        var partNumber = context.PartNumbers.Where(x => x.PartNumber1 == model.PartVM.PartNumber).FirstOrDefault();
        partNumber.PartNumber1 = model.PartVM.PartNumber;

        /// UPDATE PartNumber Record
        context.Entry(partNumber).State = EntityState.Modified;
        context.SaveChanges();
        string returnStr = "refresh";
        ModelState.Clear();
        return Json(returnStr);
    }
    return PartialView("PNControls", model);
}

public ActionResult PNControls()
{
    return View(new FindPartModel());
}

 

You are viewing reply posted by: dharmendr one year ago.
Posted one year ago

Hi bigbear,

Your post method has the [ValidateAntiForgeryToken] attribute.

So either remove the attribute from Controller Action method or add AntiForgeryToken token in the view.

@Html.AntiForgeryToken()