AAlferez
AAlferez

Reputation: 1492

MVC button sending parameters from JS

So I have a panel with 3 DDL, 2 TextBoxes, a Cancel button and an Apply button. I want my button work this way: When I click it, I want it to take the data from 3 DDL and 2 TextBoxes and build a model, send it to my controller/function and refresh the gridview .

But the function also has to check there is no duplicate entries.

So if that function returns a partial view, in case the entry I am adding is duplicated, how can I show a message to display the error?

button:

<button id="btnAddUpdateConfig" name="btnAddUpdateConfig" value="Apply" onclick="ValidateValues()">Apply</button>

My problem also comes before that; how can I send the values to the controller function? Is there a way to call a controller method passing values from the button? But that method will have to refresh the gridview if the item is added or show and error if it is not.

If I want to do it from JS, how can I do the same? I just know Ajax.ActionLink and that creates a link when I just want to call a controller method.

Upvotes: 0

Views: 632

Answers (1)

Zaheer Ahmed
Zaheer Ahmed

Reputation: 28528

how can I send the values to the controller function? Is there a way to call a controller method passing values from the button? Use jquery ajax call:

   function ValidateValues(){
    [email protected]("ControllerName","Action",new {param1=value,param2=value=param3=value})
    $.ajax({
    url:actionUrl,
    statusCode: {
    404: function() {
       alert("Data is duplicated");
    }
    }
    });
}

Now you can handle request in your action and if data is duplicate send the following code:

 return new HttpStatusCodeResult(404, "Data is duplicated");

Upvotes: 1

Related Questions