I want to pass htmlAttributes as parameter to my HtmlHelper similar as it created in
Html.ActionLink("linktext", "Home", null, new{width="100px"})
How to pass this new{width="100px"} to my method
public static string SelectCategoryAdminWithAllItem(this HtmlHelper htmlHelper, string name, **???**)
{ }
and how to parse it?
Thanks
Always try to look at sources when interested with this kind of questions. From the implementation of HtmlHelper.TextBox
public static MvcHtmlString TextBox(this HtmlHelper htmlHelper, string name, object value, object htmlAttributes)
{
return htmlHelper.TextBox(name, value, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}
as you see, type of parameter is object as you cant use anonymous types as parameters to methods, and object is choice. And when parsing it, you can use HtmlHelper.AnonymousObjectToHtmlAttributes Method
I looked through the source for MVC2 when trying to figure this one out. In MVC2 they used an overload of RouteValueDictionary in System.Web.Routing to turn an object to a dictionary rather than having a helper method available like in MVC3.
public static MvcHtmlString TextBox(this HtmlHelper htmlHelper, string name, object value, object htmlAttributes)
{
return htmlHelper.TextBox(name, value, new RouteValueDictionary(htmlAttributes));
}
A bit counter intuitive but that's the standard in 2.
Edit: Updated tags to include mvc2
Related
I am trying to create a custom html helper that will actually have several html elements in it including an input type of file and input type of button.
I have that working fine, but I'd also like to include a few hidden inputs and populate the values of those with values from a few of the properties of my model. Is it possible to fetch the values of a few properties from my model in a custom #Html.MyCustomHelperFor(m => m.somefield)
my method signature looks like this:
public static MvcHtmlString MyCustomHelperFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
also, a bit off topic, would I be better suited just writing this up in a partial view?
please advise.
You can access the full model from ViewData
MyCustomHelperFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
{
MyModelClass model = htmlHelper.ViewData as MyModelClass;
string otherProperty = model.OtherProperty;
However this is not very flexible since it will only work for one model type (unless you do conditional checks (if (htmlHelper.ViewData is MyModelClass1) {..} else if (htmlHelper.ViewData is MyModelClass2) {..} etc.
I suggest you would be better of creating a partial view or custom display and editor templates.
I search a lot,but didn't find any solution. I have a product table and here ProductID is guid. And in index.cshtml I have a edit link
#Html.ActionLink("Edit","Edit","Admin",new{id=i.ProductID})
When I click link, url like it
http://localhost:5546/Admin/Edit?Length=5
And I get following error
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Guid' for method 'System.Web.Mvc.ActionResult Edit(System.Guid)' in 'RiotBooks.Controllers.AdminController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters
How to I solve it?
You're using the wrong overload of ActionLink.
The one you're calling there is this one:
public static MvcHtmlString ActionLink(
this HtmlHelper htmlHelper,
string linkText,
string actionName,
Object routeValues,
Object htmlAttributes
)
but you really want to be calling this one:
public static MvcHtmlString ActionLink(
this HtmlHelper htmlHelper,
string linkText,
string actionName,
string controllerName,
Object routeValues,
Object htmlAttributes
)
So just change your call to
#Html.ActionLink("Edit","Edit","Admin",new{id=i.ProductID}, null)
and you'll be good.
For the record, the tip off to me was that ?Length=5 comes from the fact that the string "Admin" being passed into the routeValues parameter has only one property on it (Length) and the length of that string is 5.
In my MVC4 website, I have a page where I'm looping through a list of items that are part of my model and putting them in a table.
My model is named JobModel and it has a list of TaskModel objects accessible in a property named Tasks.
I'm using syntax like this for each item in the list...
#Html.DisplayFor(m => m.Tasks[i].TaskName)
#Html.DisplayFor(m => m.Tasks[i].TaskName)
This works fine.
However, I have a need to create a Custom DisplayFor extension, that I'll name DisplayFor2 for this discussion.
My code looks like this...
public static IHtmlString DisplayFor2<TProperty>(this HtmlHelper<TaskModel> html, Expression<Func<TaskModel, TProperty>> ex) {
//Build my custom html here...
}
When I attempt this code,
#Html.DisplayFor2(m => m.Tasks[i].TaskName)
I get the compiler complaining that:
The type arguments for method 'IHtmlString MySite.HtmlHelpers.HelperExtensions.DisplayFor2<TProperty>(this HtmlHelper<TaskModel>, Expression<Func<TaskModel,TProperty>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
What do I need to do?
*UPDATE*
I changed my method to this, and now it works.
public static IHtmlString TableCellFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> ex)
But when I do this, I don't know the index of the child item, so it's proven useless.
I'm trying to access the ModelMetaData for a Custom HtmlHelper that I'm working on. The HtmlHelper has a signature like so ...
public static MvcHtmlString DataGrid<T>(this HtmlHelper<T> htmlHelper){}
The (Razor) View looks like this ...
#model IEnumerable<LogMyAssets.Models.ContactModel>
....
#Html.DataGrid()
My problem is that I can't access the ModelMetaData for the Model as it's IEnumerable. I thought I
could do the following:
var model = (IEnumerable<T>)htmlHelper.ViewData.Model;
var metaData = model.ElementAt(0).GetMetadata();
public static ModelMetadata GetMetadata<TModel>(this TModel model)
{
return ModelMetadataProviders.Current.GetMetadataForType(null, typeof(TModel));
}
But stangely enough I get the following error:
Unable to cast object of type 'System.Collections.Generic.List`1[LMA.Models.ContactModel]'
to type 'System.Collections.Generic.IEnumerable`1[System.Collections.Generic.IEnumerable`1
I though I could could cast from Generic List to Generic IEnumerable. Am I missing something?
I don't really understand where the T is defined in (IEnumerable<T>), however my Guess is that T is already IEnumerable<something>, which means you're trying to cast to IEnumerable<IEnumerable<something>>
I am adding some functionality to the HtmlHelper-class. Basically I want to automatically disable links on a web page based on user privileges e t c.
So I have this function:
public static string ActionLinkWithPrivileges(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues)
{
return LinkExtensions.ActionLink(htmlHelper, linkText, actionName, routeValues);
}
The problem here is the routeValues-argument. Its usually created as an anonymous type so I dont know what to cast it to. This anonymous type often has a property named "id" but just writing routeValue.id gives me a compiler error.
Any help would be appreciated!
This should work :
RouteValueDictionary routeVals = new RouteValueDictionary(routeValues);
var value = routeVals["key"];
//RouteValueDictionary is under System.Web.Routing
either implement an interface or use reflection to get the PropertyInfo and then itterate through the property collection to get the right one.
you would of course need to tell the method the name of the property to get unless it's of a particular type.