Big.Child
Big.Child

Reputation: 3098

Call ActionResult from Ajax.BeginForm

I using Ajax begin form and when I click submit button, post method doesn't call, here is code:

 @using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "personListDivforReturnPerson"}))
    {        
        <div class="ReturnPersonGeneralPageBody">
            <div class="returnPersonHeader">
                საზღვრის კვეთისას დაფიქსირებული მონაცემები
            </div>
            <div class="fieldNameForMIA">
                <span>@Html.LabelFor(model => model.LastName, "გვარი")
                    <br />
                    @Html.EditorFor(model => model.LastName)
                 </span>
                <div class="fieldNameInnerForMIA">
                    <span>@Html.LabelFor(model => model.FirstName, "სახელი")
                        <br />
                        @Html.EditorFor(model => model.FirstName)
                       </span>
                </div>
            </div>
            <div class="fieldNameForMIA">
                <span>@Html.LabelFor(model => model.PersonalNo, "პირადი ნომერი")
                    <br />
                    @Html.EditorFor(model => model.PersonalNo)
                   </span>
                <div class="fieldNameInnerForMIA">
                    <span>@Html.LabelFor(model => model.DateOfBirth, "დაბადების თარიღი")
                        <br />
                        @Html.EditorFor(model => model.DateOfBirth)
                      </span>
                </div>
            </div>
            <div class="fieldNameForReturnCheckBox">
                @Html.LabelFor(model => model.IsIdentified, "სხვა სახელით დაბრუნდა")
                @Html.CheckBoxFor(model => model.IsIdentified)              
            </div>
            <div class="saveReturnPerson">
                <input type="image" name="submit" id="submit" src="/Content/Resources/SaveGeo.gif" />
            </div>
        </div>
    }

and here is post method which is never called:

        [HttpPost]
        public ActionResult EditReturnPerson(int id, FormCollection collection)
        {  ....

but this method is called when first is loaded:

 public ActionResult EditReturnPerson(long parentObjectId, int parentObjectTypeId, bool readOnly = false)
        {
....

Upvotes: 2

Views: 1024

Answers (3)

Big.Child
Big.Child

Reputation: 3098

I found my problem, the problem was [HttpPost] public ActionResult EditReturnPerson(int id, FormCollection collection) { .... in this part, id was int and my id in DB was bigint, so I changed int to long in controller and everything worked, thanks for advice.

Upvotes: 1

karaxuna
karaxuna

Reputation: 26940

ბიჭო აქ ვერ გეტყვიან ვერაფერს :D გეუბნები სერვერს მიმართავს და იქ უშლის რაღაცა ხელს. კლიენტის მხარეს ყველაფერი კარგადაა. ეს კიდე მაგარი კაცია, ფორმას უკეთებ სუბმით-ს და default მეთოდი არის პოსტი მაგ დროს.

Upvotes: 1

Shyju
Shyju

Reputation: 218852

Mention the method as POST in AjaxOptions

new AjaxOptions { UpdateTargetId = "personListDivforReturnPerson", 
                                                         HttpMethod ="POST"}

If you do not mention, It will take the Default which is GET.

EDIT : Also If you want to handle it with your own code, You can do it with handwritten JavaScript. Pretty clean and full control.

Get rid of the AjaxBeginForm and use a normal form in your view.

@using(Html.BeginForm())
{
  @Html.EditorFor(model => model.FirstName)
  @Html.EditorFor(model => model.PersonalNo)
  <input type="submit" class="ajaxSubmit" />
}

Now have some javascript to handle the form submit

<script type="text/javascript">
 $(function(){

   $(".ajaxSubmit").click(function(e){
        e.preventDefault();
        var item=$(this);
        $.post("@Url.Action("EditReturnPerson","YourControllerName")",
                         item.closest("form").serialize(), function(response){

                    $("#personListDivforReturnPerson").html(response);
        });              
   });

 });

</script>

Upvotes: 2

Related Questions