Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using Web Grid:

HTML
webGrid.Column("", format:
             @<text>
             <a class="edit-user display-mode">
<img src="@Url.Content("~/assets/images/Edit.gif")"></a>
</text>,style: "col1Width" , canSort: false),


On click of this Edit Column i am calling this script to get the ID of the record i want to Edit:

JavaScript
<script type="text/javascript">
        $('.edit-user').on('click',function(){
            var tr = $(this).parents('tr:first');
            var BranchContactID = tr.find("#hdnBranchContactID").val()
            alert(BranchContactID);         
 });
</script>


Now on click of this Edit i want an Action method to be called which will get the details of the record using the id i get from the script and "a modal popup should get displayed."

HTML
@using (Ajax.BeginForm("AddBranchContact", new AjaxOptions { HttpMethod = "POST",  OnSuccess = "$('#myModal1').modal('hide');", UpdateTargetId = "myTable" }))
{
   <div id="myModal1" class="modal fade" role="dialog">
}
Posted
Updated 18-Jan-16 3:30am
v6
Comments
John C Rayan 18-Jan-16 8:53am    
What is your issue? Specify your controller and action method in Ajax.BeginForm.
Abrar Kazi 19-Jan-16 2:14am    
The problem is :

As u see above I am doing something on OnSuccess attribute. But when there is an OnSuccess=false scenario i dont want that modal to hide.
Any suggestions ?

AddBranchContact is my Action Method.

where i am doing this

ModelState.IsActive()
{
do something
}
else
{
return Onsuccess =false Note: How to send Onsuccess as false is also a problem.
}

And if false i dont want Modal to hide as written in Ajax.BeginForm

1 solution

Ajax.BeginForm has got two events for you. OnSuccess and OnFailure. The value for these are call back methods .(event handlers). So what you do in your controller for OnSuccess is wrong. OnSuccess keyword has nothing to do with server side controllers.

What you need to do is create two javascript functions one for OnSuccess and one for OnFailure. The OnFailure function will be called for any exceptions that occur during your JQuery call. OnSuccess will be called if there is no exception occurred.

In your scenario, return a flag to OnSuccess event handler and check the value of flag and accordingly hide or show your modal.

These examples will give you an idea

Using Ajax.BeginForm() with ASP.NET MVC[^]

Ajax modal forms in ASP.Net MVC |[^]
 
Share this answer
 
Comments
Abrar Kazi 19-Jan-16 5:17am    
Thank you so much John. I will definitely try these links and hope i'll solve my problem.
Abrar Kazi 19-Jan-16 5:19am    
As u said :


In your scenario, return a flag to OnSuccess event handler and check the value of flag and accordingly hide or show your modal.


Can u tell me how to return flag from controller to Onsuccess event handler
John C Rayan 19-Jan-16 5:46am    
Like this

public ActionResult Something()
{
return Json(new { result = 0, message = "Testing" });
}

...

new AjaxOptions { HttpMethod = "POST", OnSuccess= "something" }

...

function something(data) {
switch(data.result)
{
case 1:
alert(data.result)
break;
case 0:
alert(data.result)
break;
case -1:
alert(data.result)
break;
default:
alert(data.message);
}
}
Abrar Kazi 19-Jan-16 6:11am    
Thank you very much. Solved.
Abrar Kazi 19-Jan-16 7:51am    
As you said I returned result=0 and caught that in OnSuccess='script name'.
In the script,
if result = 0 i am not hiding the modal but the Fields do not show the validation messages..


Please see this:

<div class="col-md-6">
@Html.LabelFor(m => m.BranchContactDetails.FirstName)
<div >
@Html.EditorFor(m => m.BranchContactDetails.FirstName, new { htmlAttribute = new { @class = "form-control" } })
@Html.ValidationMessageFor(m => m.BranchContactDetails.FirstName, "", new { @class = "text-danger" })

</div>
</div>

if first name is not filled than ModelState.Isvalid = false so it will return:

return Json(new { result = 0 });

the modal will be as it is but the validation messages are not displayed

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900