获取选择标签助手的更改事件数据

huangapple go评论61阅读模式
英文:

Get data on change event of select Tag helper

问题

我有一个在.NET Core MVC Razor中的项目。我正在使用select标签助手来创建下拉列表。我需要从数据库中获取某个值,并根据下拉框中选择的值在文本框中显示它。如何实现这个功能?我需要在选择值后仍然保持在同一页面,并继续填写表单。请找到我的Razor代码:

<div class="form-group row">
    <label asp-for="@Model.addAppointmentInQueue.Doctor" class="col-md-3 col-form-label-lg"></label>
    <div class="col-md-9">
        <div class="input-group">
            <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
            <select id="lstdoctor" asp-for="@Model.addAppointmentInQueue.Doctor"
                asp-items="@Model.addAppointmentInQueue.doctorList"  
                new { onchange = "return ToggleViewItems();" }
                class="form-control form-control-lg selectpicker">
                <option>Please select Doctor</option>
            </select>
        </div>
    </div>
</div>
<div class="col-md-6">
    <div class="form-group row">
        <label asp-for="@Model.addAppointmentInQueue.consultationCharges" class="col-md-3 col-form-label-lg"></label>                  
        <div class="col-md-9">
            <div class="input-group">
                <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                <input asp-for="@Model.addAppointmentInQueue.consultationCharges" class="form-control" type="text">
            </div>
        </div>
    </div>
</div>

注意:这是您提供的Razor代码的翻译部分。

英文:

I have a project in .Net core MVC Razor . I am using select tag helper for drop down list. I need to get some value from database & display it in text box based on selection of select value in dropdown box. How can I achieve this? I need to remain in same page after value selected and continue with same form. Please find my Razor code :

  &lt;div class=&quot;form-group row&quot;&gt;
                                &lt;label asp-for=&quot;@Model.addAppointmentInQueue.Doctor&quot; class=&quot;col-md-3 col-form-label-lg&quot;&gt;&lt;/label&gt;
                                &lt;div class=&quot;col-md-9&quot;&gt;
                                     &lt;div class=&quot;input-group&quot;&gt;
                                    &lt;span class=&quot;input-group-addon&quot;&gt;&lt;i class=&quot;glyphicon glyphicon-user&quot;&gt;&lt;/i&gt;&lt;/span&gt;
                                   &lt;select id=&quot;lstdoctor&quot; asp-for=&quot;@Model.addAppointmentInQueue.Doctor&quot;
                                   asp-items=&quot;@Model.addAppointmentInQueue.doctorList&quot;  
                                   new { onchange = &quot;return ToggleViewItems();&quot; }
                                   class=&quot;form-control form-control-lg selectpicker&quot;&gt;
                                     &lt;option&gt;Please select Doctor&lt;/option&gt;
                                    &lt;/select&gt;
                                &lt;/div&gt;
                                 &lt;/div&gt;
                            &lt;/div&gt;
  &lt;div class=&quot;col-md-6&quot;&gt;
                &lt;div class=&quot;form-group row&quot;&gt;
                      &lt;label asp-for=&quot;@Model.addAppointmentInQueue.consultationCharges&quot; class=&quot;col-md-3 col-form-label-lg&quot;&gt;&lt;/label&gt;                  
                    &lt;div class=&quot;col-md-9&quot;&gt;
                        &lt;div class=&quot;input-group&quot;&gt;
                             &lt;span class=&quot;input-group-addon&quot;&gt;&lt;i class=&quot;glyphicon glyphicon-user&quot;&gt;&lt;/i&gt;&lt;/span&gt;
                            &lt;input asp-for=&quot;@Model.addAppointmentInQueue.consultationCharges&quot; class=&quot;form-control&quot; type=&quot;text&quot;  &gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
                &lt;/div&gt;

答案1

得分: 0

如果您已经可以从 GetConsultationChagesForDoctor?doctor= 返回某个字符串,下面的代码将有效:

<select id="lstdoctor" asp-for="@Model.addAppointmentInQueue.Doctor"
                    asp-items="@Model.addAppointmentInQueue.doctorList" onchange="myFunction(this.options[this.selectedIndex].value)"
                    class="form-control form-control-lg selectpicker">
                <option>请选择医生</option>
            </select>
<input id="textbox1"  asp-for="@Model.addAppointmentInQueue.consultationCharges" class="form-control" type="text">
<script>
    function myFunction(chosen) {
        $.ajax("/GetConsultationChagesForDoctor?doctor=" + chosen, {
            type: 'GET', 
            success: function (data) {
                $('#textbox1').val(data);
            },
        });
    }
</script>
英文:

If you can already return some string from GetConsultationChagesForDoctor?doctor= ,the below code works<br>
Made some change on select and input

&lt;select id=&quot;lstdoctor&quot; asp-for=&quot;@Model.addAppointmentInQueue.Doctor&quot;
                    asp-items=&quot;@Model.addAppointmentInQueue.doctorList&quot; onchange=&quot;myFunction(this.options[this.selectedIndex].value)&quot;
                    class=&quot;form-control form-control-lg selectpicker&quot;&gt;
                &lt;option&gt;Please select Doctor&lt;/option&gt;
            &lt;/select&gt;
&lt;input id=&quot;textbox1&quot;  asp-for=&quot;@Model.addAppointmentInQueue.consultationCharges&quot; class=&quot;form-control&quot; type=&quot;text&quot;&gt;
&lt;script&gt;
    function myFunction(chosen) {
        $.ajax(&quot;/GetConsultationChagesForDoctor?doctor=&quot; + chosen, {
            type: &#39;GET&#39;, 
            success: function (data) {
                $(&#39;#textbox1&#39;).val(data);
            },
        });
    }
&lt;/script&gt;

huangapple
  • 本文由 发表于 2023年4月10日 19:17:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75976600.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定