0

I am working on Location Detector Module of the Project

Detecting latitude and longitude using jQuery, there is a button that submit the ajax form to the action and that action return partial view.

How can I send auto parameter to the action and get the partial view

I used

document.getElementById("fmDis").submit();

but it creates ?length=4 in url

I am using ajax form with hidden values

using (Ajax.BeginForm("DistanceFound", "Home", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "partial" }, new { id = "fmDis" }))
{
    <input type="hidden" id="lat" name="lat" />
    <input type="hidden" id="lon" name="lon" />
    <input type="submit" />
}
Nalaka526
  • 11,278
  • 21
  • 82
  • 116

1 Answers1

2

You using the wrong overload of Ajax.BeginForm where the second parameter ("Home") is being added as a route value. It needs to be this one

using (Ajax.BeginForm("DistanceFound", "Home", null, new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "partial" }, new { id = "fmDis" }))

Note your hidden inputs do not have a value attribute, so I assume you are setting their values in a script before you submit the form

  • problem is when i open the page and it will show button click on button then form is submit but i need it auto with out button – Usman Aslam Sep 07 '15 at 06:29
  • Sorry, I don't understand your comment. - My answer was addressing why you are getting `?length=4` –  Sep 07 '15 at 06:32
  • now as you said i used null value now my partial view is open in new windows – Usman Aslam Sep 07 '15 at 06:33
  • Thats because you have not included `jquery.unobtrusive-ajax.js` (or have included the scripts in the wrong order or have duplicated them) –  Sep 07 '15 at 06:34
  • @RenderSection("script", required: false) here is order of the jqueery – Usman Aslam Sep 07 '15 at 06:49
  • my script comes in render section scripts – Usman Aslam Sep 07 '15 at 06:50
  • That looks fine (assuming you have no duplicates), but from your comments are you trying to submit the form without actually clicking on the button? - in which case you can give the button a `id="submit"` and use `$('#submit').trigger('click');` –  Sep 07 '15 at 06:54
  • You could put the button inside a `
    ` with `style="display: none;"`. But we are getting of track (the question was about the url) and I have no idea what you scripts are so you should ask a new question explaining the issue and including the relevant code.
    –  Sep 07 '15 at 07:07
  • But really, there is no need at all for `AjaxBeginForm()` - you can just use ajax to post the form and update the DOM with the partial view the method returns in the success call back (using `jquery.unobtrusive-ajax.min.js` is just unnecessary extra overhead) –  Sep 07 '15 at 07:09