首页 > 解决方案 > json函数返回值但值未显示在mvc的文本框中

问题描述

您好,我想在单击编辑按钮时填充文本,但值未填写在文本框中,当我调试此代码时,json 函数返回值以及在 javascript 代码中显示的值,但是当弹出模型显示时,值不显示在文本框中。任何人都告诉我哪里错了,这段代码有什么问题。

json 函数

#region Select Requisition Main Select
        public JsonResult mRequisitionmain_Select(Int64 Req_ID)
        {

            try
            {
                Requisition mReq = new Requisition();


                con = new SqlConnection(constring);
                SqlCommand cmd = new SqlCommand("Select Req_ID,ReqNo,Comp_ID, GL_Year,ReqDate,Job,ApprovedBy,UserName, IsApproved from RequisitionMain where Req_ID='" + Req_ID + "'", con);
                cmd.CommandType = CommandType.Text;
                con.Open();

                DataTable mDT = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(mDT);

                mReq.Req_ID = mDT.Rows[0]["Req_ID"].ToString();
                mReq.ReqNo = mDT.Rows[0]["ReqNo"].ToString();
                mReq.Comp_ID = 1;
                mReq.GL_Year = "2018-2019";
                mReq.ReqDate = mDT.Rows[0]["ReqDate"].ToString();
                mReq.Job = mDT.Rows[0]["Job"].ToString();
                mReq.Approvedby = mDT.Rows[0]["ApprovedBy"].ToString();
                mReq.Username = Session["AgentName"].ToString();
                mReq.IsApproved = Convert.ToBoolean(mDT.Rows[0]["IsApproved"]);


                con.Close();
                return Json(mReq, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

javascript

function getbyID(Req_ID) {

            $.ajax({
                url: "/Home/mRequisitionmain_Select?Req_ID=" + Req_ID,
                type: "GET",
                contentType: "application/json;charset=UTF-8",
                dataType: "json",
                success: function (result) {
                    $('#Req_ID').val(result.Req_ID);
                    $('#ReqNo').val(result.ReqNo);
                    $('#ReqDate').val(result.ReqDate);
                    $('#Job').val(result.Job);
                    $('#Approvedby').val(result.Approvedby);
                    $('#IsApproved').val(result.IsApproved);
                    $('#updateModel').modal('show');
                    $('#UpdateReq').show();

                },
                error: function (errormessage) {
                    alert(errormessage.responseText);
                }
            });
            return false;
        }

HTML

 <table id="example1" class="table table-bordered table-striped">
                                                <thead>
                                                    <tr id="lblhead">

                                                        <th>Requisition Date</th>
                                                        <th>Job</th>
                                                        <th>Approved By</th>
                                                        <th>Action</th>

                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    @foreach (var item in Model.items)
                                                    {
                                                        <tr>


                                                            <td style="text-align:left; width:150px;">@Html.DisplayFor(modelItem => item.ReqDate)</td>
                                                            <td>@Html.DisplayFor(modelItem => item.Job)</td>
                                                            <td style="text-align:left; width:150px;">@Html.DisplayFor(modelItem => item.Approvedby)</td>


                                                            <td style="text-align:center; width:40px;">
                                                                <a style='color:blue; text-decoration: underline; font-weight: bold;' href='@Url.Action("RequisitionDetail", "Home",new { item.Req_ID })'> View</a> |
                                                                <a data-toggle="modal" data-target="#updateModel" onclick="getbyID(@item.Req_ID)">

                                                                    <i class="fa fa-edit"></i>
                                                                </a>
                                                            </td>
                                                        </tr>
                                                    }
                                                </tbody>
                                                <tfoot>
                                                </tfoot>
                                            </table> 


  <div class="modal fade" id="updateModel" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static" data-keyboard="false">
            <div class="modal-dialog modal-lg modal-notify modal-info" role="document">
                <!--Content-->

                <div class="modal-content" style="width:140%">
                    <!--Header-->
                    <div class="modal-header">
                        <p class="heading lead">Update Requisition</p>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true" class="white-text">&times;</span>
                        </button>
                    </div>
                    <!--Body-->
                    <form id="NewOrderForm">
                        <div class="modal-body">
                            <div class="form-row">
                                <div class="col" id="Req_ID">
                                    <!-- Requisition Req_ID -->
                                    <div class="md-form">
                                        @Html.TextBoxFor(m=>m.Req_ID, new { @class = "form-control mr-sm-3", @id = "ReqID", Required = true })
                                    </div>
                                </div>
                                <div class="col">
                                    <!-- Requisition Req_NO -->
                                    <div class="md-form">
                                        @Html.TextBoxFor(m=>m.ReqNo, new { @class = "form-control mr-sm-3", @id = "ReqNo", Required = true })

                                        <label for="lblRequisition">Requisition No.</label>
                                    </div>
                                </div>

                                <div class="col">
                                    <!-- Requisition Date -->
                                    <div class="md-form">
                                        @Html.TextBoxFor(m => m.ReqDate, new { @class = "form-control", @id = "ReqDate", Required = true })

                                        <label for="lblRequisitionDatepicker">Requisition Date</label>
                                    </div>
                                </div>
                                <div class="col">
                                    <!-- Job -->
                                    <div class="md-form">
                                        @Html.TextBoxFor(m => m.Job, new { @class = "form-control", @id = "Job", Required = true })

                                        <label for="lbljob">Job</label>
                                    </div>
                                </div>
                                <div class="col">
                                    <!-- Job -->
                                    <div class="md-form">
                                        @Html.TextBoxFor(m => m.Approvedby, new { @class = "form-control", @id = "Approvedby", Required = true })

                                        <label for="lblApprovedby">Approved by</label>
                                    </div>
                                </div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                <div class="col">
                                    <!-- Job -->
                                    <div class="md-form">
                                        <div class="custom-control custom-checkbox">
                                            <span style="float:right">
                                                @Html.CheckBoxFor(m => m.IsApproved, new { @class = "custom-control-input", @id = "IsApproved" })

                                                <label class="custom-control-label" for="defaultChecked2">Approved</label>
                                            </span>

                                        </div>
                                    </div>
                                </div>
                            </div>


                            <!--Detail-->
                            <h5 style="margin-top:10px;color:#ff6347">Requisition Details</h5>
                            <hr />
                            <div>
                                <div class="form-row">
                                    <div class="col-md-4">
                                        <!-- Requisition Date -->
                                        <div class="md-form">
                                            @Html.DropDownListFor(m => m.ItemCode, ViewBag.Items as List<SelectListItem>, new { @class = "form-control", id = "ItemCode", Required = true })
                                        </div>
                                    </div>

                                    <div class="col">
                                        <!-- Job -->
                                        <div class="md-form">
                                            <input type="number" id="Qty" name="Qty" placeholder="Qty" class="form-control" ,Required=true />

                                            <label for="lbljob">Qty</label>
                                        </div>
                                    </div>
                                    <div class="col">
                                        <!-- Job -->
                                        <div class="md-form">
                                            <input type="text" id="Remarks" name="Remarks" placeholder="Remarks" class="form-control" ,Required=true />

                                            <label for="lblRemarks">Remarks</label>
                                        </div>
                                    </div>
                                    <div class="col-md-2 col-lg-offset-4">
                                        <a id="addToList" class="btn btn-primary">Add To List</a>
                                    </div>
                                </div>
                                <table id="detailsTable" class="table">
                                    <thead style="background-color:#007bff; color:white">
                                        <tr>
                                            <th style="width:2%">SrNo.</th>
                                            <th style="width:40%">Items</th>
                                            <th style="width:15%">Qty</th>
                                            <th style="width:30%">Remarks</th>
                                            <th style="width:10%"></th>
                                        </tr>
                                    </thead>
                                    <tbody></tbody>
                                </table>
                            </div>
                            <div class="modal-footer">
                                <button type="reset" class="btn btn-primary" data-dismiss="modal">Close</button>
                                <button id="UpdateReq" type="submit" class="btn btn-primary">Save Record</button>
                            </div>
                        </div>
                    </form>

标签: c#jquerymodel-view-controller

解决方案


推荐阅读