2013-03-18 44 views
0

嗨,在我的MVC3项目中使用RAZOR,我有一个疑问。在控制器MVC3中获取dropdownlist选定值Razor

我有一个名为CatlogPage.cshtml的页面。在那个页面中我有一个Dropdownlist控件。

@(Html.Telerik().DropDownListFor(m => m.CatalogName) 
    .BindTo(Model.CatalogName).HtmlAttributes(new { style = "width:235px" })) 
    <input type="submit" value="Next" /> 

我有一个名为Hierarchy.cs控制器: 在控制器,

public ActionResult Hierarchy() 
     { 

      // Need to get the selected value in DropDownList 
      return View("Hierarchy"); 
     } 

如何从下拉列表到控制器中的值(CatalogName编)?

这是我的型号代码。

public List<SelectListItem> GetCatalogNameModel() 
     { 
      try{ 
       var cat = from s in _entities.Catalogs.ToList() 
          select new SelectListItem() 
            { 
             Text = s.CatalogName, 
             Value = s.CatalogName 
            }; 
       return cat.ToList();} 
      catch (Exception ex) 
      { 
       CreateLogFiles.ErrorLog(HttpContext.Current.Server.MapPath("~/Logs/ErrorLog"), ex, "CatalogService", "GetCatlogName"); 
       return null; 
      } 

     } 
+0

真的需要这一点的更多信息,你的意思,从顶部的模型代码发布到Hierarchy方法,因为如果它是,你缺少将模型传递给Hierarchy。不知道什么模型是在顶部的代码虽然 – OJay 2013-03-18 09:49:01

+0

@OJay PLZ看到我更新的代码 – 2013-03-18 10:44:03

+0

对不起,但这似乎是一种方法,返回一个选择项目列表。我更关心的是顶部视图的代码片段中Model的含义,Model.CatalogName的属性 – OJay 2013-03-18 10:48:48

回答

1

所以假设第一代码片段是一个强类型视图(对象DatabaseModel.CatalogModel)和您所提交的形式向数字体系方法,然后传递一个CatalogModel和访问CatalogName编应该是什么你的后?

public ActionResult Hierarchy(DatabaseModel.CatalogModel inputModel) 
     { 

      inputModel.CatalogName; //This will be the value from the drop down list 
      return View("Hierarchy"); 
     } 
1

对于DropDownList中,我使用一个Int丙以接收所述选择的ID。所以,我的回答是: 这个属性添加到您的视图模型:

public Int32 SelectedCatalogId {get;set;} 

并将其绑定到DropDownList的:

@Html.DropDownListFor(m => m.SelectedCatalogId, Model.GetCatalogNameModel()) 
相关问题