MHadi Taghdisi
MHadi Taghdisi

Reputation: 460

URL in razor pages dosen't send data to another page

I'm using Razor Pages and I have two pages CreateModel and EditModel, in CreateModel there is OnGetSubGroups handler:

public class CreateModel : PageModel    
 {
  //Dependency Injection is here

  public IActionResult OnGetSubGroups(int subId)
  {
   SubOptions = courseService.CourseSubGroups(subId);
   return new JsonResult(SubOptions);
   }
 }

now the problem is when i want to pass data using JQuery Ajax in Edit.cshtml to OnGetSubGroups URL doesn't work:

$('#Course_GroupId').on('change', function (e) {
 var optionSelected = $("option:selected", this);
 var valueSelected = this.value;
 $.ajax({
   type: "Get",                
    data: {
    subId: valueSelected,                                           
    },          
    url: "/Create?handler=SubGroups",
    contentType: "application/json; charset=utf-8",
    dataType: "json"
});

i've used breakpoint and url dosen't work. where is the problem?

Upvotes: 0

Views: 609

Answers (2)

Edward
Edward

Reputation: 29986

For routing from Edit to Create, you could not use the URL "/Create?handler=SubGroups" which will generate URL like https://localhost:44389/Create?handler=SubGroups&subId=5 and ignore the PageFolder. I assume CreateModel is not in the root folder Pages. If so, you should not combine URL with "/Create?handler=SubGroups".

I suggest you try code below:

<script type="text/javascript">
    $(document).ready(function () {
        $.ajax({
            type: "Get",
            data: {
                subId: 5,
            },
            url:"@Url.Page("Create", "SubGroups")",
            contentType: "application/json; charset=utf-8",
            dataType: "json"
        });
    });
</script>

Upvotes: 3

Mike Brind
Mike Brind

Reputation: 30045

The first thing you should do is check if the AJAX request is even being made. You can do that in Chrome and IE/Edge by hitting F12 to bring up the developer tools and looking at the network tab when you change the value in the element (Select list?) with the ID of Course_GroupId. If no request is made, then you may have the change event handler wired up to the wrong element or an element that doesn't exist. If request is made, examine its URL and the response.

Upvotes: 0

Related Questions