furiousNoob
furiousNoob

Reputation: 53

How to get value from JSON and show it in a textbox?

I want to parse value from JSON type data and show it in a textbox. I have managed to passed json data from Controller to View. But I just can't show it in a textbox in my View. I have tried to show the value using alert, but it shows only [object][Object]

View

    <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>}

Controller

public class ResultController : Controller {
    private ResultManager objResultManager = new ResultManager();
    // GET: Result
    public ActionResult ViewResult() {
        var studentList = objResultManager.GetListOfStudents();
        List<SelectListItem> studetListItems = new List<SelectListItem>();
        foreach (Student objStudent in studentList) {
            studetListItems.Add(new SelectListItem {Value = objStudent.StudentRegNo, Text = objStudent.StudentRegNo});
        }
        ViewBag.StudentListForDDL = studetListItems;
        return View();
    }
    //GET Student By Registration Number
    public JsonResult GetStudentByRegNo(string studentReg) {
        Student objStudent = objResultManager.GetStudentByRegNo(studentReg);
        return Json(objStudent,JsonRequestBehavior.AllowGet);
    }
}

Model

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; }
}

What am I doing wrong here?

Thanks in advance.

Upvotes: 1

Views: 2971

Answers (1)

Mir Gulam Sarwar
Mir Gulam Sarwar

Reputation: 2648

If everything is fine then use below in your controller

public JsonResult GetStudentByRegNo(string studentReg) {
            Student objStudent = objResultManager.GetStudentByRegNo(studentReg).FirstOrDefult();
            return Json(objStudent,JsonRequestBehavior.AllowGet);
        }

then in View get value by

data.YourStudenPropertyName

Upvotes: 1

Related Questions