简体   繁体   中英

C# MVC Ajax modal validation - 500 internal server error

I'm not really sure why this isnt working, I have the exact same thing in a different part of the project, Im getting a “POST” 500 (Internal Server Error) I'm 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.

this is script section

<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>
}

and this is the controllers code

        [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());
        } 

ADDED - Here is the Partial View 'PNControls'

@model Messer_PartNumbers.Models.FindPartModel
@{ 
    HtmlHelper.UnobtrusiveJavaScriptEnabled = true;
}

@Html.AntiForgeryToken()

<span class="alert-danger">
    @Html.ValidationSummary(true, "", new { @class="text-danger" })
</span>

@Html.HiddenFor(x => x.PartVM.PartID)
@Html.HiddenFor(x => x.PartVM.PartGroup)
@Html.HiddenFor(x => x.PartVM.GlobalPart)
@Html.HiddenFor(x => x.PartVM.Released)

<div class="form-group">
    @Html.LabelFor(x =>x.PartVM.PartNumber, htmlAttributes: new { @class="control-label col-3" })
    <div class="col-9">
        @Html.TextBoxFor(x => x.PartVM.PartNumber, new { @class="form-control", @readonly="readonly" })
        @Html.ValidationMessageFor(x => x.PartVM.PartNumber, "", new { @class="text-danger" })
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(x => x.PartVM.EnteredBy, htmlAttributes: new { @class = "control-label col-3" })
    <div class="col-9">
        @*@Html.TextBoxFor(x => x.PartVM.EnteredBy, new { @class="form-control" })*@
        @Html.DropDownListFor(x=>x.PartVM.EnteredBy, Model.PNEnteredByOptions, new { @class="form-control"})
        @Html.ValidationMessageFor(x => x.PartVM.EnteredBy, "", new { @class = "text-danger" })
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(x => x.PartVM.DateEntered, htmlAttributes: new { @class = "control-label col-3" })
    <div class="col-9">
        @Html.TextBoxFor(x => x.PartVM.DateEntered, new { @class = "form-control", @readonly = "readonly" })
        @Html.ValidationMessageFor(x => x.PartVM.DateEntered, "", new { @class = "text-danger" })
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(x => x.PartVM.MachineTypes, htmlAttributes: new { @class = "control-label col-3" })
    <small>Hold Ctrl to select multiples</small>
    <div class="col-9">
        @*@Html.TextBoxFor(x => x.PartVM.MachineTypes, new { @class = "form-control" })*@
        @Html.ListBoxFor(x => x.PartVM.MachineTypes, Model.PNMachineTypeOptions, new { @class = "form-control", @size = 5, @style = "overflow:scroll;" })
        @Html.ValidationMessageFor(x => x.PartVM.MachineTypes, "", new { @class = "text-danger" })
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(x => x.PartVM.Description, htmlAttributes: new { @class="control-label col-3" })
    <div class="col-9">
        @Html.TextBoxFor(x => x.PartVM.Description, new { @class="form-control" })
        @Html.ValidationMessageFor(x => x.PartVM.Description, "", new { @class="text-danger" })
    </div>
</div>

I found my error.

It was custom validation on my model. Was using the other model.

Thank you@! I ended up figuring it out by using the network tab and in 'All' looking through the stacktrace figuring out a more detailed error. This post is what gave me the idea to use the stacktrace in the developer tools network tab. 500 Internal Server Error in ASP.NET MVC

    [HttpPost]
    [ValidateAntiForgeryToken]
    [HandleError]
    public ActionResult PartNumberUpdate(FindPartModel model){/*skip*/}

You added [ValidateAntiForgeryToken] on top of your action.

but you don't have @Html.AntiForgeryToken() in your view.

@using (Ajax.BeginForm("PartNumberUpdate", "Parts", new AjaxOptions() { HttpMethod = "POST", UpdateTargetId = "PartNumControls", OnSuccess = "ajaxPartUpdate" })){
    @Html.AntiForgeryToken() //Add me
    //skip
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM