This is my code in SearchResults View for the colours dropdownlist
<td>#Html.DropDownList("colours", TryCast(ViewData("colours"), SelectList),
New With {.onchange = "document.getElementById('wineSearchCriteria').submit();"})</td>
I've set the form name to 'wineSearchCriteria'
<form action="/Wines/SearchResults" method="post" name="wineSearchCriteria"
input type="submit" value="Search"/>
but the form is not posting back when the colours dropdown is changed. I'm sure this will be something simple!
document.getElementById() will only retrieve elements by their ID, but you are not assigning an ID to the form. You are assigning a name to the form. Use the id attribute instead:
<form action="/Wines/SearchResults" method="post" id="wineSearchCriteria"
input type="submit" value="Search"/>
You should set the id attribute as "wineSearchCriteria", not the name.
<form action="/Wines/SearchResults" method="post" id="wineSearchCriteria" />
Related
I have a hidden field like this in my view:
#Html.HiddenFor(m => m.Url)
then i have a form in which i'm posting the data via Ajax call like this:
<form id="formdata">
<input class="btn primary" type="submit" value="Submit" onclick="submitForm('#Url.RouteUrl("createAccount/"+ '#Html.HiddenFor(m => m.Url)'+ )'">
</form>
How do i Pass the url from the hidden form into my submit function. I'm not sure if i have to use double quotes or single quotes around it? or is this achievable?
Thank you
HtmlHelper.HiddenFor used for creating hidden field with specified value assigned to viewmodel binder, instead of rendering its value directly. What you need is using Model.Url property to pass viewmodel property into JS function like this:
<input class="btn primary" type="submit" value="Submit"
onclick="submitForm('#Url.RouteUrl("createAccount/" + Model.Url)')">
In case it returns encoded URL containing query string, use Html.Raw() i.e. Html.Raw(Model.Url).
NB: You can use AjaxHelper.BeginForm to submit a form through AJAX as alternative to handling submit button click event & using jQuery.ajax with passed URL.
I'm trying to post a model back to my controller. I'm not sure why TextBoxFor works and TextBox does not.
#ModelType Models.myModel
#Code
ViewData("Title") = "Foo"
End Code
#Using (Html.BeginForm())
#Html.AntiForgeryToken()
#Html.TextBoxFor(Function(m) m.Value) <--- works
#Html.TextBox(Model.Value) <--- does not work
#Html.TextBox("Value", Model.Value) <--- works! (per answer below)
#<input type="submit" value="Save" />
End Using
Html.TextBoxFor creates right input type="text" with correct name value. You mustn't use it, but it helps in this case. Same result may be achieved even with html code:
<input type="text" name="Value" value="#Model.Value" />
Main point for asp.net mvc model binders is correct names for html controls. Html.TextBox() also does it, using first parameter as html element name, so correct usage of it should be Html.TextBox("Value", Model.Value)
I have two submit buttons which call the same action method. How can I tell which of these buttons was clicked in the formcollection of the action method (without setting the value property of the buttons)?
HTML code for buttons:
<input type="submit" name="button" />
<input type="submit" name="button" />
Action method as:
public ActionResult submitted(FormCollection form)
{
}
i know how to do if we have a value property, but I just want to try like this without value property. How can this be done?
thanks,
michaeld
The best thing to do, is intercept the click action to set a hidden form variable before the form is submitted, e.g.:
<script language="text/javascript">
$("form input[submit]").click(function() {
$("#buttonSelected").val("some unique value here");
});
</script>
Where you might have a hidden input:
<input type="hidden" id="buttonSelected" name="buttonSelected" />
That way, you can then check the specific "buttonSelected" form value to figure out which button was pressed.
I have a page with a input box and a button, when the user clicks the button i want to redirect to a controller action that has as parameter the value of the input box.
<input id="CodProiect" type="text" />
<input id="Cauta" type="button" value="Cauta" onclick="window.location.href='#Url.Action("Cauta", "Componente", new { CodProiect = "param" })';"/>
How can i get the "param" from the input box ?
You could just use a form with a GET method
<form action="#Url.Action("Cauta", "Componente")" method="GET">
<input id="CodProiect" name="CodProiect" type="text" />
<input id="Cauta" type="submit" value="Cauta" />
</form>
The form will add the parameter as part of the query string of the URL e.g. www.yoursite.com/Cauta/Componente?CodProiect=user+entered+value
Value of the Action is prepared at server side and sent to the browser so you cannot have the value at the server when it is a user input.
You can use jquery to change the URL at client side.
Also passing state in an PRG scenario is a common problem in ASP NET MVC. You can either:
Store it temporarily in session
Pass it as a parameter in URL
Use a form.
Form:
<form action="Componente/Cauta">
<input id="CodProiect" type="text" />
<input id="Cauta" type="submit" value="Cauta" />
</form>
Controller:
public ActionResult Cauta(string CodProiect)
{
//Do some stuff
}
More info: http://weblogs.asp.net/scottgu/archive/2008/09/02/asp-net-mvc-preview-5-and-form-posting-scenarios.aspx
Syntax may be outdated, but you get the point...
I have 2 forms in my myPage.chtml page as follows:
#using (Html.BeginForm("Tests1", "Test", FormMethod.Post, new { id = "FormSearch1" }))
{
<input type ="submit" value="Filter1 " id="submit" />
}
and another form as follows:
#using (Html.BeginForm("Tests2", "Test", FormMethod.Post, new { id = "FormSearch2" }))
{
<input type ="submit" value="Filter2 " id="submit" />
}
However, I am having the error message and it`s not working upin clicking the submit buttons. What am I missing?
Warning 2 Another object on this page already uses ID 'submit'.
In your example, you have two <input> elements that have the same id="submit" attribute. id should be unique on each HTML page. To solve the problem, either remove the id attribute completely (if it is not actually used), or use different values.
If you need to have these submit buttons to have the same name but different values (considering the different Actions and ids submit in your example, I doubt that), you could try to use <input type="submit" value="Filter1" name="submit"/> and <input type="submit" value="Filter2" name="submit"/> instead.
Simple
You have 2 <input> tags with the same id submit.