First time posting here, but I have exhausted every google permutation I can think of.
Unfortunately, I am working with a view that needs to have multiple partial views loaded at the same time, that are using the same model. (Did not build this codebase, but its what I've got.)
I am now being tasked with creating a pop-up modal to edit one of these objects. However, since there is already another view using this object loaded into the DOM, I will need to add an HtmlFieldPrefix as to not confuse JQuery.
(View) Edit.cshtml
@model THE_MODEL
@{
ViewBag.Title = "Edit Appointment";
if (ViewContext.IsChildAction || Request.IsAjaxRequest()) { Layout = "~/Views/Shared/_LayoutModal.cshtml"; }
}
@using (Html.BeginForm("EditModalSubmit", "Appointments", FormMethod.Post, new { id = "frmAppointment" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(false, "")
<div id="divContent">
<div id="frmDetail">
@Html.Partial("~/Views/Appointments/_Detail.cshtml", Model, new ViewDataDictionary(ViewContext)
{
TemplateInfo = new System.Web.Mvc.TemplateInfo
{
HtmlFieldPrefix = "Detail"
}
})
</div>
</div>
<div class="button-list">
<div style="min-width: 150px; display: inline-flex;"></div>
@if ((ViewContext.IsChildAction || Request.IsAjaxRequest()) && ViewContext.RouteData.Values["action"].ToString().ToUpper() == "EDIT")
{
@Html.ButtonModal("post", "Save", "Edit", "Appointments")
<text> </text>
@Html.ButtonModal("hide", "Cancel")
if (User.IsInRole(Constants.MgmtGroup))
{ @Html.ButtonModal("show", "Delete", "Delete", "Appointments", htmlAttributes: new { @class = "delete-ctl" }) }
}
</div>
</div>
}
(Partial within above view) _Detail.cshtml (Not going to show all of it, its just a form with kendo elements to edit.)
@model THE_MODEL
<table style="border-collapse: collapse; table-layout: fixed;">
<tr class="form-row" style="display: table-row;">
<td>
@Html.LabelFor(p => p.ASLS_MEET_DATE, new { @class = "form-label" })
</td>
<td>
@Html.Kendo().DateTimePickerFor(p => p.ASLS_MEET_DATE).HtmlAttributes(new { @class = "form-field" })
@Html.ValidationMessageFor(p => p.ASLS_MEET_DATE)
</td>
@* AND SO ON *@
And when I hit the save button in Edit.cshtml, it is hitting this route.
AppointmentsController.cs
[HttpPost]
[ValidateModelState]
[Route("Appointments/EditModalSubmit/{id}")]
public ActionResult EditModalSubmit([Bind(Prefix = "Detail")] THE_MODEL model)
{
ValidateAppointment(ref model);
if (ModelState.IsValid)
{
model.ASLS_CHG_DATE = DateTime.Now;
db.THE_MODEL.Attach(model);
db.Entry(model).State = EntityState.Modified;
db.SaveChanges();
if (Request.IsAjaxRequest())
{
return AjaxSuccess("Appointment successfully updated.");
}
}
// NEED TO FIX THIS - This will give validation errors.
return PartialView("Edit", model);
}
Assuming there is some kind of validation error (I put in a future date or something, ValidateAppointment fails.), it will throw me the return PartialView("Edit", model);
However, I then am hit in chrome with "parsererror - SyntaxError: Unexpected token < in JSON at position 6"
Some things I have tried
- I have tried to instead just have it return the simplest view on earth.
return PartialView("TinyView");
<h2>TinyView</h2>
And it still returns the same thing. Note: Other routes return this just fine.
- Looked at the network response requests And it simply just returns the correct html.
None of the countless other routes here return JSON or anything, they all work like this. My running theory is that it has to do with me throwing that Bind/Prefix into the route, but for the life of me I have run out of options. Most StackOverflow results about things like this are all super JQuery heavy, but not much that is strictly cshtml, etc.
I would greatly appreciate any suggestions here, I have been trying to fix this for ~3 days now and I'm losing my mind.
Edit: THE_MODEL.cs (not going to show much for confidentiality)
namespace REDACTED
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
public partial class tblACCT_SALES
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int ASLS_ACCT { get; set; }
...
[Column(TypeName = "DATETIME")]
[Display(Name = "Meet Date")]
public DateTime? ASLS_MEET_DATE { get; set; }