I have a weird case and I wanted your enlightenment. I have two controllers. One Person Controller for general Person use action methods and one Candidate Controller for more specific action methods related to Candidate. I use one partial view that is located under the Person folder in order to be used as generic in case I want to use it in the future for other types of Person. For the time being this partial view uses an Ajax.BeginForm targeting the Candidate Controller. The syntax I am using is
@using (Ajax.BeginForm("SaveCandidateLanguage", "Candidate",
new AjaxOptions
{
HttpMethod = "Post",
OnBegin = "onBeginFormValidation",
OnSuccess = "onSaveCandidateLanguageSuccess"
}))
{
// form input elements
}
This type of Ajax.BeginForm works correctly despite of the fact that it targets an action in a different controller. Now for my form validation I had to put some more arguments to my Ajax.BeginForm. My new syntax is like that:
@using (Ajax.BeginForm("SaveCandidateLanguage", "Candidate",
new AjaxOptions
{
HttpMethod = "Post",
OnBegin = "onBeginFormValidation",
OnSuccess = "onSaveCandidateLanguageSuccess"
},
new
{
id = "addEditCandidateLanguageForm",
novalidate = "novalidate"
}))
{
// form input elements
}
For some reason this way can't find the Action method. If I put my action inside the Person Controller it works correctly again. However I was wondering why is that case. I did some digging but I didn't manage to get an answer about that.
From firebug I see that the url the browser tries to post is for some reason
http://{ProjectName}/Person/SaveCandidateLanguage?Length=9
instead of
http://{ProjectName}/Candidate/SaveCandidateLanguage?Length=9
and naturally I get a 404 Not found response. I was also wondering what is the variable ?Length=9 that I see at the end of the url and where does it come from.
Ajax.BeginForm
has 11 differents declarations.
In your first case, it works because you use this one :
Ajax.BeginForm(string actionName, string controllerName, AjaxOptions options)
But the second case you use this one, trying to put a string in the object routeValues
parameter :
Ajax.BeginForm(string actionName, object routeValues, AjaxOptions options, object htmlAttributes)
Finally, the declaration you want to use is :
Ajax.BeginForm(string actionName, string controllerName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes)
implemented with a null
value, like :
Ajax.BeginForm("SaveCandidateLanguage", "Candidate", null,
new AjaxOptions
{
HttpMethod = "Post",
OnBegin = "onBeginFormValidation",
OnSuccess = "onSaveCandidateLanguageSuccess"
},
new
{
id = "addEditCandidateLanguageForm",
novalidate = "novalidate"
}))
And the reason for ?length="9"
is because "Candidate" contains 9 characters and length is the only property of string
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With