Tag: html helper

MVC RadiobuttonFor Razor如何使标签点击?

到目前为止,我正试图用剃刀语法创建一个radiobuttonlist,我已经想出了这个 @foreach (var p in Model) { @Html.RadioButton(“name”, “1”, false, new { onCLick = “ShowOption(this)”, id = p.id.ToString() }) @Html.Label(p.id.ToString(), p.name) } 但标签与radiobutton无关。

将文本框值从视图传递到按钮

视图: @using (@Html.BeginForm(“Show”, “test”, FormMethod.Post)) { @Html.LabelFor(model=> model.Show) @Html.TextBox(“txtvalue”, null) } 和控制器中的两个动作方法, public ActionResult Show(string txtvalue) { … } public ActionResult Go(string txtvalue) { … } 根据哪个按钮单击,它应该转到相应的操作方法并传递文本框的值。 任何人都可以建议我这样做的方式。 我绕着脑袋想不通。

如何在html帮助器中使用asp.net mvc 3 razor进行内联样式

我想做这个 @Html.TextBoxFor(x => x.BackgroundColor, new { style = “width: 20px; background-color: @Model.BackgroundColor;” }) 但是它不会渲染我的Mode.Background颜色(在firebug中我只看到@ bModel.BackgroundColor“)。这可能吗?

在Razor Helper里面使用@section

我们正在尝试将布局的各个部分设置为必需,但可以根据各个页面进行配置。 目前我们用一节来做这件事。 @section FloatingNav { @Model.Name @Model.AverageRating Episodes Cast Reviews Related } 这需要您在每个新页面中设置此块,但我希望使用部分默认值和选项来使用部分视图进行配置,从而简化此过程。 我希望设置一个像这样的Razor助手。 @using System.Web.Mvc.Html @helper FloatingNav(string name, int rating) { @section FloatingNav { name rating Episodes Cast Reviews Related } } @helper FloatingNav(System.Web.Mvc.HtmlHelper html, string viewName) { @section FloatingNav { @html.Partial(viewName) } } @helper FloatingNav(System.Web.Mvc.HtmlHelper html, string viewName, object model) { @section FloatingNav […]

ASP.NET 5中System.Web.Mvc.Html.InputExtensions的等价物是什么?

ASP.NET 4中使用的ASP.NET 5等效的System.Web.Mvc.Html.InputExtensions是什么? 见下面的例子: public static class CustomHelpers { // Submit Button Helper public static MvcHtmlString SubmitButton(this HtmlHelper helper, string buttonText) { string str = “”; return new MvcHtmlString(str); } // Readonly Strongly-Typed TextBox Helper public static MvcHtmlString TextBoxFor(this HtmlHelper htmlHelper, Expression<Func> expression, bool isReadonly) { MvcHtmlString html = default(MvcHtmlString); if (isReadonly) { html = […]

使用Razor或Tag构建器在Html Helper中构建Html?

我正在MVC 4中构建一个Html Helper,我想知道如何正确地在html助手中构建tags / html。 例如,这里是使用TagBuilder类创建图像标记的简单html帮助器: 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 MvcHtmlString.Create(img.ToString(TagRenderMode.SelfClosing)); } 从另一方面,我可以做这样的事情: // C#: public static MvcHtmlString Image(this HtmlHelper html, string […]

ASPNET MVC – 使用具有相同签名的新助手覆盖Html.TextBoxFor(model.property)?

我想用我自己的帮助器覆盖Html.TextBoxFor(),该帮助器具有完全相同的签名(当然是一个不同的命名空间) – 这是可能的,如果是这样,怎么样? 这样做的原因是我在现有应用程序中有100多个视图,我想改变TextBoxFor的行为,以便在属性具有[StringLength(n)]注释时输出maxLength = n属性。 自动输出maxlength = n的代码在这个问题中: 来自Asp.Net MVC中DataAnnotations StringLength的文本框的maxlength属性 。 但我的问题并不重复 – 我正在尝试创建一个更通用的解决方案:DataAnnotaion自动流入html,而不需要编写视图的人员需要额外的代码。 在引用的问题中,您必须将每个Html.TexBoxFor更改为Html.CustomTextBoxFor。 我需要这样做,以便不需要更改现有的TextBoxFor() – 因此创建一个具有相同签名的帮助程序:更改辅助方法的行为,并且所有现有实例都可以正常工作而不做任何更改(100 + views,至少500 TextBoxFor()s – 不想手动编辑它)。 我尝试了这段代码:(我需要为TextBoxFor的每次重载重复它,但一旦根问题解决了,那将是微不足道的) namespace My.Helpers { public static class CustomTextBoxHelper { public static MvcHtmlString TextBoxFor(this HtmlHelper htmlHelper, Expression<Func> expression, object htmlAttributes, bool includeLengthIfAnnotated) { // implementation here } } } 但是我在Html.TextBoxFor()视图中遇到编译器错误:“调用在以下方法或属性之间是不明确的”(当然)。 有没有办法做到这一点? […]

可以请求从htmlhelper访问查询字符串

您好可以在HTMLHelper扩展方法中访问查询字符串。 我们需要根据请求中的查询字符串进行不同的呈现。

查看数据字典覆盖ASP.NET MVC中的模型数据

我有一个创建用户的视图,如下所示。 UserName: Password: 单击“创建”按钮时,HTML表单将发布到名为“SaveUser”的操作,该操作仅接受“POST”动词,如下所示。 [AcceptVerbs(HttpVerbs.Post)] public ActionResult SaveUser( UserViewModel user) { //user.Id is zero before save //Save the user. Code omitted… //user.Id is now greater than zero //redirect to edit user view return View(“EditUser”, user ); } 保存用户后,页面将重定向到“EditUser”视图 Id: 这是问题所在:隐藏字段的值保持显示为零。 Model.Id大于零。 似乎其他东西覆盖了模型视图值。 ViewDataDictonary是个嫌疑人。 因此,在返回操作中的视图之前添加一行,如下所示。 [AcceptVerbs(HttpVerbs.Post)] public ActionResult SaveUser( UserViewModel user) { //user.Id is zero before […]

如何在HtmlHelper中调用`EditorExtensions.EditorFor`?

我在CreateView中使用不同的模型,所有模型都inheritance自BaseModel。 调用正确的EditorFor我创建了一个获取Model和实际属性的HtmlHelper。 但我不知道如何调用它。 BaseModel: public abstract class BaseModel { protected IEnumerable PropertyInfoCache { get; set; } protected IEnumerable EnumeratedPropertyCache { get; set; } protected BaseModel() { PropertyInfoCache = this.GetType().GetProperties(); EnumeratedPropertyCache = PropertyInfoCache.Select(p=> new EnumeratedProperty(p.Name,p.GetType())); } public IEnumerable EnumerateProperties() { return EnumeratedPropertyCache; } public object GetPropertyValue(string PropertyName) { var property = PropertyInfoCache.SingleOrDefault(i=>i.Name==PropertyName); if(property!=null) return property.GetValue(this,null); return […]