本文介绍了MVC3文本框文本更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用mvc3,我有以下情况:
I have the following scenario, using mvc3:
我有一个数据库表,其中包含RecordID,RecordName和RecordType.显示的是三个文本框,一个用于前面提到的每个字段.
I have a database table which holds a RecordID, RecordName and RecordType. Displayed are three text boxes, one for each of the fields mentioned previously.
我的问题是,当我在相关文本框中输入RecordID时,我希望能够显示该特定RecordID的RecordName和RecordType.我怎样才能做到这一点?
My Question is, when i enter a RecordID into the relevant text box, i want to be able to show the RecordName and RecordType for that particular RecordID. How can i achieve this?
推荐答案
在视图中:
@Html.TextBoxFor(model => model.RecordId)
@Html.TextBoxFor(model => model.RecordName)
@Html.TextBoxFor(model => model.RecordType)
<script language="javascript">
$('#RecordId').change(function(){
var recordId = this.value;
$.getJSON("/MyController/GetRecordById",
{
id: recordId
},
function (data) {
$('RecordName').val(data.Name);
$('RecordType').val(data.Type);
});
});
</script>
在控制器中:
public JsonResult GetRecordById(int id)
{
var record = recordRepository.GetById(id);
var result = new {
Name = record.Name,
Type = record.Type
}
return Json(result, JsonRequestBehavior.AllowGet);
}
这篇关于MVC3文本框文本更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!