I have created an app in MVC which is integrated into an already existing web application (Relativity). In a specific scenario, I want to update the SQL table for some of the entries. I have created a class action method containing the sql string and execution for the same with the following code.
MyModel.cs
public class Reports
{
public static int Update()
{
Relativity.API.IDBContext eddsDBContext = Relativity.CustomPages.ConnectionHelper.Helper().GetDBContext(-1);
int workspaceArtifactID = Relativity.CustomPages.ConnectionHelper.Helper().GetActiveCaseID();
String sql = "UPDATE [EDDS" + workspaceArtifactID + "].[EDDSDBO].[Reports_Admin] SET BeenRun = 'Yes', ReportDate = getdate() where Report_Type = 'Claim_Incomplete' AND (BeenRun <> 'Yes' or BeenRun is null)";
eddsDBContext.BeginTransaction();
try
{
eddsDBContext.ExecuteNonQuerySQLStatement(sql);
eddsDBContext.CommitTransaction();
}
catch (Exception ex)
{
eddsDBContext.RollbackTransaction();
throw;
}
return 0;
}
}
I want this method to be executed in my view when a click a button to update the tables. I understand that it is a bad practice to call class action methods in the view, but in this scenario, I don't find any other solution. The code for my View is as following:
Reports.cshtml
<body>
<button class="button5" onclick="fnUpdateReport();" style="margin-left:100px;">Update</button>
</body>
<script>
function fnUpdateReport() {
if (SelectedValue == "A") {
@Reports.Update();
}
}
</script>
The method call is tied to a drop down value and the update command in the method should be executed when the button is clicked.
Problem
Right now, the class method (update) is executed when the view is being loaded and not when the button is clicked. It is not working when the button is clicked, and this is an absolute requirement for this application.
If anyone can help me with a solution of how to make the method respond to the button click, that would a really great help. Also, if anyone has any other way which will definitely call the method on button click, please let me know.
Let me know if you all need some more details to help me figure this out. Thanks in advance!