C# MVC pass parameters from View to Controller using dotnetfiddle

Kaido

I am learning C# MVC using dotnetfiddle web to write code. I have created a new project with the default code of dotnetfiddle for MVC type, but I want to display the question in the alert instead of the answer.

I want to know how to pass parameters to the controller from the view.

This is the ajax method:

$.ajax({
    url: '@Url.RouteUrl(new{ action="GetAnswer", controller="Home"})',
    data: JSON.stringify({Answer: '', Question: $('#Question').val()}),
    type: 'POST',
    dataType: 'json',
    contentType: "application/json; charset=utf-8",
    success: function(resp) {
        openAlert(resp.result);
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) { 
            openAlert("ERROR: " + errorThrown); 
    }
});

And this is the controller method:

[HttpPost]
public JsonResult GetAnswer(string question)
{               
    int index = _rnd.Next(_db.Count);
    var answer = _db[index];
    return Json(new { result = question});
}

You can check and test the code here: dotnetfiddle (Edit: it will be a good answer if it works here)

Muhammad Asad

These code changes will work for you. If you wanna verify check the fiddle. DotNetFiddle

Client side Changes

                $.ajax({
                    url: '@Url.RouteUrl(new{ action="GetAnswer", controller="Home"})',
                    data: {"Answer": '', "Question": $('#Question').val()},
                        type: 'POST',
                        success: function(resp) {
                            openAlert(resp.Question);
                        },
                        error: function(XMLHttpRequest, textStatus, errorThrown) { 
                            openAlert("ERROR: " + errorThrown); 
                        }
                    });

Server Side Changes

[HttpPost]
        public JsonResult GetAnswer(SampleViewModel model)
        {               
            int index = _rnd.Next(_db.Count);
            var answer = _db[index];
            return Json(model);
        }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Pass parameters to a controller from the View by using ajax and controller pass the new eloquent to the view

Can I pass a value from a button in a view to the controller using C# ASP.NET MVC and Razor

MVC 5 pass model from view to controller using actionlink

value not pass from view to controller in mvc

Pass object from view to controller MVC 5

Pass constraint from View to Controller (MVC)

Pass a list from a view to a controller, MVC 5

How to pass multiple jquery datepicker parameters from view to controller in MVC 5?

How do I pass DateTime parameters from View to Controller in ASP.net MVC5?

mvc-pass data from controller to view of other controller

Pass data from Collection View Controller to View Controller using delegates

how to pass Html.HiddenFor from view to view and controller mvc

Pass data from View to Controller using Ajax

How to pass a parameter (that identify the primery key of an object) from the view to a controller using Spring MVC?

Asp.net MVC, Pass Array to Controller From View Without Using Ajax

How to pass the list of objects from view to MVC controller using json.stringify

Pass dropdownlist value from View to Controller in Asp.Net MVC4 without using jquery or javascript?

Pass table Object from view to controller using asp.net mvc

Pass SelectedValue in DropDownList from View Razor to Controller using asp.net mvc

ASP-MVC: How to take an object property from a controller and pass it into view using custom html helper?

ASP.Net Mvc How to pass data from View into Controller with a Button using Ajax

How to pass function parameters from Aurelia view to controller?

What are ways for pass parameters from controller after redirect in spring mvc?

Call a controller method with parameters from partial view mvc 5

How to pass data from datepicker in view to controller in mvc

How to pass a variables data from a controller to a view (MVC)

Add item into List from View and pass to Controller in MVC5

ASP.Net MVC How to pass data from view to controller

How to pass row id by href link in mvc from view to controller