使用Razor绑定DropDownList for MVC View中的选定值

时间:2011-10-27 19:25:55

标签: html asp.net-mvc razor

我有一个可行的视图,但我无法弄清楚如何从DropDownList中获取所选值:

@model IEnumerable<TRP_MVC_Prototype.Models.usp_TM_Select_ShortNameResult>
@using System.Web;
@using System.Web.WebPages;
@using System.Web.Mvc;  

@{
    ViewBag.Title = "Details";
}
@using (Html.BeginForm("Details", "ProgramSummary", FormMethod.Post, new { id = "Details" }))
{
<div id="main" style="background-color:White">
        <h1 style="background-color:transparent;color:Blue;">
        <a>You are logged on as: @ViewBag.Message </a>
        <span class="DrpDwnLst">DrpDwnLst</span>
        @Html.DropDownList("Short_Title", new SelectList(Model, "short_title", "short_title"), "--Select One--").
        @Html.ActionLink("Select","Details",new { Shrt_title = ""})
        <a style="color:Blue;position:absolute; right:500px"> @Html.ActionLink("Create Program Summary", "Index", "User_Guide") </a>
        <a style="color:Blue;position:absolute; right:250px"> @Html.ActionLink("Edit Program Summary", "Index", "User_Guide")</a>
        <a style="color:Blue;position:absolute; right:50px"> @Html.ActionLink("Delete TRP", "Index", "User_Guide")</a>
        </h1>
        <h1 style="background-color:transparent;color:Blue;">Select TRP to View</h1>
        <h1 style="color:Gray";>______________________________________________________________________________________________________________________________________________________________________________</h1>
}

DropDownList正确显示,但我不知道如何在ActionLink中返回Selected值。在动作链接中,第三个参数将值传递回它当前具有的控制器“”,但我想弄清楚如何引用所选值。

2 个答案:

答案 0 :(得分:0)

您可以使用jquery执行此操作。要从元素更改href,您可能会看到此post

您还需要处理下拉列表的更改事件:

$('#short_title').change(function() 
{
   // do someting here
});

答案 1 :(得分:0)

这是我的回答

    @Html.DropDownList("short_name", ViewBag.DetailsList as SelectList, "--Select One--", new { onchange = "dofunction(this.form.short_name);" });
    function dofunction(dropdown) {
        debugger;
        for (i = 0; i < 194; i++) {
            if (dropdown[i].selected == true) {
                var Shrt_ttls = dropdown[i].value.toString()
                //document.getElementById("shrtLst").value = Shrt_ttls;

                $.ajax({
                    url: "/ProgramSummary/Details?Shrt_titles=" + Shrt_ttls,                 
                    type: 'Post',                 
                    data: Shrt_ttls,                 
                    success: function(result) { 
                        alert( "Short Name is: " + Shrt_ttls); // process the results from the controller action                 
                    },             
                    error: function () {
                         alert ( "no deal");
                    }
                });  
            }
        }
        return true;
     }

这会将javascript函数的值返回给HTTPPost for Index。 谢谢, 布鲁斯