如何根据MVC中的第一个文本框自动获取值 [英] How to automatically get value based on the first textbox in MVC

查看:157
本文介绍了如何根据MVC中的第一个文本框自动获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框输入Id并有另一个文本框来获取第一个文本框中具有id的人的姓名。但Id和名字在另一个叫做学生的表中。这里我的要求是,当我在文本字段中输入Id时,我需要在第二个文本框字段中获取学生的姓名。这是我的控制器

 public async Task< ActionResult> Index()
{
var result =(from db in db.Details
join d in db.Students on pm.Id equals d.ID
select new DetailsList
{
Id = d.ID,
Name = d.FirstName,
Cash = pm.Cash,
Date = pm.Date
})。ToList( );
return查看(结果);
}

这是我的控制器部分,我将两个表连接到名为DetailsLists的模型类中

在我的视图页面中

 @model IEnumerable< A.Models.DetailsList> 



< table> 
< tr style =height:40px>
< th style =width:120px;>
ID
< / th>
< td style =height:30px;>
< input type =textclass =form-control/>
< / td>
< / tr>
< tr style =height:40px>
< th style =width:120px;>
@ Html.DisplayNameFor(model => model.FirstName)
< / th>
< td>
< input type =textclass =form-control/>
< / td>
< / tr>
< td>
< p>
@ Html.ActionLink(新建,创建)
< / p>
< / td>
< / tr>
< / table>





既可以输入ID,也可以在下拉列表中显示所有ID当选择Id时,它应该在第二个文本框中显示名称。



我尝试过:



我是mvc的新手,我没有得到正确的方法来完成我需要的任务,任何人都可以帮助我吗?

解决方案

< blockquote>你有2个文本框,一个用于id,另一个用于根据id显示名称。这里最好的解决方案是使用jQuery AJAX。这是如何进行的。



1.在第一个文本框keydown事件中,让jQuery AJAX在你的Controller上调用C#函数。

2.这个C#函数将收到id并将根据此id从数据库中获取名称,并在最后返回名称。

3.然后,您将在名称文本框中显示该名称。



您将首先在您的id文本框中应用.keydown()事件,如下所示:



< pre lang =c#>


#idtextbox)。keydown(function(){
// jquery ajax code
});





要进行AJAX调用,您可以使用 AJAX方法 jQuery加载



您也可以查看 CodeProject 在MVC应用程序中执行ajax调用的文章。


I have a textbox to enter Id and have another textbox to get the name of the person having the id in the first textbox. But the Id and firstname are in another table called students. Here my requirement is that when I enter the Id in the textfield I need to get the name of the student in the second textbox field. This is my controller

public async Task<ActionResult> Index()
        {
            var result = (from pm in db.Details
                          join d in db.Students on pm.Id equals d.ID
                          select new DetailsList
                          {
                              Id = d.ID,
                              Name = d.FirstName,
                              Cash=pm.Cash,
                              Date = pm.Date
                          }).ToList();
            return View(result);
        }

This is my controller part where I joined two tables into a model class called DetailsLists
In my view page

@model IEnumerable<A.Models.DetailsList>


<table>
                    <tr style="height:40px">
                        <th style="width:120px;">
                             ID
                        </th>
                        <td style="height:30px;">
                            <input type="text" class="form-control"/>
</td>
                    </tr>
                    <tr style="height:40px">
                        <th style="width:120px;">
                            @Html.DisplayNameFor(model=>model.FirstName)
                        </th>
                        <td>
                            <input type="text" class="form-control" />
                        </td>
                    </tr>
<td>
                            <p>
                                @Html.ActionLink("Create New", "Create")
                            </p>
                        </td>
                    </tr>
                </table>



can be either can enter the Id or can display all the Id's in drop down list and when selecting the Id it should display the Name in the second text box.

What I have tried:

I am new to mvc and I didnt get the correct way to do the task which I need can anyone please help me ??

解决方案

You have 2 textboxes one for id and another for showing name based on the id. Here the best solutions will be to use jQuery AJAX. This is how to proceed.

1. On the first textbox keydown event make jQuery AJAX to call C# function on your Controller.
2. This C# function will receive the id and will fetch the name from the database based on this id and will return the name at the end.
3. You will then show that name on your name textbox.

The you will first apply .keydown() event on your id textbox like this:


( "#idtextbox" ).keydown(function() { //jquery ajax code });



For making AJAX call you can use AJAX Method or jQuery Load.

You can also check this CodeProject Article that does ajax call in MVC application.


这篇关于如何根据MVC中的第一个文本框自动获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆