How to disable element prefix in asp.net mvc2 template?
Should I use Partial Control Instead?
To answer my own question, this line of code removes the hierarchical prefix from the element name..
<% ViewData.TemplateInfo.HtmlFieldPrefix = ""; %>
<%= Html.EditorFor(model => model.Description) %>
Related
I'm using EJS as a part of my front-end dev stack.
For example my normal index.ejs looks like that:
<%- include parts/header.ejs %>
<%- include parts/navigation.ejs %>
<!-- HTML content: divs, spans, etc. -->
<%- include parts/footer.ejs %>
What I want is to pass somehow a variable with the include <%- include parts/footer.ejs?variable=value %> and want to read it in the included file, to conditionally show/hide some parts of the content.
I can't find the way to do it. Is it possible with EJS?
Two ways to do this:
Dumb Way
This way is compatible with EJS 1.0, and has the advantage of being compile-time.
Just declare the variables right before includeing.
Example:
included.ejs:
<p><%= variable %></p>
main.ejs:
<% var variable = 'hola' %>
<% include included %>
Smart Way
This method is only available with EJS 2.0 or newer, but might be marginally slower (or a lot slower if caching is not enabled) than the last method:
included.ejs:
<p><%= variable %></p>
main.ejs:
<%- include('included', {variable: 'hola'}) %>
I'm learning about HTMLHelpers in ASP.NET MVC.
To render the form HTML tag you would write something like
<% using(Html.BeginForm("HandleForm", "Home")) {%>
<!--Form content goes here-->
<% } %>
or
<% Html.BeginForm(); %>
… Form Contents …
<% Html.EndForm(); %>
To render the a checkbox you would use
<%= Html.CheckBox("bookType") %>
What I would like to know is is why we need to use <% when we use BeginForm whereas we need to use <%= when we use other HTMLHelper methods
Cheers,
Html.CheckBox returns a string of HTML containing an <input> tag.
You need to print this string to the page by writing <%= ... %>.
Html.BeginForm prints the HTML inside the method (by calling Response.Write), and doesn't return HTML. (instead, it returns an IDisposable, so that you can use it in a using block)
Since you aren't printing its return value, you put it in a <% ... %> block, which executes code without printing its results.
<% %> wraps a code block
<%="string" %> is equivalent to <% Response.Write("string") %>
and in ASP.NET MVC 3 you can automatically HtmlEncode with <%: "<htmlTag>" %>
You can definitely write <%=Html.BeginForm() %> but you will also need to write <%=Html.EndForm() %>. Wrapping Html.BeginForm() within a using block will just render the closing </form> tag for you.
Because <%= means "Print this for me", pretty much the same as doing:
<% Response.Write("content"); %>
When <% means that you have a code-block that might do more than just print the value you have nested in it.
I created a Display Template which when passed a string renders a disabled text box
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<String>" %>
<%: Html.TextBoxFor(model => model, new { disabled = "disabled" })%>
Which works great. However, for some reason MVC wants to try and stuff DateTimes and Ints through it as well, which is throwing exceptions
The model item passed into the dictionary is of type 'System.Int32', but this dictionary requires a model item of type 'System.String'.
Any ideas?
You don't need to strongly type the template to a String.
you can try something like this :
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%= Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue,
, new { disabled = "disabled" }) %>
And in your view you call it like this
Html.DisplayModelFor(model => mode.name);
For more information see an example of the default built-in editor template for the string in Brad Wilson article in his his series on Templates in ASP.NET MVC.
You should consider going through the complete series. I can't express how helpful this series was for me.
I'm currently trying to create a view in asp.net MVC2 which includes a checkbox in each row. This will be a "mark" box, and there will be a button on the view which can then be used for multiple deletion.
I have a model which contains a list of the objects being listed, so I have some code which reads as:-
<% foreach (CancelledCard item in Model.CancelledCards) { %>
I've then tried to use
<%: Html.CheckBoxFor(item >= item.Checked) %>
but I'm getting an error.
What am I missing? What's the right way to do this in MVC2?
In C# a lambda expression uses => and not >=:
<%: Html.CheckBoxFor(item => item.Checked) %>
Also instead of saying that you get an error, you could have posted the error message. It would have been much more clear.
<%: Html.CheckBoxFor(item >= item.Checked) %>
is wrong. It should be:
<%: Html.CheckBoxFor(item => item.Checked) %>
I have the following asp.net mvc2 template:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ncontinuity2.core.dto.TrainingLookUpContainer>" %>
<%= Html.EditorFor(x => ViewData.Model.TrainingTree, "TrainingCategory")%>
You can see that I want to bind it to a model of type TrainingLookUpContainer.
The problem is, the following mark up is being generated:
<input id="ViewData_Model_TrainingTree_TrainingCourses_0__Uid" name="ViewData.Model.TrainingTree.TrainingCourses[0].Uid" type="hidden" value="cbd43b5a-2a6a-493f-98e4-9dc9010cbaaf" />
The bit I object to is the ViewData_Model_ prefix for the id of the element and the ViewData.Model. prefix for the name attribute.
I have no idea where this prefix is coming from and it of course means that the model never gets bound when it comes to posting the form.
Is there another way, I can control the mark up that is getting generated or is this a bug in the framework. I have used EditorFor in other parts and it works fine.
change
<%= Html.EditorFor(x => ViewData.Model.TrainingTree, "TrainingCategory")%>
to
<%= Html.EditorFor(x => x.TrainingTree, "TrainingCategory")%>