how to use one field value in multiple forms - asp.net

It is possible to have 2 separate forms on 1 asp mvc webpage, that using value of one (listbox) control ?
e.g.
using(beginForm) {
//form 1
ListBox("name")
//submit etc.
}
using(beginForm) {
//form 2
//submit2 etc.
}
that form 2 knows value of ListBox named "name" placed in first form ?

Nope - you can only send over what is inside that form without some trickery. You could use jQuery to hook into the form post and set the value dynamically based on what is in the other form.
This link describes how to hook the jQuery submit.
http://hasin.wordpress.com/2009/10/01/jqueryhooking-form-submit-and-making-it-an-ajax-request/
In your function you can then synchronize the values - the easiest way is to probably keep this field OUTSIDE of either form, and include one inside of each form. You only change the one outside of the form, and then on submit you would set each form field as such:
$('#txtForm1Field').val($('#txtPlaceHolder').val())
$('#txtForm2Field').val($('#txtPlaceHolder').val())
if you want them named the same thing you will need to select based on a form id first
Also remember you cannot embed forms within each other either.
There are other ways with jQuery (such as via ajax request just insert the value in the form data being posted) but you don't have anything 'by default' with html that would allow this because of the scope of each form.

If you can call the same view (also pass the model) from the post function of the first form then you'll get the data from from the list in the model. A better option would be to create a partial view.
As far as I feel if you pass the model while returning to the view then you'll get the data you require.

Related

Many forms for editing entity on one page

I have a list of objects on my page. I need to edit an object in a popup.
There are many objects, and generating many forms for each object is not correct.
What can I do, can an iframe do in a popup?
You don't need to use iframe.
Build your form in your controller and render it in the html. The fact that it's in popup doesn't change anything. It doesn't matter if it's in popup or not, the final result will be POST call to your action.
You should have an action that renders a form, callable from an ajax in the view where you have all those entities.
Just change the entity id that will be received in the action as argument by changing the ajax url using js depending on the clicked entity you want to edit.
Then return with the ajax the form already rendered so with only one form you can edit as many entities (of the same class) as you want, without even need to render one form before they click which one they want to edit.

Gravity Forms Multipage Form, dynamically populate from existing form entry based on unique key

I'm working with Gravity Forms (WordPress) on a pair of forms for a school. They have an initial form for applications. Each submission gets assigned a unique five character key to identify it. This is saved in a hidden field. This form has things like name, email, phone, address etc. This form is up and running, working great.
Once a student has been accepted, we now need to have another form that is for registrations. This form will be a multipage form with more detail and various questions about history/parents etc. My thought is that the first page contains a single field that asks for the key assigned to their application. Once this first page is submitted, the form would query the database based on the key and return any info from the original application that applies to the second form. So things like name, email, phone wouldn't have to be re-entered manually.
I'm starting down the road of dynamic population and running into some trouble. It appears things operate differently on multi-page forms, and depending on whether you're using AJAX or not. Has anyone had experience doing this kind of key lookup for form population? I could do this in vanilla PHP/MySQL without a problem, but the Gravity Forms stuff has me stumped.
Your question is not clear enough and is left open for interpretation. However, since I ran into problems with multi-page gravitiy forms as well, and could find very little help online, here is my experience.
If you go from one page to the next, you can access field values from previous pages using rgpost("input_" . YOUR_FIELD_ID);. Checkbox collections are accessed by "input_".FIELD_ID."_".OPTION_INDEX. Dynamically filled dropdowns seemed to get reset in my experience. If this happens to you, before moving to the next page, you can copy any values from these fields into hidden fields, and read from the hidden fields.
Fields that already have a default value (field->defaultValue) are NOT updated even though the appropriate hooks are triggered. E.g: add_filter('gform_field_value_my_field', 'populate_your_field'). A dump of the value you return in populate_your_field will show a different value than when you inspect my_field, which is supposed to receive this value. my_field will retain the first value it received and wont change anymore.
This is a problem when your fields depend on input of fields from previous pages, and users go back and forth changing those. Your dependend fields wont update on any hook. I've tested extensively with gform_field_value, gform_pre_render, gform_field_validation and gform_post_paging.
I found one solution: echo a hidden div with data attributes in the gform_pre_render hook, and populate the fields through javascript.
add_filter('gform_pre_render', 'dynamic_population', 10, 1);
function dynamic_population($form) {
echo "<div id='my_data' style='display:none' data-myjsonarray='.json_encode(my_json_array).'></div>";
}
In your gravity form, you can create a html field and add javascript in there.
<script>
window.onload = function() {
//custom function that fills given gform dropdown with given array
fill_dropdown(
jQuery('#input_1_1'),
jQuery('#my_data').data('myjsonarray')
);
}
</script>
Hope this somehow helps someone.
Gravity Forms Populate Anything can handle this really well.
Add a field to your form where the user would enter their reference number.
Configure a field to be populated with the entry data based on the provided reference ID where it matches an entry that has the same reference ID:

Add searchable textbox with typeahead to form

I have a form in an asp.net mvc view.
The form contains a textbox which is used to edit the username property of my model.
I want to add the bootstraps typeahead to this textbox searching the users while typing.
As far as I know the typeahead should post a form, but I can not put a form inside a form (since I want to use the typeaheads search result in form postback).
How can I solve it? Can it be done without using Ajax?
EDIT:
It's important that the selected username influences the content of the form. So somehow I have to post the typeahead selection, which in the server modifies the model and the result is an updated form. After that the user fills the remained fields than posts the form.
Typeahead can work on any <input type="text">, you should inspect the element in the examples. When you do you will notice that the input has a data binding called data-source which is just an array of strings. It then filters those strings whenever you type.
You could certainly populate that array via ajax. You might want to find some way to disassociate that <input> from the form though. Although it's probably not a big deal breaker.

pass variables from one asp(classic asp) page to other asp page without using parameters in URL

I need to pass variables from one ASP(Classic ASP) to another ASP page. I am using javascript (window.open()) to open the second ASP page. I should not pass the variables in the URL like window.open('second.asp?first_name=sdf&last_name=asdas');
I have number of buttons in first ASP page,on click different buttons-different pages will get opened thro java script functions. So, I cant use "POST" method all the time.
Please let me know the ways of passing variables from one page to other page with out passing explicitely in the URL.
One thought m having is : using sessions/hidden/viewstate variables.
Thanks in Advance
Rupa
I think the solution can be founnd be working round the assumption regarding "POST". You could modify the containing HTML form elements Action property in the buttons onclick. Have an expando attribute attached to the button which contains the real action value:-
btn_onclick() { this.form.action = this.getAttribute("action"); }
Now you can give each input type="submit" its own action attribute and use onclick="btn_onclick".

ASP MVC form fields wrongly getting filled in

I have an ASP MVC form which updates a business object and then its action goes to another page.
Both of these pages have a textarea with an id "Description", when it goes to the other page, for reasons unknown it fills in the values which were entered before the form submission.
Which method are you using to update your object? Have you tried adding the object type name in the ID?
Like, Product.Description instead of just Description.
Not knowing what your doing exactly, I'd say there is a 'Description' property on the ViewData's Model. ASP.NET MVC will try to match up your form values with values from the ViewData, including the Model. You can rename the control or reference the object directly as Chad said. eg Product.Description
With more details, someone may be able to help more.
Without seeing your controller actions this is a bit of guess work but how does it go to another page?
If in the post controller you return a different view (or return another action that returns a view etc) it may be that the ModelState is using the attempted values from the previous form submission, this is the expected behaviour (it's how the validation system and model binding works for a start). If you are sending them to a different form use a RedirectResult to your next action and use TempData if needed for any state you need to keep while transitioning.
If at all possible I'd also recommend using the strongly typed helpers such as Html.TextAreaFor(x=>x.Description)
<p><label>Description</label><br /><br /><%=Html.TextArea("Description", new { style = "width:470px;" })%></p>
For clarification, I'm going from an "Edit project" page, to a "Add issue" page

Resources