I have this block of code in my view in many places repeatedly:
$.get('@Url.Action("ActionOne", "Home")',
{ masterContractItemId: masterContractItemId },
function(result) {
$('#addSubContractItemModal .modal-body').html(result);
$('#addSubContractItemModal').modal('show');
}).done(function() {
$("#addSubContractItemModal .datepicker").datepicker();
$('#addSubContractItemModal .chzn-select').chosen({ search_contains: true, width: '70px' });
createBootstrapSuccessMessage("Some text here", "#addSubContractItemModal #message");
});
And this:
$.get('@Url.Action("ActionTwo", "Home")',
{ contractItemId: contractItemId },
function(result) {
$('#addContractItemModal .modal-body').html(result);
$('#addContractItemModal').modal('show');
}).done(function() {
$("#addContractItemModal .datepicker").datepicker();
$('#addContractItemModal .chzn-select').chosen({ search_contains: true, width: '70px' });
createBootstrapSuccessMessage("Some another text here", "#addContractItemModal #message");
});
What I want to do is to write one function in which I will pass actionName, parameterName, parameterValue, modalName and messsageText to invoke this function insted of these lines of codes.
Can you please help to do this?
ADD: I wrote the following method:
function RenderModalWindow(actionName, parameterName, parameterValue, modalId, messageText) {
var url = '/Area/Home/' + actionName + '?' + parameterName + '=' + parameterValue;
$.get(url,
function(result) {
$(modalId + ' .modal-body').html(result);
$(modalId).modal('show');
}).done(function() {
$(modalId + " .datepicker").datepicker();
$(modalId + ' .chzn-select').chosen({ search_contains: true, width: '70px' });
createBootstrapSuccessMessage(messageText, modalId + " #message");
});
}
Then I invoke it in the script:
RenderModalWindow("AddCategoryForContractItem", "masterContractItemId", masterContractItemId, "#addSubContractItemModal", "Text message here!");
But got an error: localhost/Area/Home/AddCategoryForContractItem?masterContractItemId=19669 404 (Not Found)
However I have target method in the controller:
public ActionResult AddCategoryForContractItem(int masterContractItemId)