【问题标题】:Fill textbox from dropdownlist selection in MVC从 MVC 中的下拉列表选择中填充文本框
【发布时间】:2016-07-05 17:48:54
【问题描述】:

从下拉列表中选择一个选项后,我试图从 jquery 填充一个文本框。我包含了一些代码的 sn-ps。

这是视图的标记。

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

所以从我上面的代码 sn-ps 来看,当下拉列表发生变化时,会调用 java,然后调用控制器中的方法并填充文本框。如果有更好的方法,我是开放的。谢谢。

【问题讨论】:

  • 你发布了一堆代码。它不工作吗?您的问题没有明确的问题状态。此外,您永远不应将“JavaScript”称为“Java”,因为它们完全不同。
  • 感谢您纠正有关 Java 和 Javascript 的问题,我的错。但是我发布了代码,以便可以看到我想要实现的目标。
  • 不,它不工作!
  • 再一次,您没有在问题中提供明确的问题陈述。我不知道“不工作”是什么意思。您需要具体描述究竟是什么不起作用。
  • 我想执行一种级联下拉列表,但不是填充另一个下拉列表,而是填充一个文本框。希望这能解释我想要达到的目标?

标签: javascript c# asp.net-mvc


【解决方案1】:

我发现了 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();
        }

     });
   }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多