html-helper

Adding a onclick dynamically using an Html Helper in MVC

自作多情 提交于 2019-12-08 11:18:47
问题 I need the ability to dynamically set an onclick using an HTML Helper. the below is what I'm trying to do, but i'm getting an obvious syntax error <%=Html.CheckBox("checkboxname", item.Id = 3, New With {.onclick = "ajaxThis(this, <%= Html.Encode(item.ID) %>, '<%= Html.Encode(item.NUMBER) %>');"})%> 回答1: The first step would be to remove the <%= %> from <%= Html.Encode(item.ID) %> and just call Html.Encode(item.ID) directly. Do the same with the item.NUMBER encoding. Something like: "ajaxThis

Model Property Display Name using Reflection

我怕爱的太早我们不能终老 提交于 2019-12-08 09:21:42
问题 I am working on a project where I need to output a few hundred properties on to the screen. To save myself lots of tedious markup, I decided to use reflection. //markup removed to keep this concise @for (var i = 0; i < Model.SiteAndJobDetails.GetType().GetProperties().Count(); i++) { @Model.SiteAndJobDetails.GetType().GetProperties()[i].Name @Model.SiteAndJobDetails.GetType().GetProperties()[i].GetValue(Model.SiteAndJobDetails, null) } Although slower to render, this will save me writing out

Creating recursive method helper with Func<>

断了今生、忘了曾经 提交于 2019-12-08 06:03:46
问题 I have two method helper with theses signatures: @helper CreateNavigation(int parentId, int depthNavigation) @helper Navigation(int parentId, int depthNavigation) I have tried to convert it to a proper method for using in ASP.NET Core 2 . But I got an error in VS2017 IDE that is: 1- Use of unassigned local variable 'Navigation' 2- local variable 'Navigation' might not be initialized before accessing How can I fix it? @using Microsoft.AspNetCore.Html @using Microsoft.AspNetCore.Mvc.Razor

Strange behavior in helper method DropDownListFor

耗尽温柔 提交于 2019-12-08 03:56:35
问题 I see a weird thing in DropDownListFor . This is a part of My View: 1:<dt> 2: @Html.LabelFor(model => model.MainModel.DefaultAreaId) 3:</dt> 4:<dd> 5: @Html.DropDownListFor(model => model.MainModel.DefaultAreaId, Model.FKs["DefaultAreaId"].Items, new { @class = "Areadd" }) 6: @Html.ValidationMessageFor(model => model.MainModel.DefaultAreaId, null, new { @class = "invalid-side-note" }) 7:</dd> I Check in Debug the Last Line of My Controller: return View(model); And the values of Model.FKs[

ASP.NET MVC ActionLink renders erroneously

余生颓废 提交于 2019-12-08 03:36:28
问题 SO community I have the following issue: If i render an ActionLink like this: @Html.ActionLink(titleText, Title.Href.TargetAction, Title.Href.TargetController, Title.Href.TargetRouteValues, null) the rendered element is: <a href="/eagle/Intervention/Edit_Inv?ID_INV=53165"> 19/ 2013</a> but if i add an object as HTMLAttributes like this: @Html.ActionLink(titleText, Title.Href.TargetAction, Title.Href.TargetController, Title.Href.TargetRouteValues, new {target="_blank"}) i get the following

HTML helpers in ASP.NET MVC 3 with Javascsript action

旧城冷巷雨未停 提交于 2019-12-08 03:06:17
问题 I have many HTML helper in Helpers.cshtml file, but some of the helper (html) need some jquery action, so how do i can call jquery inside helpers.cshtml, is that possible? i know we can keep the js file in header or particular page, but i do not want to do like that, i want to use jquery or javascript only on the page which loaded particular helper. anyone have idea on this? My scenario is, i have list box control, that is properly loading from helper, but i need to apply custom theme to the

ASP.NET MVC 4 HtmlHelper - mix decoded HTML with encoded HTML

混江龙づ霸主 提交于 2019-12-08 02:29:41
问题 I'm writing a helper method that will generate comment's HTML on a page and I want to be able to show a comment with "< script >alert("hello");< /script >" as it's content. When using @HttpUtility.HtmlDecode(comment.Content) in a *.cshtml file, that script gets rendered as plain text. But when using this HTML helper in a View: @Html.PendingComment(comment) the script gets rendered as HTML and gets executed: public static IHtmlString PendingComment(this HtmlHelper helper, VoidCommentPending

hiddenFor helper is pulling id from action parameter, not the viewModel

痴心易碎 提交于 2019-12-07 15:41:09
问题 I've stumbled upon a really weird situation with ASP.Net MVC, passing an "id" as an action parameter and an "id" hidden form element. I have an action that has an "id" parameter. This id value represents a project. I have a controller action where I'm creating a data entry form for assigning an employee to the passed in project (I create a drop down list where administrator selects an employee to be assigned to passed project). By assigning an employee to a project, I create a ProjectEmployee

Building Html in Html Helper using Razor or Tag builder?

对着背影说爱祢 提交于 2019-12-07 05:02:22
问题 I am building a Html Helper in MVC 4 and I want to know how to build tags / html in the html helpers properly. For example here is simple html helper that creates image tag using TagBuilder class: public static MvcHtmlString Image(this HtmlHelper html, string imagePath, string title = null, string alt = null) { var img = new TagBuilder("img"); img.MergeAttribute("src", imagePath); if (title != null) img.MergeAttribute("title", title); if (alt != null) img.MergeAttribute("alt", alt); return

Register custom control in MVC3 ASPX Partial view

孤者浪人 提交于 2019-12-06 16:11:20
I ask a relevant question here and as I find there is no way to use custom control in Razor views, so I get to add new ASPX partial view to use custom control, my custom control is a dll that I added to References then define Partial view as the following: <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.DateTime?>" %> <%@ Register Assembly="JQControls" Namespace="JQControls" TagPrefix="PersianDatepicker" %> <PersianDatepicker:JQLoader ID="jqdb" runat="server" /> <PersianDatepicker:JQDatePicker ID="jqdp1" runat="server" Regional="fa" /> I write the exact code in ASPX