I'm using VB.Net and MVC 5. I have a table that I would like to allow the user to assign a salvage date to an item in.
The table displays multiple fields for the item, and the last field is a textbox like this:
<input class="span2" id="salvageDate" name="salvageDate" type="text"
placeholder=#DateTime.Today />
There is a button in the first column of the table like this:
#<td><a class="btn btn-danger btn-sm"
href="ViewDetails/SalvageConfirmation/#currentItem.Unique_Item_ID"
type="submit">Salvage</a></td>
The item id comes through just fine.
I would like to pass the date to my controller and am not able to. My controller looks like this:
<Route("ViewDetails/SalvageConfirmation/{itemID:int}/{salvageDate:datetime}")>
Function SalvageConfirmation(itemID As Integer, salvageDate As Date) As ActionResult
I tried using Html.BeginForm like this:
#Using (Html.BeginForm("SalvageConfirmation", "ViewDetails", "#currentItem.Unique_Item_ID", FormMethod.Post))
#<input class="span2" id="salvageDate" name="salvageDate" type="text"
placeholder=#DateTime.Today />
End Using
but then I end up with a url that looks like this:
ViewDetails/EditSalvage/14/14/ViewDetails/SalvageConfirmation/14
and if I remove the href from the button, nothing happens.
How can I pass the value from the salvageDate textbox, that the user selects to my controller?
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.
In my application, I have search button and based on the search criteria, I have to search data. I have used few hidden variables to store data.
My .gsp page looks like this:
<g:form id="job" name="job" method="POST">
<div>
<input type="hidden" id="country" name="country" value="${country}"/>
<input type="hidden" id="paginationFlag" name="paginationFlag" value="${paginationFlag}"/>
<!-- There are other components like text field, select box etc -->
<g:actionSubmit id="Search" value="Search" formmethod="post" action="findJob"/>
</div>
</g:form>
My respective controller method looks like this:
def findJob(){
def country
def paginationFlag
if(params?.country){
country = params?.country
}else{
country = 'USA'
}
if(params?.paginationFlag){
paginationFlag = params?.paginationFlag
}else{
paginationFlag = 'false'
}
withFormat{
html{
List<Applicant> searchList //get data from database.
// other business logic
render(view : "jobList",model:[paginationFlag: paginationFlag, country:country])
}
json{
// some business logic
def candidateList // value for this candidateList is acquired from database
def json = ['jsn': candidateList]
render json as JSON
}
}
When I click search button and debug the code, first my control goes to controller-> findJob() method and executes the code inside html part.
Secondly, it goes to the gsp page (view page) and sets value.Third, it again goes to the controller and executes the code inside json part.
On the first entry to the controller, the value of paginationFlag and country in param are both null. So it will set the value 'false' and 'USA' respectively. But when control goes to controller again for the second time, again the value of param.paginationFlag and params.country are null. Should not they have the assigned values?
Why is it so? What should I do go get the value of country and paginationFlag in params on the second time? Can any one explain me ? Thank you very much for advance.
The problem is you're using a regular HTML input tag rather than a Grails g:hiddenField tag. Try this:
<g:form id="job" name="job" method="POST">
<div>
<g:hiddenField name="country" value="${country}"/>
<g:hiddenField name="paginationFlag" value="${paginationFlag}"/>
<!-- There are other components like text field, select box etc -->
<g:actionSubmit id="Search" value="Search" formmethod="post" action="findJob"/>
</div>
</g:form>
Tip
You can also simply the params assignment:
def country = params.country ?: 'USA'
def paginationFlag = params.paginationFlag ?: 'false'
I can't tell where the paginationFlag comes from, but know that a boolean is valid.
I have a form which should print questions dynamically. Foo has a Field object and a Field Definition object. I want the Field to have its fieldDefinition assigned by the form. All of the behind the scenes stuff works fine.
The below code works with assigning Strings and Longs in other scenarios.
Here's the line that's causing trouble:
<input th:type="hidden" th:field="*{fields[__${iterationStatus.index}__].fieldDefinition}" th:value="${fooViewModel.fields[__${iterationStatus.index}__].fieldDefinition}"/>
This is what it looks like when it renders in html:
<input type="hidden" value="com.blah.domain.FieldDefinition#fbb2e392" id="fields0.fieldDefinition" name="fields[0].fieldDefinition">
When I submit the form, no controller action is invoked, and the app simply redirects to the error page.
If it's impossible to actually do the assignment that way, please suggest other methods. The only way I came up with is to have Foo use the FieldDefinitionService to do the assignment after being passed an ID. I don't that Domain class to have access to another Domain object's Service.
Thanks
Just an example: when you need to iterate a form inside an element that has a th:each attribute, you can use the following structure (so far, it's the only way that's working for me).
<tr th:each="rank, stat : ${ranks}">
<td th:text="${rank.name}">This is static</td>
<td th:text="${rank.description}">This is static</td>
<td>
<form th:action="#{/user/ranks/delete}" method="post">
<input type="hidden" id="id" name="id" th:value="${rank.id}"></input>
<button class="btn btn-danger" type="submit">
<span>Delete</span>
</button>
</form>
</td>
</tr>
Here ranks is a list of entities that have to be displayed on a table and, for each entity, there is a delete button associated to a form.
The controller method should be similar to the following fragment, the parameter is availabled with the name id:
#RequestMapping(path = "/delete", method = RequestMethod.POST)
public View deleteRank(Model model, #RequestParam(name = "id") String rankId,
#ModelAttribute("user") User user)
{
Long id = Long.parseLong(rankId);
// delete ...
RedirectView redirectView = new RedirectView("/user/ranks");
return redirectView;
}
I have a strongly typed partial view. The model has a property that is a list of users in Active Directory represented by a class called ADUser.
I have a partial view that represents a drop down list for this property. While I use the value of this drop list for some other things, I have no need to submit its value, so I thought I would remove the name attribute. However, once the Html loads, the name attribute is always set to what the Html helper wants to assign it. Is there a way I can remove that attribute so that the drop down's value doesn't submit?
In my main partial view (_adusers is the name of the list):
<%: Html.Partial("ADUserDropDown", Model._adusers)%>
In my drop down's partial view:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<List<MyProject.Models.ADUser>>" %>
<%= Html.DropDownList("", new SelectList(Model, "ValueText", "DisplayText"), new { name = "", size = "12" })%>
<input type=button value="Search..." />
<input type=button value="Close" />
This results in:
<select name="_master" size="12">
_master being the name of the parent model. If I add something to the first parameter of Html.DropDownList, it results in:
<select name="_master.WhateverINamedIt" size="12">
I want to achieve:
<select size="12">
There is no point of using the HTML helper then. You can change the code of the partial view to:
<select size="12">
#foreach(var item in Model)
{
<option value="#item.ValueText">#Html.DisplayFor(m => item.DisplayText)</option>
}
</select>
However if you want to stick with the DropDownList helper the name parameter in the html attributes has to be specified with the capital letter:
<%= Html.DropDownList("", new SelectList(Model, "ValueText", "DisplayText"),
new { Name = string.Empty, size = "12" })%>
As an alternative you can remove the name with jQuery:
$('[name="_master.WhateverINamedIt"]').removeAttr('name');
Don't like or use jQuery, the plain javascript will do the job:
document.getElementsByName('_master.WhateverINamedIt')[0].removeAttribute('name');
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" />