Luke Rayner Luke Rayner - 1 month ago 7
C# Question

Invalid Expression Flag G Jquery

I am trying to use Jquery to load a partial view on my page but I keep getting this Invalid Expression Flag G error, I have no idea what it means.

Here is the code that I have created in my cshtml file:

<script>
$('#getDetails')
.click(function() {
$('#detailsPlace').load(@Url.Action("GetDetails", "Home"));
});
</script>

<table class="table">
@foreach (var item in Model)
{
<tr>
<td>
<a href="#" id="getDetails">@Html.DisplayFor(modelItem => item.Description)</a>
</td>
</tr>
}
</table>

<div id="detailsPlace"></div>


This is the code inside my contoller:

[HttpGet]
public ActionResult Index()
{
ReferenceRepository test = new ReferenceRepository();

var response = test.GetParts("A");
return View(response);
}

public ActionResult GetDetails()
{
return PartialView();
}


This is just some dummy code in my PartialView:

<body>
<p>HELLOAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</p>
</body>


When I look at the web console which is where I found the error and click on it it takes me to this line of code

$('#detailsPlace').load(/Home/GetDetails);


this is code from the webpage and thats why it looks different to mine. My controller is actually called HomeController but it only expects Home for some reason

Answer

/Home/GetDetails is being interpreted as a regular expression, using "Home" as the pattern and "GetDetails" as the flags. That's an invalid regular expression, hence the error.

You should wrap it in quotes, like this::

$('#detailsPlace').load('@Url.Action("GetDetails", "Home")');
Comments