填补下拉菜单选择文本框在MVC实体框架 [英] Fill textbox on dropdown selection in mvc entity framework

查看:128
本文介绍了填补下拉菜单选择文本框在MVC实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Asp.net MVC5和实体Framework.I我在这两个技术的新

I am using Asp.net MVC5 and Entity Framework.I am new in Both of technologies.

基本上我创建的形式,在这种形式下可用下拉列表中,当我从下拉式的价值。我想,以填补Textboxs也该表格上提供。

Basically i create a Form, In this Form a DropDown available, when i select value from DropDown. I want to fill Textboxs which also available on this form.

这是我的控制器

public class ChainController : Controller
{
    private hcEntities db = new hcEntities();

    // GET: Chain
    public ActionResult Index()
    {
        ViewBag.name = new SelectList(db.chains,"code","name");
        return View(db.chains.ToList());
    }
}

查看 -

<div class="form-horizontal">
    <hr />
    <div class="form-group">
        <label class="col-sm-2 control-label">
            Select Chain
        </label>
        <div class="col-md-3">            
            @Html.DropDownList("name" , null, new { @class = "form-control" })           
        </div>
    </div>
 @using (@Html.BeginForm())
 {
    @Html.AntiForgeryToken()
    <div class="form-group">
        <label class="col-sm-2 control-label">
            Chain UserName
        </label>
        <div class="col-md-3">            
           @Html.TextBox("ChainName", null, new { @class = "form-control" }) //WHAT DO I DO HERE???????
        </div>
    </div>
 }
</div>

型号(由EF生成chain.cs)

public partial class chain
{
    public long chain_id { get; set; }
    public string name { get; set; }
    public string code { get; set; }
    public string username { get; set; }
    public string password { get; set; }
    public long created_by { get; set; }
    public System.DateTime created_on { get; set; }
    public Nullable<long> updated_by { get; set; }
    public Nullable<System.DateTime> updated_on { get; set; }
    public chain()
    {
        created_by = 1;
        created_on = DateTime.Now;
    }
}

我不知道会是什么下一个步骤。如何填充文本框与下拉菜单的选择的用户名值。
我发现在计算器太多的答案,但我填这些都不是我的帮助。例如
<一href=\"http://stackoverflow.com/questions/22563819/fill-the-textboxes-with-selecting-dropdownlist-in-mvc\">fill在MVC 选择DropDownList中的文本框,<一个href=\"http://stackoverflow.com/questions/16814450/how-to-populate-a-textbox-based-on-dropdown-selection-in-mvc\">How基于MVC的下拉菜单选项来填充一个文本框..?

I don't know what will be next step. how to fill textbox with value of Username from the selection of dropdown. I find too many answer in stackoverflow but i fill these are not helpful for me. for example fill the textboxes with selecting dropdownlist in mvc, How to populate a textbox based on dropdown selection in MVC..?

帮帮我!

推荐答案

我做到了我的自我。这里是溶液

I did it by my self. Here is the solution.

在previous code一些变化

Some change in previous code

控制器

相反ViewBag的,我使用的ViewData。两者都可以正常使用,但我用的ViewData

Instead of ViewBag, I use ViewData. Both are working perfectly, but i use ViewData

    // GET: Chains
    public ActionResult Index()
    {
        ViewData["chain_name"] = new SelectList(db.chains, "code", "name");
        return View(db.chains.ToList());
    }

此外,我创建的一个功能谁获取从数据库中的数据

Also i create a function in controller who fetch data from database

    //Action Function 
    [HttpPost]
    public ActionResult Action(string code)
    {
        var query = from c in db.chains
                    where c.code == code
                    select c;

        return Json(query);
    }

之后,我把这个动作控制器中通过JavaScript我的看法。

After that I call this Action Controller in my view through javascript.

查看

    @using (@Html.BeginForm("Action","chains", FormMethod.Post))
     {
        @Html.AntiForgeryToken()
        <div class="form-horizontal">
            <hr />
            <div class="form-group">
                <label class="col-sm-2 control-label">
                    Select Chain
                </label>
                <div class="col-md-3">
                    @Html.DropDownList("ddlchainname", (SelectList)ViewData["chain_name"], new {         onchange = "Action(this.value);", @class = "form-control" })
                </div>
            </div>
            <div class="form-group">
        <label class="col-sm-2 control-label">
            Chain Name
        </label>
        <div class="col-md-3">
            @Html.TextBox("ChainName", null, new { @class = "form-control" })
        </div>
        <label class="col-sm-2 control-label">
            Username
        </label>
        <div class="col-md-3">
            @Html.TextBox("username", null, new { @class = "form-control" })
                </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">
            Chain Code
       </label>
        <div class="col-md-3">
            @Html.TextBox("ChainCode", null, new { @class = "form-control" })
        </div>
    </div>
        </div>
     }

通过下拉菜单的onChange JavaScript函数调用

Javascript Function call by dropdown onChange

<script type="text/javascript">
function Action(code) {
    $.ajax({
        url: '@Url.Action("Action", "Chains")',
        type: "POST",
        data: { "code": code },
        "success": function (data) {
            if (data != null) {
                var vdata = data;
                $("#ChainName").val(vdata[0].name);
                $("#ChainCode").val(vdata[0].code);
                $("#username").val(vdata[0].username);
            }
        }
    });
}
</script>

这就是做工精细...

That's work Fine...

这篇关于填补下拉菜单选择文本框在MVC实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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