ASP.NET MVC: set custom name to rendered html element - asp.net-mvc

I generate html textbox in this way:
<%: Html.TextBoxFor(m => m.Category) %>
ASP.NET MVC render html:
<input type="text" value="" name="Category" id="Category">
Is there is a way to set manually name of the textbox not eqaul to property "Category", but something else?
Thanks.

<%: Html.TextBox("someOtherName") %>

You have to change the default editor template or add a new template for this. You can refer this blog post from Brad Wilson for some insight.

Related

Why does ASP.NET Core Tag Helper for Boolean Checkbox Not Display Label?

I'm looking at the login.cshtml page of a new asp.net core account view page and I notice that the use DisplayNameFor to output the name of the field ("RememberMe"). Why is the not sufficent for this? It seems the label is not participating.
<div class="checkbox">
<label asp-for="RememberMe">
<input asp-for="RememberMe" />
#Html.DisplayNameFor(m => m.RememberMe)
</label>
</div>
Since input and label are both Tag Helpers, shouldn't the name be rendered automatically like for email and other input tags on same page?
Very interesting question.
Per MVC code, the labeltaghelper gives precedence to available child content. Only when no child content is available will it put in a default label text (order and possible values per)
If you remove all child elements of the label you will notice that the label will in fact use the display name or property name (Remember Me)
Personally, I think in the template, it's just a design decision to use #Html.DisplayNameFor(m => m.RememberMe)so that it goes well with bootstrap.
Update: This would show the same information but the formatting is not going to be as good
<div class="checkbox">
<input asp-for="RememberMe" />
<label asp-for="RememberMe">
</label>
</div>

Assign ViewBag value to input checkbox?

I am trying to assign ViewBag value in Boolean to an HTML Input checkbox. It is throwing the following error: Cannot resolve symbol '<%= ViewBag.Solicitation %>'
<input id="chkSolicitation" type="checkbox" name="chkSolicitation"
checked="<%= ViewBag.Solicitation %>" />
I recommend using this, assuming you are using Razor:
#Html.CheckBox("chkSolicitation", (bool)ViewBag.Solicitation)
If you are not using Razor, use this:
<%: Html.CheckBox("chkSolicitation", (bool)ViewData["Solicitation"]) %>

asp.net mvc posting form values using html.actionlink

how can i post the form values using html.actionlink, don't want to use routes dictionary
<%=Html.ActionLink("Download", "MyFiles", "Jobs", null, new { #class = "cvclick" })%>
A link points to an HTTP GET request.
An HTTP GET request is sent to a URL; the URL must be defined using a route.
To POST values you could use an HTML form:
<% using (Html.BeginForm("MyFiles", "Jobs")) { %>
<%= Html.Hidden("key1", "value1") %>
<%= Html.Hidden("key2", "value2") %>
<input type="submit" value="Download" />
<% } %>
To POST values you could use Spark View Engine and a HTML form:
<form action="myfiles" controller="jobs">
<hidden name="key1" value="value1" />
<hidden name="key2" value="value2" />
<submit title="Download" />
</form>
(The code uses some pretty standard bindings that be wired in Spark)
As for links and ActionLink. I would use the ajax helper instead since it can POST stuff. (Ajax.ActionLink)
Edit
So you want to DOWNLOAD a file? Well. The link should point on an action in your controller. The action should return a FileResult with your file. See here: http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.file(v=vs.90).aspx
You can post the data using :-
anthing inside Beginform will be posted
<% using (Html.BeginForm("ActionName", "ControllerName"))
{ %>
texbox code
<input type="submit" class="dASButton" value="Submit" />
<% } %>

MVC Get ID of object

in the following post: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html
<%= Html.Editor(prop.PropertyName) %>
Html.Editor creates a text box with MVC generated name. How do I get/access that name? id?
So in my code this gets generated:
<input type="text" value="Right" name="Template.RightColumn.ContainerName" id="Template_RightColumn_ContainerName">
and I want to generate this:
<div id="Template_RightColumn_ContainerName"></div>
so I need to access PropertyId. how do i do it?
thanks
I found it. you can use these:
<%: ViewData.TemplateInfo.HtmlFieldPrefix %>
or
<%: ViewData.TemplateInfo.GetFullHtmlFieldId("HtmlContent") %>
where HtmlContent is the name of your field. prefix will be generated automatically.
thanks

asp.net mvc Html.Textbox, unable to set value?

in my route table I have this entry
routes.MapRoute(
"myRoute",
"route/{controller}/{action}/{id}/{start}/{end}",
new { controller = "Home", action = "Index", id = "", start="", end="" }
);
in my master page I have a line of code like so:
<%= Html.TextBox("foo", "bar") %>
If I access the page in the form of http://mysite.com/route/Home/Index/id/start/end the textbox renders OK with a value of "bar"
However if I access the page using the default parameters http://mysite.com/route/ the textbox does not have a value! In the emitted HTML it shows up like so:
<input id="foo" type="text" value="" name="foo"/>
it didn't set the value to "bar"...is this a bug? or is this not allowed in mvc master pages?
it should work fine
" <%= Html.TextBox("name", "Please enter your name...")%>
Output : < input id="name" name="name" type="text" value="Please enter your name..." />
<%: Html.TextBox("foo", "bar") %>
sometimes you need to force it to be a simple html attribute as follows
<%: Html.TextBox("foo", null,new{value="bar"}) %>

Resources