C# 如何从控制器中的DropDownListFor获取MvcHtmlString
我试过这个:C# 如何从控制器中的DropDownListFor获取MvcHtmlString,c#,asp.net-mvc,razor,html.dropdownlistfor,C#,Asp.net Mvc,Razor,Html.dropdownlistfor,我试过这个: public ActionResult RebuildDropDown(string selectedItem) { bool error = false; MvcHtmlString drop = null; try { var helper = new HtmlHelper<Asset>(new ViewContext(), new ViewPage()); //SelectList made fr
public ActionResult RebuildDropDown(string selectedItem)
{
bool error = false;
MvcHtmlString drop = null;
try
{
var helper = new HtmlHelper<Asset>(new ViewContext(), new ViewPage());
//SelectList made from List<SelectListItem>
var selectList = Asset.CriarSelectList(selectedItem);
//x.Id is string
drop = helper.DropDownListFor(x => x.Id, selectList);
}
catch (System.Exception e)
{
error = true;
}
return Json(new { error, drop });
}
你到底想完成什么?为什么?如果您试图通过ajax获取html片段,那么请使用视图。否则,返回json并在javascript中将其处理为html dom节点。
drop = helper.DropDownListFor(x => x.Id, selectList);