mvc中的下拉选择问题

时间:2013-10-11 17:27:34

标签: asp.net-mvc asp.net-mvc-3 templates

嗨,我正在一个mvc项目中工作,我在下拉选择这个非常小的问题上工作。

有2个dropdown.1st用于国家名称,第2个用于旅游类型。

根据下拉选择,一个按钮用于搜索。

以下是该网站的链接:

www.ourplanettravel.com.au /

如果我们从第一个下拉列表中选择“塔斯马尼亚”,从第二个下拉列表中选择“Tours& Cruises”并单击搜索按钮,则第二个下拉列表仅在此情况下失去其值(显示 - 选择旅游类型 - )在其他选项中它完美运作。

以下是我正在使用的代码:

     <select id='TourismType' name="TourismType"> 
       <option value=''>--Choose Tourism Type--</option>   
       {{if $item.data.oTourismType}} 
         {{each  $item.data.oTourismType}}

    <option value='${Text}'>${Text}</option>
     {{/each}} 

   </select> 
     {{/if}} 

请建议我在哪里错。

1 个答案:

答案 0 :(得分:0)

看起来下拉列表在当前视图实例中保留其值(因此“Tours&amp; Cruises”是您搜索后查询字符串的一部分并包含在搜索结果中),但不会保留其下拉列表中的值。基本上,传递给将在转到下一页时显示的视图的模型没有绑定选定的旅游类型。您可以在控制器中重新绑定属性。

但是,一般情况下,我建议使用Razor助手来进行模型绑定而不是显式标记,这可能首先避免了这个问题。

一个下拉列表的通用示例......

模型

public class YourModel {
    public int SelectedTourismType { get; set; }
    public IEnumerable<TourismType> TourismTypes { get; set; }
}

旅游类型:

public class TourismType {
    public int TourismTypeID { get; set; }
    public string DisplayName { get; set; }
    // other properties if applicable
}

查看:

@model YourModel

// Your form or whatever here...

@Html.DropDownListFor(m => m.SelectedTourismType,
    new SelectList(Model.TourismTypes, "TourismTypeID", "DisplayNameName"),
    "Select an option") // Default text before the user has selected an option

控制器:

public ActionResult YourAction()
{
    YourModel model = new YourModel();
    model.TourismTypes= new List<TourismType> {
        new TourismType { TourismTypeID = 1, Value = "Tours & Cruises" },
        new TourismType { TourismTypeID = 2, Value = "Some other type name" }
    }

    return View("YourViewName", model);
}

只要在刷新下一页上的视图时通过相同的模型,这应该可以正常工作。当然,您需要修改它以包含两个下拉列表,一个依赖于另一个下拉列表。

相关问题