从MVC中的下拉列表选择中填充文本框

时间:2016-07-05 17:48:55

标签: javascript c# asp.net-mvc

我在从下拉列表中选择一个选项后,尝试从jquery填充文本框。我提供了一些代码片段。

这是视图的标记。

<div class="form-group">
        @Html.LabelFor(model => model.DependantID, "DependantID", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("DependantID", null, htmlAttributes: new { @class = "form-control", @onchange = "GetRegNo()" })
            @Html.ValidationMessageFor(model => model.DependantID, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.RegNo, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @*@Html.EditorFor(model => model.RegNo, new { htmlAttributes = new { @class = "form-control" } })*@
            <span id="loading_progress1" style="display: none;">Please wait...</span>
            @Html.TextBoxFor(model => model.RegNo, new { @Value = @ViewBag.Regno, @readonly = "readonly", @class = "form-control" })
            @*@Html.TextBoxFor(model => model.RegNo, new { @Value = Regno, @readonly = "readonly", @class = "form-control" })*@
            @Html.ValidationMessageFor(model => model.RegNo, "", new { @class = "text-danger" })
        </div>
    </div>

这是我希望实现的jquery代码

function GetRegNo() {
    var dependid = $('#DependantID').val();
    var dprogress = $('#loading_progress1');
    dprogress.show();
    //alert(schoolid);
    $.ajax({
        cache: false,
        //url: '@Url.Action("/Patients/FillClass")',
        url: '/Patients/FillClass',
        type: "GET",
        datatype: "json",
        data: { DependID: dependid },
        success: function (stud) {
            $("#RegNo").html("");  ///clear entry before appending
            $("#RegNo").html() = stud.regno;
            alert("I reach here");
            alert("data.regno " + stud.regno);
        //$.each(studi, function (i, stude) {
        //$("#SClassID").append(
        //$('<option></option>').val(stude.SClassID).html(stude.Class));
       // });
        dprogress.hide();
        },
        error: function (response) {
        alert("Error: " + response.responseText);
        dprogress.hide();
        }

     });

然后这是来自控制器

[AcceptVerbs(HttpVerbs.Get)]
    public ActionResult FillClass(int? dependid)
    {
        if (dependid == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        string regno="";
        var N = db.Patients.Max(f => (int?)f.PatientID).GetValueOrDefault(0);
        N++;
        string Nt = Convert.ToString(N);
        if (dependid == 1)
        {

           regno = "M" + Convert.ToString((DateTime.Now).ToString("yy")) + BizLogic.Right("00000" + Nt, 5);
        }
        else
        {

            regno = "D" + Convert.ToString((DateTime.Now).ToString("yy")) + BizLogic.Right("00000" + Nt, 5);
        }
        return Json(regno, JsonRequestBehavior.AllowGet);
    }

因此,从上面的代码片段开始,当下拉列表中发生更改时,将调用java,而java又会调用控制器中的方法并填充文本框。如果有更好的方式,我全都开放。谢谢。

1 个答案:

答案 0 :(得分:0)

我发现了jquery脚本的问题。我将发布更改并指出更改。这与我的任务有关。我在此标记的行将值发布到文本框中。

function GetRegNo() {
    var dependid = $('#DependantID').val();
    var dprogress = $('#loading_progress1');
    dprogress.show();
    //alert(schoolid);
    $.ajax({
        cache: false,
        //url: '@Url.Action("/Patients/FillClass")',
        url: '/Patients/FillClass',
        type: "GET",
        datatype: "json",
        data: { DependID: dependid },
        success: function (stud) {
            $("#RegNo").html("");  ///clear entry before appending
     here:  $('input#RegNo').val(stud); //i missed it here, changing to this changed solved the problem, compare with with script in the question

        dprogress.hide();
        },
        error: function (response) {
        alert("Error: " + response.responseText);
        dprogress.hide();
        }

     });
   }