c# - 如何在 jquery 变量中获取业务类属性值

标签 c# asp.net .net

我需要为 java 脚本变量获取业务类属性值。我正在使用该值进行一些操作。在这里,我尝试在 aspx 页面中创建对象和字符串,但无法在 Java 脚本中访问字符串“val”。如何访问它?

Aspx 文件:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<% WebApplication1.Business obj = new WebApplication1.Business(); %>
<% string val = obj.Name; %>
<head runat="server">
    <title></title>
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.min.js"></script>
</head>
<body>
<script type="text/javascript">
        $(document).ready(function () {
        $("#btnSubmit").click(function(){
        alert("welcome");
            var valProperty = <%= val%>;
            alert("Val Property is :   "+ valProperty);
            $("#txtName").val(valProperty);
           });
        });
    </script>
    <form id="form1" runat="server">
    <div>
    <input type="text" id="txtName" />
    <br />
    <input type="button" id="btnSubmit" value="Get Server Property Value" />
    </div>
    </form>
</body>
</html>

类文件:

 public class Business
 {
    public string Name { get; set; }
    public string Money { get; set; }
 }

最佳答案

我认为你访问它很好,你只是没有设置 obj.Name

<% WebApplication1.Business obj = new WebApplication1.Business(); %>
// assign something to obj.Name here
<% string val = obj.Name; %>

关于c# - 如何在 jquery 变量中获取业务类属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17809589/

相关文章:

.net - 在 .NET 中实现数据绑定(bind)的线程安全集合

.net - 如何使用 Entity Framework 代码忽略 DbUpdateConcurrencyException?

c# - 在多线程环境中引发事件

c# - 十进制数字逗号样式

c# - 如何检测文件是否使用 C# 的 silverlight saveDialog 打开?

c# - OO 风格 - 简单问题

c# - ASP.NET MVC 项目构建在 travis-ci 上开始失败

c# - 使用 PadRight 方法填充字符串

jquery - 如何在Asp.net GridView 中添加Jquery Ui日期选择器控件?

javascript - Chartjs 无法在 IE8 中工作,即使包含 excanvas 后也会出现 Canvas 元素问题