Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
48
rated 0 times [  55] [ 7]  / answers: 1 / hits: 16999  / 11 Years ago, wed, august 28, 2013, 12:00:00

I have this so far:



 <% foreach (Object object in Collection)
{
u<% using (Html.BeginForm(ActionName, Controller, new { FU = bar }, FormMethod.Post, new { ID = MyID}))
{%>
<input type=submit value=Submit />
<%}
} %>

$('#MyID').submit(function() {
var url = Url.Action(ActionName, ControllerName);
var fu = newValueOfFU; // New value for FU
$('#MyID').prop('action', url + ?FU= + fu);
});


I want to change the value of FU with the value from jQuery


More From » jquery

 Answers
30

Simplified Answer.



You are using the incorrect overload. See the overloads list on MSDN for an idea of which to use.



Your current overloads expects routing information as the 3rd parameter. Any values that you provide will be matched against the routes defined for the site. If any parameters do not match it, they will be simply added as get parameters.



What you are trying to achieve is to assign an ID or another attribute to the form. This is done using an overload which allows for html attributes to be defined (see Overload on MSDN. eg,



@using(Html.BeginForm(Action, Controller, FormMethod.POST, new {ID = MyID}))


Note: The order of parameters is Different.



Simply look at the html generated and you will get an idea of what is happening.



// Update



Use this



@using(Html.BeginForm(Action, Controller, new {FU=bar}, FormMethod.POST, new {ID = MyID, onsubmit=return sayHello()}))


This will generate html similar to



 <form action=/Controller/Action?FU=Hello method=post id=MyID onsumbit=return sayHello()>


and combined with script



<script>
function sayHello()
{
alert(Hello);
return true;
}
</script>


will give you alert, as well as sending FU to controller.



// Update
This is how you can update the action attribute of a form.



<script>
$('#MyID').submit(function() {
var url = @Url.Action(Action,Controller);
var fu = newValueOfFU; // New value for FU
$('#MyID').prop('action', url + ?FU= + fu;
return true;
});
</script>

[#76070] Tuesday, August 27, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
neo

Total Points: 117
Total Questions: 100
Total Answers: 95

Location: Albania
Member since Fri, Jan 28, 2022
2 Years ago
;