Fill textbox on dropdown selection in mvc entity framework

asp.net-mvc c# entity-framework

Question

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

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.

This is my Controller

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());
    }
}

View:-

<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>

Model(chain.cs generated by EF)

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;
    }
}

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..?

Help Me !

1
3
5/23/2017 11:54:05 AM

Accepted Answer

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

Some change in previous code

Controller

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);
    }

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

View

    @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>
     }

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...

3
4/11/2015 11:57:05 AM

Popular Answer

You may change your select list parameter from "code" to "username" and use some jQuery code on the view. I have created a simple example for you, please have a look:

In my Controller:

List<temp> tempLIst = new List<temp>();
tempLIst.Add(new temp() { Id = 1, code = "111", name = "first", username = "user first" });
tempLIst.Add(new temp() { Id = 1, code = "222", name = "second", username = "user second" });
tempLIst.Add(new temp() { Id = 1, code = "333", name = "third", username = "user third" });
tempLIst.Add(new temp() { Id = 1, code = "444", name = "four", username = "user four" });

ViewBag.name = new SelectList(tempLIst, "username", "name");
return View();

Here temp is the class having properties (Id, code, name and username)

In my view

<div>
temp list: @Html.DropDownList("name",(IEnumerable<SelectListItem>)@ViewBag.name,"select value")
@Html.TextBox("txtValue")
</div>

This is some script section i use on this view page.

<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script>
    $(document).ready(function () {
        $('#name').change(function () {
        $('#txtValue').val($(this).val());
        });
    });
</script>

Now when you run this code snippet, the textbox value will change according to the selected dropdownlist's value.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow