消耗助手code在Asp.net MVC OPTGROUP功能 [英] Consuming a Helper code for optgroup functionality in Asp.net MVC

查看:206
本文介绍了消耗助手code在Asp.net MVC OPTGROUP功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有与助手,所以我有点困在使用code手头的工作经验。

I don't have the experience of working with Helpers so I am bit stuck in using a code at hand.

我的要求很简单,我需要的是在 DropDownListFor 扩展方法OPTGROUP功能。尽管搜索,我碰到这个回答并复制了这个,因为它是在一个名为 MyExtensionClass.cs <文件/ EM>。

My requirement is simple and all I need is optgroup functionality in DropDownListFor extension method. While searching, I came across this Answer and have copied this as it is in a file named MyExtensionClass.cs.

不过,我不知道如何使用这个或调用这个定义的扩展方法。请告诉我如何使用这跟我的列表。

But, I don't know how to use this or call the extension method defined in this. Please tell me how can i use this with my list.

现在,以下是控制器code的选择列表为此我要使用的扩展方法。

Right now, following is the controller code for a selectlist for which i want to use the extension methods.

ViewBag.ParentCategoryId = new SelectList(db.Categories, "Id", "Name");

这是我的看法code

And this is my view code

@Html.DropDownListFor(model => model.Product.CategoryId, 
     (IEnumerable<SelectListItem>)ViewBag.CategoryId, "---Choose Category---", 
       new { @class = "required" })  

请帮我这个升级至OPTGROUP扩展方法。

Please help me upgrade this to extension method with optgroup.

推荐答案

我们使用塞尔朱达对OPTGROUP下拉菜单帮手。这里有一个例子:

We use Serge Zab's helper for optgroup dropdowns. Here is a sample:

视图模型:

public class OurViewModel
{
    public int? TypeId { get; set; }
    public IEnumerable<GroupedSelectListItem> GroupedTypeOptions { get; set; }
}

控制器:

public ActionResult Add()
{
    var model = new OurViewModel
    {
        // fill with initial values
    };
    PutTypeDropDownInto(model);
    return View(model);
}

[NonAction]
private void PutTypeDropDownInto(OurViewModel model)
{
    model.GroupedTypeOptions = _repos.GetTypes()
        .OrderBy(t => t.Category.EnglishName).ThenBy(t => t.EnglishName)
        .Select(t => new GroupedSelectListItem
        {
            GroupKey = t.Category.RevisionId.ToString(),
            GroupName = t.Category.EnglishName,
            Text = t.EnglishName,
            Value = t.RevisionId.ToString()
        }
    );
}

视图

@Html.DropDownGroupListFor(m => m.TypeId, Model.GroupedTypeOptions, 
    "[Select a type]")

请注意,你不能使用常规的SelectList。你必须使用自己的GroupedSelectListItem类的集合。此外,我们的解决方案不使用viewbag。下拉列表是强类型的视图模型。

Note that you can't use a regular SelectList. You have to use a collection of his GroupedSelectListItem class. Also, our solution doesn't use viewbag. The dropdown list is strongly typed on the viewmodel.

更新

要获得HTML帮助在视图中工作,认为需要能够找到它。您可以在您的MyExtensionClass.cs命名视图的顶部添加@using指令,或命名空间添加到视图特定的web.config文件,像这样:

To get the html helper to work in your view, the view needs to be able to find it. You can either add a @using directive at the top of the view with your MyExtensionClass.cs namespace, or add the namespace to the view-specific web.config, like so:

<pages pageBaseType="System.Web.Mvc.WebViewPage">
  <namespaces>
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Routing" />
    <add namespace="Microsoft.Web.Mvc" />
    <add namespace="Namespace.For.MyExtensionClass" />
  </namespaces>
</pages>

这篇关于消耗助手code在Asp.net MVC OPTGROUP功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆