开发者

how to use one field value in multiple forms

开发者 https://www.devze.com 2023-02-21 23:13 出处:网络
It is possible to have 2 separate forms on 1 asp mvc webpage, that using value of 开发者_如何学Cone (listbox) control ?

It is possible to have 2 separate forms on 1 asp mvc webpage, that using value of 开发者_如何学Cone (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.

0

精彩评论

暂无评论...
验证码 换一张
取 消