网页Razor 2 - 如何在回发后保留选定的选项

时间:2013-08-02 14:46:06

标签: asp.net-mvc-4 razor razor-2 asp.net-webpages

所以我在.NET 4.0 ASP.NET网页Razor v2解决方案中有这个小小的cshtml页面。

@{
    Layout = "~/_SiteLayout.cshtml";
    Page.Title = "News For All Crew";

    var db = Database.Open("ConnectionString");    
    var departmentsQry = "select Name from Info_Department_Names IPN order by Name";       
}

<form method="post" action="">
    <select id="selectStation" name="selectStation" onchange="this.form.submit();">
        @foreach (var dept in db.Query(departmentsQry))
        {
            <option value="@dept.Name">@dept.Name</option>
        }
    </select> 
</form>

当用户选择一个选项时,我希望它在回发后保持选择状态。我该怎么做?

1 个答案:

答案 0 :(得分:3)

@foreach (var dept in db.Query(departmentsQry))
{
    <option value="@dept.Name" selected="@(dept.Name == Request["selectStation"])">@dept.Name</option>
}

有关条件属性的更多信息,请参阅此处:http://www.mikesdotnetting.com/Article/201/Cleaner-Conditional-HTML-Attributes-In-Razor-Web-Pages