html-helper

Html.ActionLink cannot be dynamically dispatched

不打扰是莪最后的温柔 提交于 2019-11-27 14:33:46
I have a problem with MVC3 I'm trying to use @Html.ActionLink() to generate a Link for titles in my blog project. Using constant strings in ActionLink works just dandy, but if I use Posts.Title (the Title of the current Post model being looped), I get this exception: CS1973: 'System.Web.Mvc.HtmlHelper<dynamic>' has no applicable method named 'ActionLink' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax. "Consider casting the dynamic

Html inside label using Html helper

亡梦爱人 提交于 2019-11-27 14:28:17
How can I add inline html elements inside a label with Html.Label? Looks like a good scenario for a custom helper: public static class LabelExtensions { public static MvcHtmlString LabelFor<TModel, TProperty>( this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> ex, Func<object, HelperResult> template ) { var htmlFieldName = ExpressionHelper.GetExpressionText(ex); var for = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName); var label = new TagBuilder("label"); label.Attributes["for"] = TagBuilder.CreateSanitizedId(for); label.InnerHtml =

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

天大地大妈咪最大 提交于 2019-11-27 14:21:37
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 Label by an ID rather than wrapping the Label within a block (i.e. div) and then styling the block.

Creating MVC3 Razor Helper like Helper.BeginForm()

☆樱花仙子☆ 提交于 2019-11-27 13:57:39
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" is the css id for that div and "the title" is the title of the section. Is there a better way to

Current date and time - Default in MVC razor

淺唱寂寞╮ 提交于 2019-11-27 13:51:41
问题 When the MVC view page with this textbox, loads , I would like to display current date and time by default. How can I do this? in razor. @Html.EditorFor(model => model.ReturnDate) 回答1: Before you return your model from the controller, set your ReturnDate property to DateTime.Now() myModel.ReturnDate = DateTime.Now() return View(myModel) Your view is not the right place to set values on properties so the controller is the better place for this. You could even have it so that the getter on

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

时间秒杀一切 提交于 2019-11-27 13:16:51
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. In order to generate a dropdownlist you need 2 properties on your view model: a scalar property to bind the selected

ASP.NET MVC - Html.DropDownList - Value not set via ViewData.Model

点点圈 提交于 2019-11-27 13:13:30
问题 Have just started playing with ASP.NET MVC and have stumbled over the following situation. It feels a lot like a bug but if its not, an explanation would be appreciated :) The View contains pretty basic stuff <%=Html.DropDownList("MyList", ViewData["MyListItems"] as SelectList)%> <%=Html.TextBox("MyTextBox")%> When not using a model, the value and selected item are set as expected: //works fine public ActionResult MyAction(){ ViewData["MyListItems"] = new SelectList(items, "Value", "Text"); /

warnings - html.helpers not recognized after project update

廉价感情. 提交于 2019-11-27 12:56:07
问题 i recently updated my project to mvc 5, EF 6. i updated all the packages via package console manager (update-package). Now i see tons of warnings about the html helpers in every view/partial view/layout. These are some of the errors: The name 'ViewBag' does not exist in the current context The name 'Styles' does not exist in the current context The name 'Scripts' does not exist in the current context 'System.Web.WebPages.Html.HtmlHelper' does not contain a definition for 'Partial' and no

DateTime field and Html.TextBoxFor() helper. How to use it correctly?

女生的网名这么多〃 提交于 2019-11-27 12:17:18
问题 I have a DateTime field in my Model. If I try to use this field in a strong typed partial view this way <%= Html.TextBoxFor(model => model.DataUdienza.ToString("dd/MM/yyyy"), new { style = "width: 120px" }) %> I will get the following compilation error at runtime System.InvalidOperationException : Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions. Anyway if I use it removing the formatting, ToString("dd

How to concatenate several MvcHtmlString instances

会有一股神秘感。 提交于 2019-11-27 11:37:52
问题 I have some doubts about how to concatenate MvcHtmlString instances because of this information found in MSDN : MvcHtmlString Class Represents an HTML-encoded string that should not be encoded again Do I risk that strings are HTML-encoded twice when using code like this: var label = Html.LabelFor(model => model.Email); var textbox = Html.TextBoxFor(model => model.Email); var validation = Html.ValidationMessageFor(model => model.Email); var result = MvcHtmlString.Create( label.ToString() +