Yure Yure - 1 year ago 109
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


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


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();
var faid = $("#hidFieldAssignmentId").val();
var ans = confirm("Are you sure you want to save?");

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

Answer Source

This should work

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download