2

所以我在 .NET 4.0 ASP.NET Web Pages 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>

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

4

1 回答 1

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

于 2013-08-02T21:35:12.867 回答