填写文本框,在mvc中选择下拉列表

时间:2014-03-21 16:09:51

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我想填充从数据库中获取数据的文本框。当我使用下拉列表选择任何值时,我需要从下拉列表中获取ID并调用填充@Html.EditorFor(model => model.taskName)的函数。该页面没有变化。怎么可能?

<div class="form-group">
      @Html.LabelFor(model => model.jobID, "jobID", new { @class = "control-label col-md-2" })
       <div class="col-md-10">
      @Html.DropDownList("jobID", String.Empty)
      @Html.ValidationMessageFor(model => model.jobID)
</div>
</div>
     <div> @Html.Partial("../Location/taskname") </div>
</div>

taskname.cshtml

@Html.AntiForgeryToken()
    <div class="form-horizontal">
        @Html.ValidationSummary(true)
        <div class="form-group">
            @Html.LabelFor(model => model.taskName, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.taskName)
                @Html.ValidationMessageFor(model => model.taskName)
            </div>
        </div>
    </div>

1 个答案:

答案 0 :(得分:1)

让你的下拉列表如下:

@Html.DropDownList("jobID", null, new {@id="job"})

和任务名称文本框:

@Html.EditorFor(model => model.taskName, new {@id="taskname"})

这样做,为下拉列表索引更改事件编写jquery事件,并发送ajax调用以获取从下拉列表中选择的作业ID的任务名称:

$('select#job').change(function(){

var JobId =$(this).val();


// Send Ajax call and get Task name

var Url = 'http://example.com/Controller/Action/?jobid='+JobId ;

$.ajax({
         url:    Url ,
         success: function(result) {
                     $('input#taskname').val(result); 
                  },
         error: function(error) {
                     alert(errorss); 
                  }

    });     


});