Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I return the result from Controller to View as in the following:
C#
public async Task<actionresult> Index(decimal? x, decimal? y)
{

...
result = JsonConvert.DeserializeObject<list<road>>(roads);
 return View(result);
}


In the View I have,

JavaScript
function CallFunction() {

alert("test");
}
@if (Model != null)
{
   <text>alert("test")</text>
  @Html.Raw("CallFunction();");
}

  $.ajax({
          url: "Home/Index",
          type: "GET",
          dataType: "html",
          data: { val1: x, val2: y},

          success: function (data) {
                      
           },

          error: function (data)
           {
                //alert(data);
           }

           })

What I have tried:

In the code above, the alerts do not work. When I replace "if (Model != null)" with "if (true)", the Javascript alerts work. I debugged and I am sure that "if (Model != null)" evaluates to true. The goal from this is to check the model properties for specific values and then alert the user. It has to be the AJAX call back to the controller (communication is continuously going on between the controller and the view) that is causing this. Any help is appreciated.
Posted
Updated 1-Oct-20 5:56am
v6
Comments
Member 13628506 2-Oct-20 9:21am    
Anyone can shed some light on this?
bbirajdar 6-Oct-20 10:59am    
It will not work because the Model is null as you are returning the Json from controller action and not a Model. Change your return View() to return the Model.
Member 13628506 24-Oct-20 8:30am    
Model is not null. Figured it out. Checked the model value in success function by returning JSON object from the controller.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900