Yure Yure - 3 months ago 22
ASP.NET (C#) Question

How to get the value of label in cshtml

I´m working with mvc3 asp.net project. And as title above how to get the value of a label from .cshtml

This is how I make my label and it has a value of date today

<label>@DateTime.Now.ToString("MM/dd/yyyy")</label>
.

I tried this one
<label id= "ID">@DateTime.Now.ToString("MM/dd/yyyy")</label>
but it return a
"(no value or empty)"
value. Any suggestion will accept thanks.

I want to get the value from .cshtml label to javascript/js

Here is the output if I use alert using with
ID


UPDATE

Here is the button where I want to get the value of the said label

$("#btnCaptureIn1").click(function (event) {
var date = new Date();
var hours = date.getHours();
var minushrs = 12;
var hrs = hours - minushrs;
var minutes = date.getMinutes();
var today = hrs + ":" + minutes;
document.getElementById('FAIn1').value = today;
var FADATED = $("#ID").val();
alert(FADATED);
var faid = $("#hidFieldAssignmentId").val();
var ans = confirm("Are you sure you want to save?");

if (ans) {
$.ajax({
type: "POST",
url: '../Request/CaptureTimeIn1?FAID=' + faid + '&faDate=' + FADATED,
dataType: 'json',
success: function (response) {
alert("Successfully saved!");
$("#FAIn1").val('');
$("#FiTATimeList").trigger("reloadGrid");
},
error: function (reponse) { }
});
} else {
$("#FAIn1").val('');
}
$("#FAIn1").val('');
});

Answer

This should work

<label id='lblWhatever'>text</label>


$('#lblWhatever').text();
Comments