Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从JSON中获取值并在文本框中显示?_C#_Jquery_Json_Ajax_Asp.net Mvc - Fatal编程技术网

C# 如何从JSON中获取值并在文本框中显示?

C# 如何从JSON中获取值并在文本框中显示?,c#,jquery,json,ajax,asp.net-mvc,C#,Jquery,Json,Ajax,Asp.net Mvc,我想解析JSON类型数据中的值,并将其显示在文本框中。我已设法将json数据从控制器传递到视图。但我无法在我的视图中的文本框中显示它。我尝试使用alert显示该值,但它仅显示[object][object] 查看 <div class="form-group"> @Html.LabelFor( model => model.StudentRegNo, htmlAttributes: new { @class = "control-label col-md

我想解析JSON类型数据中的值,并将其显示在文本框中。我已设法将json数据从控制器传递到视图。但我无法在我的视图中的文本框中显示它。我尝试使用alert显示该值,但它仅显示[object][object]

查看

    <div class="form-group">
        @Html.LabelFor( model => model.StudentRegNo, htmlAttributes: new { @class = "control-label col-md-2" } )
        <div class="col-md-10">
            @Html.DropDownListFor( model => model.StudentRegNo, new SelectList( ViewBag.StudentListForDDL, "Value", "Text" ), "--SELECT--", new { @class = "form-control", @id = "studentReg" } )
            @Html.ValidationMessageFor( model => model.StudentRegNo, "", new { @class = "text-danger" } )
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor( model => model.StudentName, htmlAttributes: new { @class = "control-label col-md-2" } )
        <div class="col-md-10">
            @Html.TextBoxFor( model => model.StudentName, new { @class = "form-control", @id = "StudentName" } )
        </div>
    </div>
@section Scripts {
<script src="~/Scripts/jquery-2.2.0.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
@*@Scripts.Render( "~/bundles/jqueryval" )*@
<script>
    $(document).ready(function () {
        $("#studentReg").change(function () {
            var reg = $("#studentReg").val();
            var json = { studentReg: reg };
            $.ajax({
                type: "POST",
                url: '@Url.Action( "GetStudentByRegNo","Result")',
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(json),
                success: function (data) {
                    //$.each(data, function (key, value) {
                    //    //var name = value.StudentName;
                    //    alert(key.StudentName);
                    //});
                    alert(data);
                }
            });
        });
    });
</script>}
我做错了什么


提前感谢。

如果一切正常,请在控制器中使用下面的功能

public JsonResult GetStudentByRegNo(string studentReg) {
            Student objStudent = objResultManager.GetStudentByRegNo(studentReg).FirstOrDefult();
            return Json(objStudent,JsonRequestBehavior.AllowGet);
        }
然后在视图中通过

data.yourStudePropertyName


我正在获取objStudent对象中的所有数据。我只是无法在文本框中显示StudentName属性。FirstOrDefault()出现错误。请解决此问题,感谢您上次的评论数据。YourStudePropertyName
public class Student {
    public string StudentRegNo { get; set; }
    public string StudentName { get; set; }
    public string StudentEmail { get; set; }
    public string StudentContactNo { get; set; }
    public DateTime StudentRegDate { get; set; }
    public string StudentAddress { get; set; }
    public string Department { get; set; }
}
public JsonResult GetStudentByRegNo(string studentReg) {
            Student objStudent = objResultManager.GetStudentByRegNo(studentReg).FirstOrDefult();
            return Json(objStudent,JsonRequestBehavior.AllowGet);
        }