开发者

MVC asp.net, get data from textbox into javascript function in view

any thoughts on this?

I'm using MVC2 in VS2010. I have javascript in my view to put markers on a google map. I need to get the address and distance from text boxes,

<%= Html.TextBox("address") %> <%= Html.TextBox("distance") %>

to use as arguments in the function drawMap(address, distance); in my code.

Is there a register object I can use to access this? Also, having some issues using <%= Html. (..) %> in the <script type="text/javascript"><script> flags. I realise I should probably be implementing the MVC model here, and although use this to populate the textboxes on loading, can't pull the information from a textbox back out again on the POST back, to get values in the javascript function in the view.

Any suggestions greatly received! Thanks in advance.

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // further question re: DropDownListFor<>

//////////////////////////////////////////////////////////////////////////////////////////////开发者_如何学运维/////////////////////////////////////////////////////////////////

Is there also a simple way of getting information from:

<%: Html.DropDownListFor(model => model.SavedAddress1, ViewData["StartLoc"] as SelectList) %>

The SelectList for this is held in the Controller. Would I get which one had been selected from the controller in the POST? Or is there a way of determining which had been selected directly in the Javascript.

Many thanks


To get the textbox value just use javascript

document.getElementById("address").value 

or, with jquery, use

$("#address").val();

If you instead use a model passed to the view (which I prefer if the user don't have to change the value in the textbox) you can put that in the script

<script type="text/javascript">
drawmap('<%= Model.Address %>', '<%= Model.Destination%>');
<script>

To have this value persisted in the form post, you can use the Html.HiddenFor tag

 <%: Html.HiddenFor(model => model.Address) %>

Update to your edit

For the dropdownlist, if you put it in the form tag, it will be binded to the model, so you will get it in the controller. While with javascript you can access the value the same way yuo used for the textbox.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜