从DropDownList到Textbox

时间:2011-03-18 08:54:15

标签: c# html asp.net-mvc

我的页面上有DropDownListTextboxDropDownList显示来自DataBase(Table "List").的数据 当我在DropDownList中选择一行时,我希望在Textbox中看到该行,请在按“确定”后进行更改并将此行保存到DataBase(Table "Text")

请告诉我,怎么做?

代码:

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

1 个答案:

答案 0 :(得分:2)

为此你需要写一些JavaScript。

处理下拉列表的onchange事件

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

在你的观点上 在head块内

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