admin管理员组文章数量:1399994
Im working in a MVC .NET application. I assign a value to an element ID like this:
document.getElementById("latbox").value = event.latLng.lat();
and I retrieve it to show it like this:
<p>Latitud: <input size="20" type="text" id="latbox" name="lat" ></p>
The thing is, how can I find this value in my controller? Thanks.
Im working in a MVC .NET application. I assign a value to an element ID like this:
document.getElementById("latbox").value = event.latLng.lat();
and I retrieve it to show it like this:
<p>Latitud: <input size="20" type="text" id="latbox" name="lat" ></p>
The thing is, how can I find this value in my controller? Thanks.
Share Improve this question edited Jul 30, 2015 at 18:54 C1pher 1,9726 gold badges34 silver badges52 bronze badges asked Jul 30, 2015 at 18:46 Karlo A. LópezKarlo A. López 2,6784 gold badges31 silver badges60 bronze badges 4- What are you trying to do exactly? Do you want to send the value of the 'latbox' input box to your controller? Or are you trying to set this value from your controller? You need to provide a more detailed use case – azog Commented Jul 30, 2015 at 18:51
- Sorry if I have explained wrong, I want to GET this value from my controller. Thanks. – Karlo A. López Commented Jul 30, 2015 at 18:53
- You mean, you want to post this value to your controller? – brroshan Commented Jul 30, 2015 at 18:57
- Whatever I need to use the value in the controller, Thanks. – Karlo A. López Commented Jul 30, 2015 at 18:58
3 Answers
Reset to default 3In your view, you can use a <input type='hidden' name='lat'/>
that you assign the value you need. Note that it must match the name of your controller parameter
@using (Html.BeginForm("Action", "Controller", FormMethod.Post))
{
<p>Latitud: <input size="20" type="text" id="latbox" name="lat"></p>
<input type="hidden" name="lat" id="latbox-hidden" />
<input type="submit" value="submit" />
}
<script type="text/javascript">
var lat = document.getElementById("latbox");
lat.value = "foo";
document.getElementById("latbox-hidden").value = lat.value;
alert(document.getElementById("latbox-hidden").value);
</script>
And your controller:
[HttpPost]
public ActionResult Action(string id, string lat) //same name to bind
{
//do your thing
}
HTML fields are posted using the name attribute, which in your case is "lat". On the controller you could have a parameter like this:
string lat
It would get the value of the HTML input element.
You can also use ajax call to send data to action
<head runat="server">
<script src="http://code.jquery./jquery-1.9.1.js"></script>
<script src="http://ajax.googleapis./ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var serviceURL = '/AjaxTest/FirstAjax'; // give path of your action
var param = document.getElementById("latbox");
$.ajax({
type: "POST",
url: serviceURL,
data: param,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: successFunc,
error: errorFunc
});
function successFunc(data, status) {
alert(data);
}
function errorFunc() {
alert('error');
}
});
</script>
本文标签: javascriptRetrieve id element from html in C code MVC NETStack Overflow
版权声明:本文标题:javascript - Retrieve id element from html in C# code MVC .NET? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744237560a2596628.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论