html-helper

Testing HtmlHelpers in ASP.NET MVC

杀马特。学长 韩版系。学妹 提交于 2019-11-28 04:43:40
Is there any way to (unit) test my own HtmlHelpers? In case when I'd like to have custom control (rendered by HtmlHelper) and I know requierements for that control how could I write tests first - and then write code? Is there a specific (nice) way to do that? Is it worth? Marc Climent The main problem is that you have to mock the HtmlHelper because you may be using methods of the helper to get routes or values or returning the result of another extension method. The HtmlHelper class has quite a lot of properties and some of them quite complex like the ViewContext or the current Controller.

MVC 3: Conditionally Adding the Disabled Attribute with the HtmlHelpers

倖福魔咒の 提交于 2019-11-28 04:36:52
I have an ASP.Net MVC 3 web application and I am adding a check box to a view page using the HtmlHelper class, like this... @Html.CheckBox("CheckBox1", true, new { @class = "Class1" }) What I want to do is conditionally add the disabled attribute based on a view state property. Basically the following would be ideal... @Html.CheckBox("CheckBox1", true, new { @class = "Class1", @disabled = Model.ReadOnly }) Unfortunately, due to the nature of the disabled attribute, this will not work because any value assigned to the disabled attribute (even "false") will be translated as true. I have already

How to create readonly textbox in ASP.NET MVC3 Razor

那年仲夏 提交于 2019-11-28 04:26:29
How to create a readonly textbox in ASP.NET MVC3, With Razor view engine ? Is there an HTMLHelper method available to do that ? Something like this ? @Html.ReadOnlyTextBoxFor(m => m.userCode) @Html.TextBoxFor(m => m.userCode, new { @readonly="readonly" }) You are welcome to make an HTML Helper for this, but this is simply just an HTML attribute like any other. Would you make an HTML Helper for a text box that has other attributes? UPDATE: Now it's very simple to add html attributes to default editor templates. Means instead of doing this: @Html.TextBoxFor(m => m.userCode, new { @readonly=

How to use @Html.CheckBoxFor() while dealing with a List<ViewModel>

泪湿孤枕 提交于 2019-11-28 04:21:51
问题 This is my View. How to use CheckboxFor(): @using eMCViewModels; @model eMCViewModels.RolesViewModel @{ ViewBag.Title = "CreateNew"; } <h2> CreateNew< /h2> @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>RolesViewModel</legend> <div class="editor-label"> @Html.LabelFor(model => model.Name) </div> <div class="editor-field"> @Html.EditorFor(model => model.Name) @Html.ValidationMessageFor(model => model.Name) </div> <div> @foreach (RoleAccessViewModel mnu in Model

There's no @Html.Button !

蓝咒 提交于 2019-11-28 04:02:38
this is weird. I see references out there for @Html.Button() but when I type that Intellisense doesn't find such a helper... there's dropdownlist, hidden, editors, et cetera, but no button! what's up with that? public static class HtmlButtonExtension { public static MvcHtmlString Button(this HtmlHelper helper, string innerHtml, object htmlAttributes) { return Button(helper, innerHtml, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes) ); } public static MvcHtmlString Button(this HtmlHelper helper, string innerHtml, IDictionary<string, object> htmlAttributes) { var builder = new

ASP.NET MVC: Hidden field value does not get rendered using HtmlHelper.Hidden

半世苍凉 提交于 2019-11-28 03:22:33
Something pretty weird is happening with my app: I have the following property in my ViewModel: public int? StakeholderId { get; set; } It gets rendered in a partial view as follows: <%= Html.Hidden("StakeholderId", Model.StakeholderId) %> The form is submitted, and the relevant controller action generates an id and updates the model, before returning the same view with the updated model The problem I'm experiencing is that the hidden field does not have anything in its "value" attribute rendered the second time even though StakeholderId now has a value. If I just output the value on its own,

How to remove the default link color of the html hyperlink 'a' tag?

孤人 提交于 2019-11-28 03:02:44
The default link color is blue. How to remove the default link color of the html hyperlink tag <a> ? The inherit value : a { color: inherit; } … will cause the element to take on the colour of its parent (which is what I think you are looking for). you can do some thing like this: a { color: #0060B6; text-decoration: none; } a:hover { color:#00A0C6; text-decoration:none; cursor:pointer; } .cancela,.cancela:link,.cancela:visited,.cancela:hover,.cancela:focus,.cancela:active{ color: inherit; text-decoration: none; } I felt it necessary to post the above class definition, many of the answers on

How can I add a class attribute to an HTML element generated by MVC's HTML Helpers?

自作多情 提交于 2019-11-28 02:55:59
ASP.NET MVC can generate HTML elements using HTML Helpers, for example @Html.ActionLink() , @Html.BeginForm() and so on. I know I can specify form attributes by creating an anonymous object and pass that object for the (fourth in this case) htmlAttributes parameter where specifying an id for the element: Html.BeginForm("Foo", "Bar", FormMethod.Post, new { id = "MyForm"}) But what about the class attribute? Obviously this does not work: Html.BeginForm("Foo", "Bar", FormMethod.Post, new { class = "myclass"}) As that just throws random syntax errors when my view is requested, because it expects

Generate URL in HTML helper

心已入冬 提交于 2019-11-28 02:54:21
Normally in an ASP.NET view one could use the following function to obtain a URL (not an <a> ): Url.Action("Action", "Controller"); However, I cannot find how to do it from a custom HTML helper. I have public class MyCustomHelper { public static string ExtensionMethod(this HtmlHelper helper) { } } The helper variable has the Action and GenerateLink methods, but they generate <a> ’s. I did some digging in the ASP.NET MVC source code, but I could not find a straightforward way. The problem is that the Url above is a member of the view class and for its instantiation it needs some contexts and

ASP.NET MVC 3 Custom HTML Helpers- Best Practices/Uses

徘徊边缘 提交于 2019-11-28 02:45:41
New to MVC and have been running through the tutorials on the asp.net website. They include an example of a custom html helper to truncate long text displayed in a table. Just wondering what other solutions people have come up with using HTML helpers and if there are any best practices or things to avoid when creating/using them. As an example, I was considering writing a custom helper to format dates that I need to display in various places, but am now concerned that there may be a more elegant solution(I.E. DataAnnotations in my models) Any thoughts? EDIT: Another potential use I just