html-helper

How do I generate a URL outside of a controller in ASP.NET MVC?

三世轮回 提交于 2019-12-03 09:24:22
How do I generate a URL pointing to a controller action from a helper method outside of the controller? Pass UrlHelper to your helper function and then you could do the following: public SomeReturnType MyHelper(UrlHelper url, // your other parameters) { // Your other code var myUrl = url.Action("action", "controller"); // code that consumes your url } L01NL You could use the following if you have access to the HttpContext : var urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext); Alexei Using L01NL's answer, it might be important to note that Action method will also get

How to write HtmlHelper in Fluent syntax

两盒软妹~` 提交于 2019-12-03 09:15:11
I have a simple tag builder that looks like this: public static MvcHtmlString Tag(this HtmlHelper helper, string tag, string content) { var tagBuilder = new TagBuilder(tag){InnerHtml = content}; return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.NormalTag)); } And, I can use it like this: @Html.Tag("em", Model.Title) which produces: <em>The Title</em> How can this be written to use a Fluent Syntax so it's use would look like this: @Html.Tag("em").Content(Model.Title) You have to define a builder interface and implementation. I hope my example can provide some guidance: public static

How to Unit Test HtmlHelper with Moq?

我只是一个虾纸丫 提交于 2019-12-03 08:12:20
问题 Could somebody show me how you would go about creating a mock HTML Helper with Moq? This article has a link to an article claiming to describe this, but following the link only returns an ASP.NET Runtime Error [edit] I asked a more specific question related to the same subject here, but it hasn't gotten any responses. I figured it was too specific, so I thought I could get a more general answer to a more general question and modify it to meet my requirements. Thanks 回答1: What you can do is

Is this overkill, or good use of CakePHP's HTML helper?

有些话、适合烂在心里 提交于 2019-12-03 07:58:11
I just reformatted the default layout of my CakePHP application. I eliminated as much in-line html as possible by putting almost everything inside the html helper methods. It was fun, but I'm wondering what benefit I've gained from this exercise, if any? <?php $output = implode("\n", array( $html->docType(), $html->tag('html', implode("\n", array( $html->tag('head', implode("\n", array( $html->charset(), $html->tag('title', 'Title For App'), $html->css('css', NULL, array('media' => 'screen,print')), $html->css('print', NULL, array('media' => 'print')), $html->script(array('cufon', 'jquery',

Html.BeginForm() type of extension

折月煮酒 提交于 2019-12-03 07:50:40
Does anyone know the syntax for creating a custom HtmlHelperextension method which behaves like.. <% using (Html.BeginForm()) {%> <p>Loads of html stuff here </p> <% } %> I'm thinking of something along the lines of.... Any ideas? Cheers, ETFairfax You need to create a class that implements IDisposable interface and return that from your HtmlHelper . public static class HtmlHelperTableExtensions { private class TableRenderer : IDisposable { HtmlHelper html; public TableRenderer(HtmlHelper html) { this.html = html; } public void Dispose() { HtmlHelperTableExtensions.EndTable(html); } } public

dropdown in mvc3 edit form

大憨熊 提交于 2019-12-03 07:14:16
问题 This maybe very simple but I cant seem to sort it out on my own. I have created a simple db and entity modal that looks like this I am trying to create an Create form that allows me to add a new Order. I have a total of 3 tables so what I am trying to do is have the form allowing the person to enter Order date and also has a dropdown list that allows me to select a product from the product table I want to be able to create a Add or Edit view that allow me to insert the OrderDate into the

Find Area name and Controller Name in custom Htmlhelper with ASP.NET MVC3

坚强是说给别人听的谎言 提交于 2019-12-03 06:20:56
I try to rewrite and customize @Html.ActionLink , in one of overloads of this method the parameters are: public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName); And I want something like the above and also need to find AreaName and ControllerName without pass it by parameters, I think to use the followings: string controller = ViewContext.RouteData.Values["Controller"]; string area = ViewContext.RouteData.DataTokens["Area"]; but the error rise as : An object reference is required for the non-static field, method, or property 'System.Web.Mvc

Asp.Net MVC 2 Html.TextBoxFor Best way to specify a format string for a DateTime property of model

依然范特西╮ 提交于 2019-12-03 04:59:43
问题 As the question title explains, what is the best way to specify a format which my Views use when displaying values of a DateTime property via the Html.TextboxFor method. The default display includes the Date and Time where I really only want the Date displayed. Thanks 回答1: In your model definition add the DisplayFormatAttribute to the DateTime property: [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")] public DateTime Date { get; set; } Then in your View call:

Model Binding on Multiple Model Form Submission from Strongly-Typed View

雨燕双飞 提交于 2019-12-03 04:34:54
问题 I'm having problems binding on a form with multiple models being submitted. I have a complaint form which includes complaint info as well as one-to-many complainants. I'm trying to submit the form but I'm getting errors on the bind. ModelState.IsValid always returns false. If I debug and view the ModelState Errors, I get one saying: "The EntityCollection has already been initialized. The InitializeRelatedCollection method should only be called to initialize a new EntityCollection during

Adding CSS class to Html.BeginForm()

て烟熏妆下的殇ゞ 提交于 2019-12-03 02:31:17
问题 How to add class attribute for following situation (Using ReturnUrl only): @using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl })) { } I want something like this: @using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl }, new { @class = "login-form" })) { } 回答1: There is an overload for that, if you supply the controller action, name, and form method. @using (Html.BeginForm("ActionName", "ControllerName", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class="login