html-helper

asp.net mvc4 razor pass view model to helper

白昼怎懂夜的黑 提交于 2019-12-13 04:19:59
问题 I want to pass view model to my html helper/ I have tried public static string GenerateFullTable(this HtmlHelper helper, IEnumerable<CarsViewModel> model) { But I dont know which model would be. Does it possible to make universal helper which gets would get different view models? 回答1: Yes, it's called Generics. http://msdn.microsoft.com/en-us/library/ms379564(v=vs.80).aspx Edit: Here's one example... public static string GenerateFullTable<T>(this HtmlHelper helper, IEnumerable<T> model) { ...

Not getting validation Error Messages from DataAnnotation Validations

寵の児 提交于 2019-12-13 04:12:51
问题 I am have a simple model class for Login, with few DataAnnotation Validations Public Class LoginUser <Required()> _ Public Property UserName As String <Required()> _ <StringLength(8)> _ Public Property Password As String End Class View is a partial View and is as follows: <% Using (Html.BeginForm("Login", "User", FormMethod.Post))%> <% Html.EnableClientValidation()%> <%= Html.ValidationSummary() %> <table ID="loginTable" runat="server"> <tr> <td> <%= Html.LabelFor(Function(x) x.UserName)%> <

Why does Razor Pages scaffolding use HTML Helpers (and not Tag Helpers) for Index, Details and Delete pages?

我怕爱的太早我们不能终老 提交于 2019-12-13 03:07:44
问题 I recently started my first project using Razor Pages, I am using EF Core and have scaffolded all of my models into CRUD Razor Pages. Something I noticed was that the Create and Edit Razor Pages that are generated use Tag Helpers to display the data. i.e. <div class="form-group"> <label asp-for="ViewModel.Name" class="control-label"></label> <input asp-for="ViewModel.Name" class="form-control" /> <span asp-validation-for="ViewModel.Name" class="text-danger"></span> </div> Whereas the Index,

HtmlHelper using ViewContext.Writer not rendering correctly

梦想的初衷 提交于 2019-12-13 02:47:43
问题 I am building a simple CMS with asp.net MVC and I have almost all the parts working. I have one minor issue which I can't work out how to solve. I have a Html helper method that renders the content of area. This method however uses the Response.Write to write its content rather than returning a string. The reason for this is that I'm performing a partial request and hence do not have a string to return. My template body looks like this: <body> <h1>Default Template</h1> <% Html.ContentArea(

Adding HtmlHelper NameSpace in Web.Config does not work

老子叫甜甜 提交于 2019-12-12 14:50:16
问题 Problem No 1: Hi guys I have started learning ASP.NET MVC , I have made a simple extension method ,like this namespace MvcTestz //Project is also named as "MvcTestz" { public static class SubmitButtonHelper //extension method. { public static string SubmitButton(this HtmlHelper helper,string buttonText) { return string.Format("<input type=\"submit\" value=\"{0}\">",buttonText); } } } Then I have added namespace of the Custom HtmlHelper in to the Web.Config ,like this <namespaces> <!--other

ASP.NET MVC 4 custom HTML Helpers folder location

我只是一个虾纸丫 提交于 2019-12-12 12:00:34
问题 I'm starting to develop an application using ASP.NET MVC 4 framework with Razor syntax. I want to know where (folder location) I should create my HTML Helper class. Best practice. For example: VisualStudioSolution Controlles Html HtmlHelperClass.vb Models Views 回答1: use this.To use the "@helper" feature in Razor you need to place the CSHTML file in the App_Code folder of your app. There is no "Views/Helpers" folder in ASP.NET MVC 3. ScottGu's blog post was written before the feature was fully

How to unit test an extension method that calls html.EditorFor

梦想与她 提交于 2019-12-12 09:49:54
问题 During a spike, I created a nifty html helper extension method for use in my views, it worked well. It will make my views so much easier to maintain. Now I need to figure out how to unit test it. Here is the gist of what I am trying to do: public static MvcHtmlString EditorOrDisplayForBasedOnConvolutedBusinessLogic<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression) { if (ConvolutedBusinessLogic()) { return html.EditorFor(expression); } else { return

How to Unit Test HtmlHelper similar to “using(Html.BeginForm()){ }”

泄露秘密 提交于 2019-12-12 09:43:39
问题 Can somebody please suggest how I could write a Unit Test with Moq for following HtmlHelper method? public static HtmlTagBase GenerateTag<T>(this HtmlHelper htmlHelper , object elementData , object attributes) where T : HtmlTagBase { return (T)Activator.CreateInstance(typeof(T) , htmlHelper.ViewContext , elementData , attributes); } which you would use as follows (please note the using statement - this is causing me confusion): <%--Model is a type of ShareClass--%> <% using (Html.GenerateTag

Displaying HTML String from Model in MVC Razor View

◇◆丶佛笑我妖孽 提交于 2019-12-12 09:37:44
问题 I have a Model filed that returns an HTML string with line break BR tag, but How do I display that HTML on the browser ? The problem ins instead putting the line break, the Tag itself displaying on the UI I tried to put the model within Html.Raw(modelItem => item.Speaking), but it never works as it expecting a string inside, and Cannot convert lambda expression to type 'string' because it is not a delegate type Below is the code and comments what I've tried. <div> @{ string strTest = "<br/

ASP.Net MVC ModelState / Html.TextBox postback issue

梦想与她 提交于 2019-12-12 08:13:07
问题 I have an issue cropping up in a form I'm trying to post. In the scenario where the form doesn't validate, I'm taking the standard route of calling ModelState.AddModelError() then returning a View result. The thing is, the HTML.* helpers are supposed to pick up the posted value when rendering and I'm noticing that my text fields ONLY do so if I include them in the parameter list of the postback action, which shouldn't be required seeing as some forms have way too many fields to want to list