For a string property in MVC3, I've created a partial view at ~/Shared/EditorTemplates/String.cshtml and I've placed within it the following:
#model System.String
<div class="Input">
#Html.TextBox("", this.Model)
#Html.ValidationMessage("")
</div>
Viewing the result, it looks good. But the label is still showing up. As you can see it's not included in the partial view, above, so it must be coming from the base view.
How do I override the label output so when I do #Html.EditorForModel(), my string properties will have a customized label?
Use System.ComponentModel.DisplayName attribute to override the member name.
For example:
public string MyProperty {get;set;} //displays "MyProperty"
[DisplayName("My Property")]
public string MyProperty {get;set;} //displays "My Property"
I ended up creating a custom Object.cshtml template.
Related
Ok.. here we go.. the weirdest and most confusing question of the month :)
I would like to create a HtmlHelper that some how renders html, but uses a partial view for its template of how the html should be rendered, so to put it more simple.. I would like to do exactly the same as a "normal" Controller and view does.. get some data, pass it to the view and then render the html, but in this case I would like to pass some data to a partial view, and then get the returned html as a string and then return that html from a HtmlHelper method...
In this way I would like to be able to write for instance #Html.HeadMenu, that then would return the html for the headmenu, but I would also be able to at anytime without recompiling be able to change the html.. since its all in a partial view.. and I wont have to worry about any server-side things.. and I will also get the benefit of the intellisense since my method will show up in #Html.
I hope you will understand this..since its kind of hard to explain..
Thanks in advance!
How about the Partial HTML-extension method, it sounds like what you are trying to achive right?
#{
var htmlString = Html.Partial("YourPartialViewName").ToString();
}
It also has an overload so that you can pass a model to the partial view:
#{
var htmlString = Html.Partial("YourPartialViewName", partialViewModel).ToString();
}
You could be looking for the Html.RenderAction(actionName, controllerName, routeValues) method.
I would do it this way
Define data that you imagine to pass to your htmlhelper
public class HeadMenuViewModel
{
public string SomeProperty {get;set;}
}
Define view named HeadMenuViewModel.cshtml in Views/Shared/DisplayTemplates
#model HeadMenuViewModel
<div>
////
</div>
From now, you can display your data using
#Html.DisplayFor(model => model.HeadMenu)
And you could write named shortcut-extension for it
using System.Web.Mvc.Html;
...
public static MvcHtmlString HeadMenu<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression)
{
return helper.DisplayFor(expression);
}
Now, change your HeadMenuViewModel.cshtml everytime you need to
So, I am having a hard time getting this to work. I am trying to add some typographic HTML codes to some labels I have strongly typed to my model.
<%: Html.LabelFor(m => m.Name) %>
I am using data annotations to customize the DisplayName of the property.
[DisplayName("Your friend’s name")]
public string Name { get; set; }
However the actual code is being displayed for the label:
Your friend’s name
I have also tried just using the old write method:
<%= Html.LabelFor(m => m.Name) %>
Any help is appreciated.
LabelFor will always encode the string to Html. You may wish to create an override for this helper, to output the string not encoded. Such as Html.HtmlLabelFor()
Source code for MVC is available too if you need to look at the innards of the original helper.
http://www.codeplex.com/aspnet
If you are using MVC3 and Razor then all strings are encoded to HTML when rendered on the page. Then you should to look into the HtmlString class if you return this to a helper class then it will render as HTML.
http://davidhayden.com/blog/dave/archive/2010/12/27/HtmlRawRazorViewEngine.aspx
There seems to be no way to do this. I simple used the actual rendered codes and it displayed fine.
[DisplayName("Your friend’s name")]
public string Name { get; set; }
I have created an asp.net custom control and now want to pass some values to it from the page which will use the control.Is there any way i can add my own property to the control (like Text property is present for a Label control) ?
It's a class. Add a public property to it.
Assuming ASP.NET 3.5+ just create properties
public string YourProperty {get; set;}
Would be declarative on the control
Adding a property to a custom control in asp.net is no different than adding a property on any class in C# (for instance).
public class Custom : Control
{
public string Text { get; set; }
}
just add it as public property, then it should be useable for your needs.
Im trying to create a "user control menu" where links to a page's usercontrols are placed at the top of the page. This will allow me to put several usercontrols on a page and allow the user to jump to that section of the page without scrolling so much. In order to do this, I put each usercontrol in a folder (usercontrols) and gave each control a Description property (<%# Control Language="C#" Description = "Vehicles" .... %>).
My question is how can I access this description dynamically? I want to use this description as the link in my menu. So far, I have a foreach on my page that looks in the ControlCollection for a control that is of the ASP.usercontrols type. If it is I would assume that I could access its attributes and grab that description property. How can I do this? (Im also open to a better way to achieve my "user control menu", but maybe thats another question.) Should I use ((System.Web.UI.UserControl)mydynamiccontrol).Attributes.Keys?
you can iterate over the collection and do either a switch or a few if statements
I would suggst you have an interface or an abstract base class for all your user controls:
public abstract class MyBaseClass : UserControl
{
public abstract string MyDescription {get;}
}
public MyUserControlA : MyBaseClass
{
public string MyDescription {get {return "my description";}}
}
public MyUserControlB : MyBaseClass
{
public string MyDescription {get {return "my other description";}}
}
Then you can loop over them as you do:
foreach ...
if (mydynamiccontrol is MyBaseClass)
{
Response.Write(((MyBaseClass)mydynamiccontrol).MyDescription);
}
Hope this helps
I have a Dynamic Data website and while inserting a record into a table, the foreign key relationship shown as a drop-down uses the wrong field for it's select item text value.
How can I change the drop-down such that when working with this one table, it will use a different column as the value in the drop-down?
thank you
The solution is to add a partial class with some attributes from the System.ComponentModel.DataAnnotations namespace & assembly. Notice the [DisplayColumn("Description")] below. That's what field is used to render as the text in a list.
Further reading
[MetadataType(typeof(ProductMetadata))]
**[DisplayColumn("Description")]**
[ScaffoldTable(true)]
public partial class Product
{
}
public class ProductMetadata
{
[UIHint("TextReadOnly")]
public string CreatedBy;
[UIHint("TextReadOnly")]
public string CreatedDate;
[ScaffoldColumn(false)]
public EntityCollection<OrderItem> OrderItem;
}