.NET MVC 3添加属性SelectListItem在C#属性、NET、MVC、SelectListItem

2023-09-03 17:14:23 作者:★把心丟到海しǐ喂鱼ㄦ

所以,我想在风格化下拉标签。我的MVC助手设置有点像这一点,但在DB真实的东西拉,所以数据并不难codeD。

So I'm trying to stylize the tag in a dropdown. My MVC helper is set up kind of like this but the real thing pulls from the DB, so the data is not hard coded.

@{
    List<string> ListOfText = new List<string> { "FirstThing","SecondThing","ThirdThing"};
    List<string> ListOfValue = new List<string> { "1","2","3"};
    List<SelectListItems> ListOFSELCETLISTITEMS = new List<SelectListItem>();    

    for (int x = 0; x < 3; x++)
    {
        ListOFSELCETLISTITEMS .Add(new SelectListItem
        {
            Text = ListOfText[x],
            Value = ListOfValue[x],
            Selected = (selectedValue == ListOfValue[x])
        });
    }
}   
@Html.DropDown("NAME",ListOFSELCETLISTITEMS)

这让我有点像

<select id="Name" name="Name">
    <option value="1">FirstThing</option>
    <option value="2">SecondThing</option>
    <option value="3">ThirdThing</option>
</select>

我需要的是这样的事情

What i need is something like this

<select id="Name" name="Name">
    <option value="1" class="option1">FirstThing</option>
    <option value="2" class="option2">SecondThing</option>
    <option value="3" class="option3">ThirdThing</option>
</select>

我试着做这样的事情

I've tried doing things like

@{
    List<string> ListOfText = new List<string> { "FirstThing","SecondThing","ThirdThing"};
    List<string> ListOfValue = new List<string> { "1","2","3"};
}
<select id="Name" name="Name">
    @ for (int x = 0; x < 3; x++)
    {
        <text>
        <option value="@ListOfValue[x]" @{if(selectedValue == ListOfValue[x])
            { @Html.Raw("selected='selected'") }}
            @class = '@Html.Raw("option"+x)'>
            @ListOfText[x]
        </option>
        </text>
    }   
</select>

但这似乎到控制器混淆,并没有认识到,以上映射到下拉的值

But this seems to confuse the controller and it doesn't recognize that the dropdown's value above maps to

public ActionResult method(string Name)

在一则讯息控制器或获得。那么该行

in the controller on a post or get. Well the line

 @Html.DropDown("NAME",ListOFSELCETLISTITEMS)

在这个视图允许控制器理解方法应该映射到下拉的价值。

in the view does allow the controller to understand the method should map to that dropdown's value.

我怎样才能做到这一点?有没有一种方法来unconfuse控制器,并能手写HTML没有Html.helpers?

How can i do this? Is there a way to unconfuse the controller and be able to hand write HTML without the Html.helpers?

推荐答案

要渲染直接在控制器动作,你可以试试:

To render directly a controller action, you can try :

@ {Html.RenderAction(动作,控制器); }

@{ Html.RenderAction(Action, Controller); }

要调用你的控制器动作,应该返回一个字符串您的内容。

To invoke your controller action that should be returning a string with your content.

[HttpGet]
public string Action(int id) 
{
   return your context in here
}

不过,我认为这是清洁剂中添加一个Ajax操作返回的数据构建的选择,并有结果后,使用jquery解决方案添加类

However I think it's cleaner to add an Ajax action returning the data for building the select and after having the results, using jquery solution for adding a class (which can be returned in the AJAX response itself)

编辑:澄清:

假设你有项目的集合如下:

Assuming you've got a collection of Item as follows:

class Item {
   public int Value { get; set; }
   public string CssClass { get; set; }
   public string Description{ get; set; }
}
private const string EMPTY_OPTION = "<option value=''></option>";
[HttpGet]
public string Action(int id) 
{
    // Load a collection with all your option's related data
    IQueryable data = LoadSomethingFromDbOrWherever(id);
    // Build output
    StringBuilder sb = new StringBuilder();
    sb.AppendFormat("<select id='foo' name='foo'>");
    sb.AppendFormat(EMPTY_OPTION);
    foreach (Item b in data)
    {
            sb.AppendFormat("<option value='{0}' class='{1}'>{2}</option>",
                            b.Value, b.CssClass, b.Description);
    }
    sb.AppendFormat("</select>");
    return sb.ToString();
}

阿贾克斯选项:

for ajax option:

[HttpGet]
public JsonResult Action(int id) 
{
   //same as above, obtain a collection
    // Load a collection with all your option's related data
    IQueryable data = LoadSomethingFromDbOrWherever(id);
    var jsonData = new
            {
                    from c in data
                    select new
                    {
                        Value= c.Value,
                        CssClass = c.CssClass,
                        Description = c.Desription
                    }).ToArray()
            };
    return Json(jsonData);
}

通过$就调用这一点,并在回调你必须包含所有数据的JavaScript对象,然后使用jQuery建立的选择选项。

Call this via $.ajax and in the callback you'll have a javascript object containing all your data, then use jQuery to build up the select options.

问候