English 中文(简体)
在App.net MVC中为选择小组功能制定《助手法》
原标题:Consuming a Helper code for optgroup functionality in Asp.net MVC

我没有与帮助者合作的经验,因此我不大愿意使用手法。

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.

但是,我不知道如何使用这一方法,也没有说其中界定的推广方法。 请告诉我,我怎么能够用我的名单来做到这一点。

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");

这也是我的看法。

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

请帮助我把这一方法升级为选择小组的推广方法。

最佳回答

我们利用Serge Zab的助手选择集体减员。 样本如下:

The viewmodel:

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

www.un.org/Depts/DGACM/index_spanish.htm 控制器:

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()
        }
    );
}

www.un.org/Depts/DGACM/index_spanish.htm The view

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

请注意,你可以经常使用选择性语言。 你们必须使用他的集团语言。 而且,我们的解决办法没有使用观点。 降幅清单在电文中被严格分类。

<>Update>

为了让html的助手在你看来工作,这一看法必须能够找到。 你可以把“使用”指令与你的“我空想”名称空间放在首位,也可以将名称空间添加到特定观点的网站上。

<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>
问题回答

这一点在伙伴关系中得到了补充。 NET MVC, 版本5.2!

Property Group in SelectedList 项目允许你为每个项目指定一个小组

<代码>Html.DropDownList(和DropDownListFor(>现在产生optgroup基于项目清单所列各组别的内容。





相关问题
ASP.NET MVC HTML helper library

There seems to be an abundance of jQuery plugins that you can grab and use quite easily. Are there any HTML helper samples, examples or libraries that you can also download and use. In particular I ...

HTML Helpers and Partial Views

If I have say a Partial View called MypartialView and I have a HTML Helper called "MyHTMLHelper" how can I return a partial view from the helper? My requirement is that sometimes I d like to render a ...

MVC: Override default ValidationMessage

In the world of MVC I have this view model... public class MyViewModel{ [Required] public string FirstName{ get; set; } } ...and this sort of thing in my view... <%= Html.ValidationSummary("...

how will I call the on change event of the ajax dropdownlist?

In my MVC application I am using an ajax dropdownlist and an ajax Cascading dropdownlist I want to write the onChange event of the cascading dropdownlist please tell me what shall I do. I am posting ...

LoadLibrary on OCX file fails in Windows 7 x64

I need to open a html help file from within a legacy windows application written in old version of C++ Builder. HtmlHelp is loaded via HtmlHelp.ocx, which I am loading via LoadLibrary. This has ...

When should I use HtmlHelper Extension Methods?

I am increasingly finding situations where my ASP.NET MVC view requires some logic to perform layout. These routines have no place being in either my model or my controller. I have 3 options: Write ...

热门标签