html-helper

Capture wrapped content in BeginForm style disposable html helper

喜你入骨 提交于 2019-11-26 17:13:30
问题 I am trying to write a BeginForm style html helper that uses IDisposable to wrap other code. I want the helper to only render the wrapped code if a certain condition is met (e.g. user is in a certain role). I thought that I could simply switch the context.Writer in the Begin method and switch it back in the Dispose method. The code below compiles and runs but the wrapped content gets rendered in all cases. If I step through it, the wrapped content is not written to the new StringWriter and

How to add static list of items in MVC Html.DropDownList()

我只是一个虾纸丫 提交于 2019-11-26 16:59:44
问题 I would like to assign a static list of items in a SelectList() to a Html.DropDownList() in ASP.NET MVC, what is the best practice? I was about to try to find a way to use new SelectList(new {key = "value"}... but one, that didn't work, and two, would I be breaking a law here, should my static list be declared in ViewData anyway and passed as IList/IENumerable ? 回答1: It is a best practice not to create the SelectList in the view. You should create it in the controller and pass it using the

How to specify ID for an Html.LabelFor<> (MVC Razor) [duplicate]

↘锁芯ラ 提交于 2019-11-26 16:44:40
问题 Possible Duplicate: Client Id for Property (ASP.Net MVC) Is there a way to make Razor render an ID attribute for a Label element when using the Html.LabelFor<> helper? Example: .cshtml page @using (Html.BeginForm()) { @Html.LabelFor(m => m.Foo) @Html.TextBoxFor(m => m.Foo) } Rendered page <form ...> <label for="Foo" id="Label_Foo" /> <input type="text" name="Foo" id="Foo" /> </form> FYI - The sole reason I'm wanting to add an ID to the Label is for CSS design. I'd prefer to reference the

Creating MVC3 Razor Helper like Helper.BeginForm()

社会主义新天地 提交于 2019-11-26 16:33:21
问题 I want to create a helper that i can add content between the brackets just like Helper.BeginForm() does. I wouldnt mind create a Begin, End for my helper but it's pretty simple and easy to do it that way. basically what i am trying to do is wrapping content between these tags so they are rendered already formatted something like @using Html.Section("full", "The Title") { This is the content for this section <p>More content</p> @Html.TextFor("text","label") etc etc etc } the parameters "full"

Converting HTML.EditorFor into a drop down (html.dropdownfor?)

你离开我真会死。 提交于 2019-11-26 16:16:32
问题 Currently I am using a Html.EditorFor control in a default 'Create' View page like this. <%: Html.EditorFor(model => model.IsActive) %> I would like to convert this to a drop down with values and still be binded to the model in the view. My question is two fold. If there are only 2/3 values needed in the drop down..Is there a quick way to explicitly populate 2 or 3 values? If the list is big and needs to come from a sql query, how to do this? Thanks in advance for the help. 回答1: In order to

Conditional html attribute with Html Helper

泄露秘密 提交于 2019-11-26 14:51:40
问题 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

MVC HTML Helpers and Lambda Expressions

瘦欲@ 提交于 2019-11-26 13:57:25
问题 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

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

折月煮酒 提交于 2019-11-26 13:45:58
问题 The Html.RouteLink() HtmlHelper works great for text links. But what's the best way to link an image? 回答1: 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

Using an MVC HtmlHelper from a WebForm

假如想象 提交于 2019-11-26 13:18:50
问题 I'm in the process of adding some UI functionality to a hybrid WebForms/MVC site. In this case, I'm adding some AJAX UI features to a WebForms page (via jQuery), and the data is coming from an MVC JsonResult. Everything is working 100%, with one exception: I would like to implement the XSRF protection of AntiForgeryToken. I have used it in combination with the ValidateAntiForgeryToken attribute on my pure MVC applications, but would like to know how to implement the Html.AntiForgeryToken()

What&#39;s the difference between RouteLink and ActionLink in ASP.NET MVC?

笑着哭i 提交于 2019-11-26 12:38:06
问题 I think that the title pretty much sums it up: What\'s the difference between RouteLink() and ActionLink() in ASP.NET MVC? i.e. when do you use Html.RouteLink() and when do you use Html.ActionLink() in your View? 回答1: Action and Routes don't have to have a 1:1 relationship. ActionLink will generate the URL to get to an action using the first matching route by action name. RouteLink will generate a URL to a specific route determined either by name or route values. 回答2: Actually, the output