html-helper

Spark View Engine with custom HTML Helpers

匆匆过客 提交于 2019-12-24 11:27:26
问题 I've added some of my own helpers to the System.Web.Mvc within my project and got it working with the default asp.net mvc view engine. By defining the helper like namespace System.Web.Mvc { public static class XSSHelper { public static string h(this HtmlHelper helper, string input) { return AntiXss.HtmlEncode(input); } public static string Sanitize(this HtmlHelper helper, string input) { return AntiXss.GetSafeHtml(input); } public static string hscript(this HtmlHelper helper, string input) {

Razor HtmlHelpers with sub-sections?

こ雲淡風輕ζ 提交于 2019-12-24 11:21:23
问题 Is there a way to build the custom Html Helpers and put them into sub-sections? I.e: @Html.Buttons.Gray @Html.Buttons.Blue @Html.Tables.2Columns @Html.Tables.3Columns Thanks. 回答1: Helpers are simply extension methods. So you could create helpers that return object that allow you to chain method calls, e.g. @Html.Button("Text").Grey() . public ButtonHelper { public string Text {get; set;} public MvcHtmlString Grey() { return MvcHtmlString.Create("<button class='grey'>"+ Text +"</button>"); } }

Populating ListBoxfor with ViewModel and passing back its contents

亡梦爱人 提交于 2019-12-24 10:46:48
问题 I am struggling to figure out how to pass back my model (unmodified) back to my controller. I have tried binding my ViewModel to many different HTML.Helpers and what not. I can get my data to bind and display, but I cannot get the ViewModel to post back to my controller. I have the following ViewModels: public class MediaProviderViewModel : ViewModelBase { public int MediaProviderId { get; set; } public string MediaProviderName { get; set; } public static MediaProviderViewModel FromEntity

How can I apply a top margin to my Html.BeginForm

為{幸葍}努か 提交于 2019-12-24 03:25:20
问题 I have this code: <div class="mdl_bdy_frm"> @using (Html.BeginForm()) { @Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.") @Html.LabelFor(m => m.Login.UserName, new { @class = "adm" }) @Html.TextBoxFor(m => m.Login.UserName, new { @class = "adm", size = 30 }) @Html.ValidationMessageFor(m => m.Login.UserName) @Html.LabelFor(m => m.Login.Password, new { @class = "adm" }) @Html.PasswordFor(m => m.Login.Password, new { @class = "adm", size = 30 })

MVC TextBox with name specified not binding model on post

限于喜欢 提交于 2019-12-24 02:23:50
问题 This is like MVC 101 here so I feel completely helpless on why this isn't working. I have an extremely basic Model: public class StockEnrollmentModel { [Required] [DisplayName("Employee Name:")] public string EmployeeName { get; set; } } My view looks like this: @using (Html.BeginForm("SimulateForm", "HR", FormMethod.Post)) { <div class="row"> <div class="col-md-6"> <div class="form-group"> @Html.LabelFor(e => e.EmployeeName) @Html.TextBox("stock_employee_name", Model.EmployeeName) </div> <

Url.Action(action,controller,routeValues) doubling up ID in URL

我与影子孤独终老i 提交于 2019-12-24 02:09:23
问题 I am using the following code (taken from a Stackoverflow post: Action Image MVC3 Razor ) in an HTML extension helper to build an action link. My Url.Action() method is returning a url that has the routeValues both in the route as well as appended onto the url like so: /Proposals/List/Tabled?id=Tabled when what I want is just /Proposals/List?id=Tabled Any suggestions about why it wants to do this? Update : A route rule from my Global.asax file. That must be why it's doing it, but why it's

Asp.Net MVC @helper template in different folder than App_Code [duplicate]

不羁岁月 提交于 2019-12-24 00:59:52
问题 This question already has answers here : Can I have a global razor @helper outside of App_Code? (4 answers) Closed 3 years ago . Like I said in the title of my question, is it possible to put the @helper template of cshtml file in a folder other than App_Code to be visible by my views? I discovered the very nice article from ScottGu but I don't found any solution to my question. Thank you. 回答1: You can, if you write them as extension methods (static methods of a static class). Something like:

MVC Multiple forms in PartialView returns null on postback for except the 1st one listed

◇◆丶佛笑我妖孽 提交于 2019-12-24 00:58:54
问题 Ok, I hope someone out there can help me out on this one (Thanks in advance!). I'm using MVC4. My ContractsModel contains (among other things) a list of ContractModel(s). In my partial view, I loop though the list creating a form for each. So far so good. The issue is on postback. The 1st form always posts back fine. But the others (2, 3, 4, etc) always return null. I've checked with debugging tools and confirmed that the rendered controls are appropriately named. <select name="Contracts[0]

Pass textbox value from view to buttons

江枫思渺然 提交于 2019-12-23 15:44:05
问题 View: @using (@Html.BeginForm("Show", "test", FormMethod.Post)) { <fieldset> <table > <tr> <td> @Html.LabelFor(model=> model.Show) </td> <td> @Html.TextBox("txtvalue", null) </td> </tr> </table> <input type="button" value="Show" onclick = "@("location.href='"+ @Url.Action("Show", "test")+"'" )" /> <input type="button" value="Go" onclick ="@("location.href='"+ @Url.Action("Go", "test")+"'" )"/> </fieldset> } and two action methods in the controller, public ActionResult Show(string txtvalue) {

What is the equivalent of System.Web.Mvc.Html.InputExtensions in ASP.NET 5?

允我心安 提交于 2019-12-23 12:04:02
问题 What is the ASP.NET 5 equivalent of System.Web.Mvc.Html.InputExtensions as used in ASP.NET 4? See example below: public static class CustomHelpers { // Submit Button Helper public static MvcHtmlString SubmitButton(this HtmlHelper helper, string buttonText) { string str = "<input type=\"submit\" value=\"" + buttonText + "\" />"; return new MvcHtmlString(str); } // Readonly Strongly-Typed TextBox Helper public static MvcHtmlString TextBoxFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper,