sphinx
sphinx

Reputation: 11

From DropDownList to Textbox

I have DropDownList and Textbox on my page. DropDownList show data from DataBase(Table "List"). When I choose one line in DropDownList I want to see it in Textbox, make changes and save this line into DataBase(Table "Text") after press "OK".

Please tell me, how to do this?

Code:

Controller.cs:

    public ActionResult Create(Model model)
    {         
        var viewModel = new ISMSViewModel
        {
            Model = new Model(),
        };

        return View(viewModel);

    } 

    [HttpPost]
    public ActionResult Create(Model model, int i)
    {
        try
        {
            sysDB.AddToActives(model);
            sysDB.SaveChanges();

            return RedirectToAction("Browse");

        }
    } 

ListText.ascx:

            <%: Html.EditorFor(model => model.Model, new { Lists = Model.Lists } %>

            <%: Html.Label("List") %>
            <%: Html.DropDownList("ListId", new SelectList(ViewData["Lists"] as IEnumerable, "ListId", "Name", Model.ListId)) %>

            <%: Html.Label("TextBox") %>
            <%: Html.TextBoxFor(model => model.TextBox) %>
            <%: Html.ValidationMessageFor(model => model.TextBox) %>

Upvotes: 1

Views: 809

Answers (1)

ajay_whiz
ajay_whiz

Reputation: 17931

For this you need to write some JavaScript.

handle the onchange event for dropdown

<%: Html.DropDownList("ListId", new SelectList(ViewData["Lists"] as IEnumerable, "ListId", "Name", Model.ListId), new {@onchange="setVal(this)"}) %>

on your view inside head block

<script type="text/javascript">
function setVal(obj)
{
document.getElementById("TextBox").value=obj.value;
}
</script>

Upvotes: 2

Related Questions