Reputation: 4538
I want to do get the HTML from an Html.DropDownList in a controller. This is the code I have so far:
var binMasters = repository.LoadBinsByZone(12345);
var ddl = new SelectList(binMasters, "BinID", "BinCode");
string result = ddl.ToString();
return result;
The result is "System.Web.Mvc.SelectList" instead of the HTML. How do I get the HTML from an Html.DropDownList in the controller?
Upvotes: 0
Views: 343
Reputation: 18237
Why you just create a method in your controller that returns an action result which it's the html associated to the view that you want to render
//this if you want get the html by get
public ActionResult Foo()
{
return View();
}
And the client called like this
$.get('your controller path', parameters to the controler , function callback)
or
$.ajax({
type: "GET",
url: "your controller path",
data: parameters to the controler
dataType: "html",
success: your function
});
Also you can load partial views, and render in a specific parts of your view with the jquery load which it's no more than a ajax called
Upvotes: 2
Reputation: 218847
There is no "HTML DropDownList" in the Controller. The HTML hasn't been rendered until it goes through the view. (In fact, you specifically shouldn't want to have anything to do with the raw HTML or any View-level functionality within the Controller. Those concerns should be distinctly separated.)
The reason it shows you "System.Web.Mvc.SelectList"
is because what you're doing is calling .ToString()
on a reference object (ddl
which is of type System.Web.Mvc.SelectList
). The default behavior of .ToString
on object
is to return the name of the class. (Which makes sense, given that object
has no knowledge of what other functionality its descendants may present.)
A bigger question remains... Why do you need to do this? As I said, the Controller shouldn't know or care about the View that's going to be generated from it. Indeed, the Controller should be re-usable for other Views as well. So it occurs to me that there's probably a much better way to solve the root problem you're facing if we take a step back.
Upvotes: 1
Reputation: 108957
Try
var Html = new HtmlHelper(new ViewContext(ControllerContext, new WebFormView(""), new ViewDataDictionary(), new TempDataDictionary()), new ViewPage());
var result = Html.DropDownList("dropDownList", ddl).ToHtmlString();
Upvotes: 0