html-helper

MVC3: How to get currently executing view or partial view programatically inside a HtmlHelper extension?

流过昼夜 提交于 2019-11-28 00:26:59
问题 How to get currently executing view name or partial view name programmatically inside a HtmlHelper extension? In my situation, I can't use ViewData or can't pass view name to the extension from the View. 回答1: var webPage = htmlhelper.ViewDataContainer as WebPageBase; var virtualPath = webPage.VirtualPath; 回答2: This is your best bet: Retrieve the current view name in ASP.NET MVC? 回答3: There is one dirty way to find real path even for partial view but it is really.. dirty. helper

Pass SelectedValue of DropDownList in Html.BeginForm() in ASP.NEt MVC 3

≯℡__Kan透↙ 提交于 2019-11-27 22:43:15
问题 This is my View Code: @using(Html.BeginForm(new { SelectedId = /*SelectedValue of DropDown*/ })) { <fieldset> <dl> <dt> @Html.Label(Model.Category) </dt> <dd> @Html.DropDownListFor(model => Model.Category, CategoryList) </dd> </dl> </fieldset> <input type="submit" value="Search" /> } As code shown I need to pass the dropdown selected value to the action in BeginForm() Html helper. What is your suggestion? 回答1: The selected value will be passed when the form is submitted because the dropdown

Handling onchange event in HTML.DropDownList Razor MVC

我的未来我决定 提交于 2019-11-27 21:03:49
I'm handling an onchange event with a selected value by simple HTML like this: <select onchange="location = this.value;"> <option value="/product/categoryByPage?PageSize=15" selected="selected">15</option> <option value="/product/categoryByPage?PageSize=30" selected="selected">30</option> <option value="/product/categoryByPage?PageSize=50" selected="selected">50</option> </select> Doing it like this: List<SelectListItem> items = new List<SelectListItem>(); string[] itemArray = {"15","30","50"}; for (int i = 0; i < itemArray.Count(); i++) { items.Add(new SelectListItem { Text = itemArray[i],

Creating a SelectListItem with the disabled=“disabled” attribute

醉酒当歌 提交于 2019-11-27 19:43:25
I'm not seeing a way to create, via the HtmlHelper , a SelectListItem that will spit out the following HTML: <option disabled="disabled">don't click this</option> The only properties SelectListItem has are: new SelectListItem{ Name = "don't click this", Value = string.Empty, Selected = false } The only option I see is to Subclass the SelectListItem to add an Enabled property to get the value to the view Not use the HTML helper for DropDownList Create a new HtmlHelper extension that accepts my new EnablableSelectList and adds my disabled attribute. Jab This is something I might try before

Is it possible to create a custom ASP.NET MVC strongly typed HTML Helper?

别来无恙 提交于 2019-11-27 17:33:08
问题 I was wondering if it is possible to create a custom strongly typed HTML Helper in ASP.NET MVC 2? Creating a regular (read not-strongly-typed) helper is straightforward but i am having difficulty creating strongly typed versions. For example, I would like to create a DatePickerFor html helper... Any guidance or snippets would be greatly appreciated, Thank you in advance! JP 回答1: Ok, I figured it out (and it was pretty straightforward...). Posting one of my overloads in case anyone else runs

Custom html helpers: Create helper with “using” statement support

亡梦爱人 提交于 2019-11-27 17:31:12
I'm writing my first asp.net mvc application and I have a question about custom Html helpers: For making a form, you can use: <% using (Html.BeginForm()) {%> *stuff here* <% } %> I would like to do something similar with a custom HTML helper. In other words, I want to change: Html.BeginTr(); Html.Td(day.Description); Html.EndTr(); into: using Html.BeginTr(){ Html.Td(day.Description); } Is this possible? ybo Here is a possible reusable implementation in c# : class DisposableHelper : IDisposable { private Action end; // When the object is created, write "begin" function public DisposableHelper

How to set a default value with Html.TextBoxFor?

会有一股神秘感。 提交于 2019-11-27 16:58:34
Simple question, if you use the Html Helper from ASP.NET MVC Framework 1 it is easy to set a default value on a textbox because there is an overload Html.TextBox(string name, object value) . When I tried using the Html.TextBoxFor method, my first guess was to try the following which did not work: <%: Html.TextBoxFor(x => x.Age, new { value = "0"}) %> Should I just stick with Html.TextBox(string, object) for now? you can try this <%= Html.TextBoxFor(x => x.Age, new { @Value = "0"}) %> This should work for MVC3 & MVC4 @Html.TextBoxFor(m => m.Age, new { @Value = "12" }) If you want it to be a

Surrounding a ValidationSummary with a box via CSS

守給你的承諾、 提交于 2019-11-27 16:33:03
问题 By default Html.ValidationSummary() produces HTML like this: <span class="validation-summary-errors">There were some errors...</span> <ul class="validation-summary-errors"> <li>First Name too long</li> <li>Invalid Email Address</li> </ul> I'd like to select the entire validation summary and add a bounding box around it via CSS, so I am adding a CSS class like this: .validation-summary-errors{ background-color:#D9FFB2; border: 1px solid #5CBA30; color:#000000; margin-top:15px; margin-bottom

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

我怕爱的太早我们不能终老 提交于 2019-11-27 15:03:56
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 ? 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 ViewData. Example: var list = new SelectList(new [] { new {ID="1",Name="name1"}, new{ID="2",Name="name2"}, new

Steve Sanderson's BeginCollectionItem helper won't bind correctly

六月ゝ 毕业季﹏ 提交于 2019-11-27 14:52:17
I am using Steve Sanderson's BeginCollectionItem helper and ran into a problem. I have a form that has an option to add unlimited reward fields. I am using his helper since it solved this problem with how to keep generating the fields and not have to worry about how to bind it when the form gets submitted. I have in this same form some checkboxes that there is an unknown amount. The difference with this one versus the rewards is the unknown amount will become known after a database call and will be known by the time the code gets to the view. So my code looks like this public class FrmVm {