Issue with C# MVC JS functionality not functioning properly within Partial view following ajax validation

In my layout, I include my script code from the view.

@if (IsSectionDefined("VSscript"))
    {
        @RenderSection("VSscript");
    }

Within the view, I define the script section @section VSscript { and set up an Ajax form that connects to a partial view.

@using (Ajax.BeginForm("CreateServiceQuote", "Service", new AjaxOptions() { HttpMethod = "POST", UpdateTargetId = "ajaxCreate", OnSuccess="quoteSuccess" }))

The ajax portion contains the code for the partial view:

@Html.Partial("SQcreate")

All my javascript is stored in the VSscript section in the view. The partial view only includes the necessary controls. Everything works fine initially, but after one round of validation, the javascript no longer runs. The Ajax onsuccess part always runs, but the code inside my document.ready does not execute. Here's my controller, which simply calls the partial view if it's not valid:

public ActionResult SQcreate()
{
    var model = new ServiceModel();
    return PartialView(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateServiceQuote(ServiceModel model)
{
        if (ModelState.IsValid)
    {

        context.Serv_Quotes.Add(serviceQuote);
        context.SaveChanges();                
        ModelState.Clear();

        return Json("QuoteSuccess", JsonRequestBehavior.AllowGet);
    }            
    return PartialView("SQcreate", serviceModel);
}

This is the layout:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Titler</title>
    @Styles.Render("~/Content/css")
    @Styles.Render("~/Content/themes/base/css")
    @Scripts.Render("~/bundles/modernizr")
    @if (IsSectionDefined("VSscript"))
    {
        @*@RenderSection("VSscript");*@
        @RenderSection("VSscript", required: false)

    }    
</head>

...

And this is the view:

@model PartNumbers.Models.ServiceModel
@{
    HtmlHelper.UnobtrusiveJavaScriptEnabled = true;
    Layout = "~/Views/Shared/_LayoutPageI.cshtml";
    ViewBag.Title = "Virtual Service";

}

@section VSscript {
    <script type="text/javascript">
    $(function () {
$("#btnSQopenCon").on("click", function () {
            $("#AddSQcontact").modal("show");
        })

        // jQuery UI Date Picker Class Object Name
        $(".datepickerui").datepicker();

        ...
})
...

Answer №1

To tackle this issue, I recommend monitoring the 'Network tab' within Dev tools. By doing so, I discovered errors related to certain components of my model failing to load during view switches and reloads.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Sending a boolean value from C# to an Angular view

This is a snippet of my code: Code written in C#: public bool isAllowed { get; set; } protected void Page_Load(object sender, EventArgs e) { isAllowed = false; } Angular controller: <script> var myApp = angular.module('m ...

What is the process for saving an SVG element from an HTML page as a downloadable text file?

I've been practicing creating SVG elements from scratch on an HTML page using Javascript. The texts within the SVG element are styled with typefaces fetched from the server through a "@font-face" declaration in the CSS file. It's been quite succe ...

Having difficulty in successfully transmitting a dictionary via AJAX (POST) to Python (Django views) resulting in an empty dictionary every time

When I include this script in the head of the HTML : <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> and then use a button to call a function: <div> ...

Emails not being sent by Nodemailer

I recently configured my glitch project with a contact form and I'm attempting to set it up so that it sends me an email when someone fills out the form. The issue I'm experiencing is that while the server console logs indicate that the message h ...

Tips for avoiding swiper from interacting with the content upon reaching the final swiper slide button

Can anyone help me with a problem I'm experiencing? When navigating using the next or previous buttons, I always come across one button that is grayed out, indicating that I cannot move forward (for the next button) or backward (for the prev button) a ...

The submission of FormData to the PHP server is causing an issue

I am having an issue with sending the formData to my PHP script using AJAX. Despite inspecting the elements, I can't find any errors in the process. Below is the structure of my form: The input values are sent to a JS file onclick event. <form c ...

Enhancing React Router v4 transitions using the ReactCSSTransitionGroup extension

I've been attempting to create a simple opacity transition for a component in React. I've set up the ReactCSSTransition group, but unfortunately, it doesn't seem to be working as expected. Any help would be greatly appreciated! Here's m ...

Is there a way to retrieve the ID by using the autocomplete feature to search for a user's first name and last name in PHP

Check out this code from index.php (all credit to the original owner): <HTML> <HEAD> <TITLE> Ajax php Auto Suggest </TITLE> <link href="css/style.css" rel="stylesheet" type="text/css"> <SCRIPT LANGUAGE="JavaScript ...

Return all HTML code from the Ajax element

I can't seem to pinpoint the issue with my code. When I make an ajax call using the code below: ajax.js: function ajaxObj(meth, url){ var x = new XMLHttpRequest(); x.open(meth, url, true); x.setRequestHeader("Content-type", "application/x-www_form-u ...

What is the best way to convert this into CQL?

Can someone help me convert this line into CQL for Amazon Keyspaces, which is based on Cassandra? DATEADD(day, @p_daysback, GETDATE()); ...

Ways to retrieve the marginLeft of an element set with absolute positioning?

Currently, I am attempting to display an information box when hovering over an element with the mouse. The issue I am facing is that the element moves, and I need to determine its left margin. However, the element does not have a margin-left property in ...

substitute every item that is a part of a particular group

My current project involves creating an embedded widget. Users will be required to insert an anchor tag and JavaScript code on their page, which then dynamically renders content similar to how embedded tweets work. <a href="http://localhost:3000/user/1 ...

Exporting a SkinnedMesh with position using Three.js

I am attempting to export a model from a Three.js scene using the OBJExporter.js found at https://github.com/mrdoob/three.js/blob/master/examples/jsm/exporters/OBJExporter.js Within the scene, I have both base meshes and SkinnedMeshes. To account for this ...

Building a bridge: How to integrate an existing Javascript library into Angular?

I have developed a pure javascript library that is available for installation via npm. Now, I am looking to enhance its compatibility with Angular projects by creating a seamless "wrapper" for it within Angular. My challenge lies in not knowing the exact p ...

zod - Mastering the Art of Dive Picking

Working with zod and fastify, my UserModel includes the username and device properties. The username is a string, while the device consists of "name", "id", and "verified" fields in an object (DeviceModel). For the sign-up process, I need to return the co ...

An issue has occurred: TransformError SyntaxError: Unexpected keyword 'const' was encountered

While practicing programming with React-Native, I encountered a problem that I couldn't figure out how to solve. I attempted to use solutions from various forums, but none of them worked. import { StyleSheet, Text, View, Image } from 'react-nativ ...

How can I transfer this JavaScript object from jQuery to PHP?

This particular inquiry is derived from a discussion on this forum. Although I have followed the solution provided below, I am encountering difficulties when attempting to pass the object into PHP. I believe it's a minor issue, but I'm unable to ...

Enhancing Request JSON Body Validation in Next.js 14 API Handler

I'm currently in the process of developing an API for my iOS application using Next.js 14. I have managed to get it up and running successfully, however, I am facing some challenges when it comes to properly validating the body of a request. ESLint is ...

A code to ensure input values are always set to the maximum possible value

I recently developed a script that monitors the live value property of 3 <input> elements, correcting them if the live value is outside the specified range. However, I encountered an issue where the script automatically sets the value to the max as s ...

Is there a way to assign a 'null' reference from MongoDB to a POCO's default constructor?

Currently, I am converting MongoDB documents into C# objects (refer to this question for more context). While most entries are successfully mapped, I have encountered some instances where the values are null. This issue arises due to the XML previously con ...