html-helper

Extract Display name and description Attribute from within a HTML helper

寵の児 提交于 2019-11-29 01:41:56
问题 I am building a custom HTML.LabelFor helper that looks like this : public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> self, Expression<Func<TModel, TValue>> expression, Boolean showToolTip) { var metadata = ModelMetadata.FromLambdaExpression(expression, self.ViewData); ... } To be able to get the proper name for the property I am using the following code : metadata.DisplayName And on the property of the ModelView class I got : [DisplayName("Titel")] The problem is

Create using for own helper? like Html.BeginForm

天大地大妈咪最大 提交于 2019-11-28 23:43:35
I was wondering, is it possible to create your own helper definition, with a using? such as the following which creates a form: using (Html.BeginForm(params)) { } I'd like to make my own helper like that. So a simple example I'd like to do using(Tablehelper.Begintable(id) { <th>content etc<th> } which will output in my view <table> <th>content etc<th> </table> Is this possible? if so, how? Thanks Sure, it's possible: public static class HtmlExtensions { private class Table : IDisposable { private readonly TextWriter _writer; public Table(TextWriter writer) { _writer = writer; } public void

ASP.NET MVC - Html.DropDownList - Value not set via ViewData.Model

泪湿孤枕 提交于 2019-11-28 20:36:50
Have just started playing with ASP.NET MVC and have stumbled over the following situation. It feels a lot like a bug but if its not, an explanation would be appreciated :) The View contains pretty basic stuff <%=Html.DropDownList("MyList", ViewData["MyListItems"] as SelectList)%> <%=Html.TextBox("MyTextBox")%> When not using a model, the value and selected item are set as expected: //works fine public ActionResult MyAction(){ ViewData["MyListItems"] = new SelectList(items, "Value", "Text"); //items is an ienumerable of {Value="XXX", Text="YYY"} ViewData["MyList"] = "XXX"; //set the selected

DateTime field and Html.TextBoxFor() helper. How to use it correctly?

隐身守侯 提交于 2019-11-28 19:23:14
I have a DateTime field in my Model. If I try to use this field in a strong typed partial view this way <%= Html.TextBoxFor(model => model.DataUdienza.ToString("dd/MM/yyyy"), new { style = "width: 120px" }) %> I will get the following compilation error at runtime System.InvalidOperationException : Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions. Anyway if I use it removing the formatting, ToString("dd/MM/yyyy") , everything works but the field is formatted using the time part which I dont need at all.

ASP.NET MVC 2 - Html.DropDownListFor confusion with ViewModel

亡梦爱人 提交于 2019-11-28 18:42:07
问题 I'm getting totally lost and confused on how to use the new strongly typed Html.DropDownListFor helper on ASP.NET MVC 2.0 R2 In the View I'm writting: <%= Html.DropDownListFor(m => m.ParentCategory, new SelectList(Model.Categories, "CategoryId", "Name", Model.ParentCategory), "[ None ]")%> <%= Html.ValidationMessageFor(m => m.ParentCategory)%> and my Model object is thus: public class CategoryForm : FormModelBase { public CategoryForm() { Categories = new List<Category>(); Categories.Add(new

How to pass Area in Url.Action?

别等时光非礼了梦想. 提交于 2019-11-28 18:33:05
The problem in Html.ActionLink() is that you can't add additional html content inside the tag that it generates. For example, if you want to add an icon besides the text like: <a href="/Admin/Users"><i class="fa fa-users"></i> Go to Users</a> Using Html.ActionLink(), you can only generate: <a href="/Admin/Users">Go to Users</a> So, to resolve this, you can use Url.Action() to generate only the URL inside the tag like: // Here, Url.Action could not generate the URL "/admin/users". So this doesn't work. <a href="@Url.Action("", "Users", "Admin")"><i class="fa fa-usesr"></i> Go to Users</a> //

How to concatenate several MvcHtmlString instances

ぃ、小莉子 提交于 2019-11-28 18:31:05
I have some doubts about how to concatenate MvcHtmlString instances because of this information found in MSDN : MvcHtmlString Class Represents an HTML-encoded string that should not be encoded again Do I risk that strings are HTML-encoded twice when using code like this: var label = Html.LabelFor(model => model.Email); var textbox = Html.TextBoxFor(model => model.Email); var validation = Html.ValidationMessageFor(model => model.Email); var result = MvcHtmlString.Create( label.ToString() + textbox.ToString() + validation.ToString()); (note: this is supposed to go into an HtmlHelper extension

MVC: Override default ValidationMessage

时光怂恿深爱的人放手 提交于 2019-11-28 18:23:23
In the world of MVC I have this view model... public class MyViewModel{ [Required] public string FirstName{ get; set; } } ...and this sort of thing in my view... <%= Html.ValidationSummary("Please correct the errors and try again.") %> <%= Html.TextBox("FirstName") %> <%= Html.ValidationMessage("FirstName", "*") %> My question: If I submit this form without supplying a name, I get the following message "The FirstName field is required" OK. So, I go and change my property to... [DisplayName("First Name")] [Required] public string FirstName{ get; set; } ..and now get "The First Name field is

ASP.NET MVC 3 HtmlHelper Exception does not recognize ModelMetadata on inherited interface

梦想的初衷 提交于 2019-11-28 18:23:09
After upgrading to MVC 3 RTM I get an exception where it previously worked. Here is the scenario. I have several objects that use the same underlying interfaces IActivity and IOwned. IActivity implements IOwned (another interface) public interface IActivity:IOwned {...} public interface IOwned { int? AuthorId {get;set;} } I have a partial view that uses IActivity for reuse from other concrete partials. Here is the definition of the Activity Partial. <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IActivity>" %> <%: Html.HiddenFor(item => item.AuthorId) %> However, it throws

URL.Action() including route values

别等时光非礼了梦想. 提交于 2019-11-28 18:13:25
问题 I have an ASP.Net MVC 4 app and am using the Url.Action helper like this: @Url.Action("Information", "Admin") This page is used for both adding a new and edit an admin profile. The URLs are as follows: Adding a new: http://localhost:4935/Admin/Information Editing Existing: http://localhost:4935/Admin/Information/5 <==Admin ID When I'm in the Editing Existing section of the site and decide that I would like to add a new admin I click on the following link: <a href="@Url.Action("Information",