Dan*_*Dan 5 .net html c# asp.net-mvc-3
所以我试图在下拉列表中设置标签的样式.我的MVC助手设置类似这样,但真实的东西来自数据库,因此数据不是硬编码的.
@{
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)
Run Code Online (Sandbox Code Playgroud)
这给了我类似的东西
<select id="Name" name="Name">
<option value="1">FirstThing</option>
<option value="2">SecondThing</option>
<option value="3">ThirdThing</option>
</select>
Run Code Online (Sandbox Code Playgroud)
我需要的是这样的东西
<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>
Run Code Online (Sandbox Code Playgroud)
我尝试过这样的事情
@{
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>
Run Code Online (Sandbox Code Playgroud)
但这似乎混淆了控制器,它无法识别上面的下拉值映射到
public ActionResult method(string Name)
Run Code Online (Sandbox Code Playgroud)
在一个帖子或得到的控制器.那行
@Html.DropDown("NAME",ListOFSELCETLISTITEMS)
Run Code Online (Sandbox Code Playgroud)
在视图中确实允许控制器理解该方法应该映射到该下拉列表的值.
我怎样才能做到这一点?有没有办法解除控制器的控制,并能够在没有Html.helpers的情况下手写HTML?
要直接渲染控制器操作,您可以尝试:
@{ Html.RenderAction(Action, Controller); }
调用您的控制器操作,该操作应该返回包含您的内容的字符串。
[HttpGet]
public string Action(int id)
{
return your context in here
}
Run Code Online (Sandbox Code Playgroud)
不过我认为添加一个 Ajax 操作返回用于构建选择的数据并在获得结果后使用 jquery 解决方案添加一个类(可以在 AJAX 响应本身中返回)会更干净
编辑:澄清:
假设您有一个 Item 集合,如下所示:
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();
}
Run Code Online (Sandbox Code Playgroud)
对于 ajax 选项:
[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);
}
Run Code Online (Sandbox Code Playgroud)
通过 $.ajax 调用此函数,在回调中您将拥有一个包含所有数据的 javascript 对象,然后使用 jQuery 构建选择选项。
问候
| 归档时间: |
|
| 查看次数: |
11008 次 |
| 最近记录: |