1

Im new in MVC,I have problem the button cannot call Ajax function to render Action in controller.

this bellow my script button

<input type="submit" value="show" class="btn btn-success" name="submitButton" onclick="OnActionClick()" />

My Ajax

function OnActionClick() {
    alert("Error when update data");   
    $.ajax({
            type: "GET",
            url: "@Url.Content("~/Report/ViewReport")",
            data: {
                name:'try'
            },
            success: function () {
               alert("ok"); 
            },
            error: function () {
                alert("Error when update data");                    
            }
        });        
}

and here my Action in controller

public ActionResult ViewReport(string name)
    {
        Database db = new Database("Collections");
        List<report> data = new List<report>();
        data = db.Fetch<report>(@"select top 10 * from SampleTbl where name = @0",new object[]{name});
        return View(data);
    }
Indra Suandi
  • 47
  • 1
  • 5

2 Answers2

0

You need to use Url.Action() instead of using Url.Content().

Generates a fully qualified URL to an action method.

url: '@Url.Action("ViewReport", "Report")',

And you should type="button"

<input type="button" onclick="OnActionClick()" />
Satpal
  • 132,252
  • 13
  • 159
  • 168
0

You should use @Url.Action() and not @Url.Content(). Please see this for difference between the two: MVC @Url.Content vs @Url.Action

Community
  • 1
  • 1
User3250
  • 2,961
  • 5
  • 29
  • 61