html-helper

Difference between Html.RenderAction and Html.Action

喜你入骨 提交于 2019-11-26 22:43:08
Does anybody know what's the difference between Html.RenderAction and Html.Action ? Megawolt Html.Action() – Outputs string Html.RenderAction() – Renders directly to response stream If the action returns a large amount of HTML, then rendering directly to the response stream provides better performance than outputting a string. The difference between the two is that Html.RenderAction will render the result directly to the Response (which is more efficient if the action returns a large amount of HTML) whereas Html.Action returns a string with the result. check out this link for a detailed

DropDownListFor does not select value if in for loop

我的梦境 提交于 2019-11-26 21:07:56
问题 In my view <%= Html.DropDownListFor( x => x.Countries[ i ], Model.CountryList )%> in my controller public int[ ] Countries { get; set; } public List<SelectListItem> CountryList { get; set; } When the forms gets posted there is no problem, the dropdown is populated and the values the user selects are posted. But when I try to load the form with already assigned values to the Countries[ ] it does not get selected. 回答1: I'm getting the same too. When using foreach to loop around a

Handling onchange event in HTML.DropDownList Razor MVC

此生再无相见时 提交于 2019-11-26 20:31:38
问题 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"

What does Html.HiddenFor do?

夙愿已清 提交于 2019-11-26 20:10:31
Although I have read the documentation on Html.HiddenFor, I've not grasped what is it used for... Could somebody explain its uses and give a short example? Where should those helpers go in the code? Justin Niessner It creates a hidden input on the form for the field (from your model) that you pass it. It is useful for fields in your Model/ViewModel that you need to persist on the page and have passed back when another call is made but shouldn't be seen by the user. Consider the following ViewModel class: public class ViewModel { public string Value { get; set; } public int Id { get; set; } }

I want to understand the lambda expression in @Html.DisplayFor(modelItem => item.FirstName)

大憨熊 提交于 2019-11-26 19:55:09
I’m fairly new at C# and MVC and have used lambdas on certain occasions, such as for anonymous methods and on LINQ. Usually I see lambda expressions that look something like this: (x => x.Name), (x => { Console.WriteLine(x)) I understand that lambda = "goes to". I have never seen a lambda expression where the left parameter is not used. I don’t know how to translate this lambda expression though @Html.DisplayFor(modelItem => item.FirstName) Can anyone shed some light on this one for me? Shouldn’t this be (modelItem => modelItem.FirstName)? I got this from Microsoft's Introduction to ASP.NET

Set disable attribute based on a condition for Html.TextBoxFor

耗尽温柔 提交于 2019-11-26 19:54:54
I want to set disable attribute based on a condition for Html.TextBoxFor in asp.net MVC like below @Html.TextBoxFor(model => model.ExpireDate, new { style = "width: 70px;", maxlength = "10", id = "expire-date" disabled = (Model.ExpireDate == null ? "disable" : "") }) This helper has two output disabled="disabled " or disabled="". both of theme make the textbox disable. I want to disable the textbox if Model.ExpireDate == null else I want to enable it The valid way is: disabled="disabled" Browsers also might accept disabled="" but I would recommend you the first approach. Now this being said I

MVC 3 - Html.EditorFor seems to cache old values after $.ajax call

耗尽温柔 提交于 2019-11-26 19:48:58
This is a follow on from the following question: MVC 3 + $.ajax - response seems to be caching output from partial view There is a detailed description of the problem over there. However, I have now managed to narrow down the problem, that seems to be with the Html.EditorFor helpers, hence the new question. The issue: I post data to the server using $.ajax, then return the html of the partial view that holds the input controls. The problem is that, despite passing a newly created object to the Partial Views model, the various @Html.EditorFor and @Html.DropDownListFor helpers return the OLD

ASP.NET MVC DropDownListFor with model of type List<string>

╄→гoц情女王★ 提交于 2019-11-26 19:46:26
I have a view with a model of type List and I want to place a drop down list on the page that contains all strings from the list as items in the drop down. I am new to MVC, how would I accomplish this? I tried this: @model List<string> @Html.DropDownListFor(x => x) but that threw an error. Any help is appreciated. To make a dropdown list you need two properties: a property to which you will bind to (usually a scalar property of type integer or string) a list of items containing two properties (one for the values and one for the text) In your case you only have a list of string which cannot be

How can I create a Html Helper like Html.BeginForm

六眼飞鱼酱① 提交于 2019-11-26 17:44:05
问题 I have an Extension Method that verifies if the user is able to see a portion of the webpage, based on a Role. If I simple remove the content, this brings me more work as all the missing forms will not be correctly registered upon save and I have to deal with this behavior by modifying all my code, so I thought why not just use display:none; attribute? I would like to have something like: @using(Html.RoleAccess(currentUser, RoleAccessType.Content_General_Website)) { ... } and that this would

How do I apply a CSS class to Html.ActionLink in ASP.NET MVC?

 ̄綄美尐妖づ 提交于 2019-11-26 17:38:49
问题 I'm building an ASP.NET MVC application, using VB.NET and I'm trying to apply a css class to a Html.ActionLink using the code: <%=Html.ActionLink("Home", "Index", "Home", new {@class = "tab" })%> But when I run the code I receive the below error: Compiler Error Message: BC30988: Type or 'With' expected. I'm new to MVC and really haven't much of a clue what I'm doing so I can't see what's wrong there as I'm using code based of an example elsewhere. 回答1: It is: <%=Html.ActionLink("Home", "Index