html-helper

Extending MVC3 HTML Helpers to include custom HTML5 Attribute

本小妞迷上赌 提交于 2019-12-11 04:01:44
问题 I know I can add custom attributes to any given helper using an anonymous type with the attribute and value specified for it to be rendered as a HTML5 attribute however im looking to achieve the same across all HTML Helpers in a given view triggered by an externally specified helper. Similar to the same functionality you receive from the un-obtrusive JavaScript helper where it renders validation rules in the context of a form field's attributes. Does anyone know if there is a "simple" way to

File upload control to upload PDF only

孤街浪徒 提交于 2019-12-11 03:23:56
问题 I got a file control like <div class="form-group"> @Html.LabelFor(m => m.File, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @Html.TextBoxFor(m => m.File, new { type = "file" }) </div> I want it to allow only PDF format files, so in my model, its like [Display(Name = "Terms of Business")] [Required, FileExtensions(Extensions=".pdf", ErrorMessage="Incorrect file format")] public HttpPostedFileBase File { get; set; } However, the control still allows to upload documents of

Unable to combine tags using TagBuilder

陌路散爱 提交于 2019-12-11 02:29:47
问题 I'm trying to build a combination tag: First tag: <span class="requiredInidicator">* </span> Second tag: <label>SomeText</label> (Attributes snipped for brevity) I would like to combine these to return an MVCHtmlString but the following code ignores the span completely. Could someone point out what I am doing wrong Here is my code: // Create the Label var tagBuilder = new TagBuilder("label"); tagBuilder.Attributes.Add("id", "required" + id); //I get the id earlier fyi - not pertinent fyi for

Passing HTML Attributes and [DisplayFormat] using Html.EditorFor and Html.TextBox

杀马特。学长 韩版系。学妹 提交于 2019-12-11 02:28:38
问题 I am trying to use Html.EditorFor for a value where I need both a DisplayFormat attribute and an HTML attribute (specfically a CSS class) to be applied. Html.TextBox ignores the DisplayFormat attribute, and Html.EditorFor will not let me pass Html attributes. Other than writing the HTML myself, what is the preferred solution here? 回答1: Here's one possibility: public class MyModel { [UIHint("MyDateTemplate")] public DateTime Value { get; set; } } and in ~/Views/Home/EditorTemplates

Do ASP.NET MVC helper methods like Html.DropDownList() encode the output HTML?

岁酱吖の 提交于 2019-12-11 02:01:00
问题 I am just wondering if I have to worry about encoding the values that get output when I use HTML helpers like Html.DropDownList() . If so, how do I encode them? It's easy to do if I were building the drop down manually -- just wrap each value with " Html.Encode() ". However, I don't know how to do this when using HTML helpers. 回答1: It looks like the values are encoded automatically, so there's no reason to do it yourself. Here's a snippet from the actual ASP.NET MVC 1.0 source code that you

How can I create a custom MVC3 template or HTML helper for a property of type KeyValuePair<string,string>

谁都会走 提交于 2019-12-10 22:57:13
问题 I've got a model with 3 properties that are of type KeyValuePair public class TestModel { public KeyValuePair<string,string> MyProperty{ get; set; } } My Controller code is as follows, public ActionResult Index() { var model = new TestModel {MyProperty= new KeyValuePair<string, string>("Color", "Blue")}; return View(model); } If I use the following Razor code, my results are not what I want. @model TestModel @Html.LabelFor(m => m.MyProperty) @Html.DisplayFor(m => m.MyProperty) My view ends up

Cakephp customizing the output of date input form helpers

淺唱寂寞╮ 提交于 2019-12-10 21:17:30
问题 I got echo $this->Form->input('birthdate', array( 'label' => __('Geburtsdatum', true) , 'dateFormat' => 'DMY' , 'minYear' => date('Y') - 70 , 'maxYear' => date('Y') - 10 )); and in the model I've set birthdate to date. Now Cakephp spits out three select boxes, which I absolutely adore. BUT it also spits out two ugly dashes / hyphens in between, which I want to get rid of. SELECTBOX - SELECTBOX - SELECTBOX any suggestions? 回答1: There is a separator option you can specify: 'separator' => 'YOUR

How can I override the name attribute of a RadioButtonFor?

纵然是瞬间 提交于 2019-12-10 20:37:12
问题 I'm trying to group together radiobuttons that are creating using a for loop and Razor syntax. Here is the code: @for (var i = 0; i < Model.Sessions.Count(); i++) { @Html.HiddenFor(it => it.Sessions[i].Id) @Html.RadioButtonFor(it => it.Sessions[i].Checkbox, "0", new {@class = "Sessions", @id = id, @name="Sessions"}) @Html.LabelFor(it => it.Sessions[i].Name, Model.Sessions[i].Name) <span class="time-span"><em>@Model.Sessions[i].StartTime</em><em>@Model.Sessions[i].EndTime</em></span> <br /> }

How to do an inline style with asp.net mvc 3 razor in a html helper

核能气质少年 提交于 2019-12-10 19:09:18
问题 I want to do this @Html.TextBoxFor(x => x.BackgroundColor, new { style = "width: 20px; background-color: @Model.BackgroundColor;" }) Hoever it does not render what is in my Mode.Background color(in firebug I just see @bModel.BackgroundColor"). Is this possible? 回答1: You are already inside a code block; Razor does not parse within code blocks for other code blocks. The style part of the line should look something like this: style = "width: 20px; background-color: " + Model.BackgroundColor + ";

RenderPartial and ViewBag

谁都会走 提交于 2019-12-10 19:01:36
问题 I'm trying to modify this code: http://www.codeproject.com/Articles/260470/PDF-reporting-using-ASP-NET-MVC3 To make it to get the ViewBag data for render the View in the PDF. Basically, I need to pass to Html.RenderPartial with a new Context my ViewBag, the code now is: Html.RenderPartial(viewName, model); And I'm trying to change it to something like: Html.RenderPartial(viewName, model, new ViewDataDictionary(ViewBag)); I don't get any error but the Dictionary keeps empty. BR 回答1: Try