Reputation: 33
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!
Upvotes: 0
Views: 630
Reputation: 755
You have multiple options here based on the approach you are planning to take, if the update needs to occur asynchronously (update happens unobtrusively, sometimes you might want to have a loading gif until request is successful), then you will need to create an ActionResult
that calls Report.Update()
. This ActionResult
will be called with AJAX, from your view.
Alternatively, if you do not require an asynchronous solution. You can just create an ActionResult
that calls Report.Update()
but then call RedirectToAction
at the end to load up the page again with the same logic as before.
Let me know what your requirement is and I will elaborate more on the design of your solution.
Upvotes: 1
Reputation: 23
Please create an Action method and then create an Ajax call from view. This is a right approach.
Upvotes: 0
Reputation: 691
you have to understand first how MVC is work. In this approach you just put your server method into scripts which is execute at run time on browser and browser not allow to run server side method directly.
you have to send request to server like if script then ajax request or directly call partial view.
Upvotes: 0