hsim
hsim

Reputation: 2080

Update data in textboxfor fields using a selected object in a dropdownlist

In a view where my user can create a new item, he will have to select an item from a dropdownlist like this:

@Html.DropDownListFor(model => model.m_CustomerId, ViewBag.ListCustomers as SelectList, "--- Select Customer ---", new  {@class = "CustomerInfo" })

When the selection is made, there are fields in the current view that would name to be updated with the selection made. Those fields are like those:

Item Name: @Html.TextBoxFor(model => model.m_ItemName, new { @disabled = "disabled" }) @Html.ValidationMessageFor(model => model.m_ItemName)<br/>
        Item Date: <span style="font-weight: bold">@Html.DisplayFor(model => model.m_ItemDate)</span> @Html.ValidationMessageFor(model => model.m_ItemDate)<br/>

I've read a few things like maybe using JQuery or Java, but I'm honestly cluesless and I don't know these languages. How could I do this? I do not mind trying out languages, scripts or anything else, but I'm rather a newbie in MVC app and this is puzzling me. Thanks a lot!

Upvotes: 2

Views: 764

Answers (1)

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

So what you might want to do is download and install jQuery via NuGet Package Manager. Once you've gotten that installed then we can extend the controls. You could extend the DropDownListFor by adding some attributes.

@Html.DropDownListFor(model => model.m_CustomerId,
    ViewBag.ListCustomers as SelectList,
    "--- Select Customer ---",
    new
    {
        @class = "CustomerInfo",
        onchange = "function() { $('m_ItemName').val($(this).text()); }"
    }
)

Upvotes: 1

Related Questions