html-helper

How to pass Area in Url.Action?

孤街醉人 提交于 2019-11-27 11:26:39
问题 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

MVC: Override default ValidationMessage

耗尽温柔 提交于 2019-11-27 11:18:24
问题 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(

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

风格不统一 提交于 2019-11-27 11:17:05
问题 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

ASP.NET MVC HTML helper methods for new HTML5 input types

一个人想着一个人 提交于 2019-11-27 11:04:39
问题 HTML5 appears to support a new range of input fields for things such as: Numbers Email addresses Colors URLs Numeric range (via a slider) Dates Search boxes Has anyone implemented HtmlHelper extension methods for ASP.NET MVC that generates these yet? It's possible to do this using an overload that accepts htmlAttributes , such as: Html.TextBoxFor(model => model.Foo, new { type="number", min="0", max="100" }) But that's not as nice (or typesafe) as: Html.NumericInputFor(model => model.Foo, min

What is the difference between Html.Hidden and Html.HiddenFor

两盒软妹~` 提交于 2019-11-27 11:03:15
问题 I can find a good definition for Html.HiddenFor on MSDN but the only thing I can find on Html.Hidden is related to problems it has. Can someone give me a good definition and an example. 回答1: Most of the MVC helper methods have a XXXFor variant. They are intended to be used in conjunction with a concrete model class. The idea is to allow the helper to derive the appropriate "name" attribute for the form-input control based on the property you specify in the lambda. This means that you get to

Using helpers in rails 3 to output html

让人想犯罪 __ 提交于 2019-11-27 10:31:32
问题 I'm trying my best to build a helper that outputs a <'ul> consisting of all the members of a collection. For each member of the collection I want to print out a <'li> that has a title, and a div of links to CRUD the member. This is pretty similar to what Rails outputs for scaffolding for the index view. Here is the helper I've got: def display_all(collection_sym) collection = collection_sym.to_s.capitalize.singularize.constantize.all name = collection_sym.to_s.downcase html = '' html << "<ul

Conditional html attribute with Html Helper

。_饼干妹妹 提交于 2019-11-27 09:47:06
I am using a Html helper to create a checkbox. Pending some condition, I want to add the disabled attribute to the htmlAttribute object. I have the following code: @if (Model.IsAuthorized) { @Html.CheckBoxFor(x => @Model.Property, new { @class = "input-class" }) } else { @Html.CheckBoxFor(x => @Model.Property, new { @class = "input-class", @disabled = "disabled" }) } I'd like to make this code more terse. Is there a way to conditionally add certain html attributes in one line/without a block conditional? While you could use @Html.CheckBoxFor(m => m.Property, Model.IsAuthorized ? (object)new {

How can I create a Html Helper like Html.BeginForm

陌路散爱 提交于 2019-11-27 08:05:55
I have an Extension Method that verifies if the user is able to see a portion of the webpage, based on a Role. If I simple remove the content, this brings me more work as all the missing forms will not be correctly registered upon save and I have to deal with this behavior by modifying all my code, so I thought why not just use display:none; attribute? I would like to have something like: @using(Html.RoleAccess(currentUser, RoleAccessType.Content_General_Website)) { ... } and that this would write something like: <div class="role_Content_General_Website" style="display:none;"> ... </div> or

MVC HTML Helpers and Lambda Expressions

亡梦爱人 提交于 2019-11-27 08:01:47
I understand Lambda queries for the most part, but when I am trying to learn MVC, and I see the default Scaffolding templates, they use Lambda expressions for so many components. One for example is the DisplayFor HTML Helper. The code goes @Html.DisplayFor(model => model.name) I hope no one thinks this is a stupid question, it is just that whilst I (think I) understand Lambda expressions for the most part, they don't "flow" like regular code and I have to think about it quite hard to understand what is actually happening! So the question really is, 1) is there any benefit that I am missing to

Is there an ASP.NET MVC HtmlHelper for image links?

不羁岁月 提交于 2019-11-27 07:40:56
The Html.RouteLink() HtmlHelper works great for text links. But what's the best way to link an image? MiniScalope Here is mine, it`s the core function make some overloads public static string ImageLink(this HtmlHelper htmlHelper, string imgSrc, string alt, string actionName, string controllerName, object routeValues, object htmlAttributes, object imgHtmlAttributes) { UrlHelper urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url; string imgtag = htmlHelper.Image(imgSrc, alt,imgHtmlAttributes); string url = urlHelper.Action(actionName, controllerName, routeValues); TagBuilder imglink