How to set ID and Text in html.label helper in mvc2 - c#

I want to set ID and Text attribute in html.label helper in mvc2
<%:html.label<have to set ID and Text properties here>%>
Plz help me out..

The Html.Label method returns an HTML label element and the property name of the property that is represented by the specified expression. For example:
ASPX Syntax
<%: Html.Label("Text Content", new { id = "labelId" })%>
Razor Syntax
#Html.Label("Text Content", new { id = "labelId" })
The second parameter is the htmlAttributes, so, you can add any html attribute you want as a property of this anonymous object. For example:
new { id = "id-element", name = "name-element", size = 10, #class = "css-class" }
IdFor
If you want to take the Id by a html helper method, try to use:
#Html.IdFor(model => model.Property)

Related

Duplicate name attribute generated for the HTML Helper

I get a duplicate name attribute while validating through https://validator.w3.org
#Html.EditorFor(model => model.User_Name, new { htmlAttributes = new { #class = "form-control", id = "txtFullNameRealtor", #Name = "txtFullNameRealtor", placeholder = "Full Name" } })
I get this in the html source
<input Name="txtFullNameRealtor" class="form-control text-box single-line" id="txtFullNameRealtor" name="User_Name" placeholder="Full Name" type="text" value="" />
As you can see there,two name attributes are generated that is Name="txtFullNameRealtor" and name="User_Name".
Is there a way to make it generate a single name attribute ?
And I want the Name attribute be explicitly set by me to be there.
You could acheive this with the following:
#Html.TextBox("txtFullNameRealtor", Model.User_Name, new { #class = "a-class-name" })
As it looks like you want a text box anyway you can use the text box helper. The first argument is where the name/id are derived from, the second argument is the value and the 3rd are the htmlAttribtues. There are more overloads as well.
User Html.Editor() instead of Html.EditorFor().
EditorFor will infer the html element name from the model property that is "bound" to it.
Editor allows you to declare whatever html name you would like with the last parameter.
public static MvcHtmlString Editor(
this HtmlHelper html,
string expression,
string templateName,
string htmlFieldName
It sounds like you are binding a domain model directly to your UI. I'd suggest create a viewmodel with the property names you want and translating between your view model and domain model in your controller instead.
You can try this
#Html.EditorFor creates name same as that of property name.
if you want to specific name you can use #Html.Editor instead of #html.EditorFor. i hope this helps
#Html.Editor("txtFullNameRealtor" er_Name, new { htmlAttributes = new { #class = "form-control", id = "txtFullNameRealtor", placeholder = "Full Name" } }

DropdownList without a name attribute

I'm building a form in my project, but because I'm using AJAX to post the form values and multiple instances of a certain dropdown (class attribute instead of id), I'm not in need of a name attribute on my form controls.
Is there a way to create a dropdown list dynamically but explicitly omitting the name attribute without getting an exception?
#Html.DropDownList(string.Empty, Model, new { #class = "year" })
Gives me this exception:
Value cannot be null or empty.
Parameter name: name
because first argument is empty.
You can do it like following.
#Html.DropDownList("aName", Model, new { Name=string.Empty, #class = "year" })

DropDownList without value at the PostBack

I have created a DropDownList which is initialized from code behind like that :
Code Behind :
List<SelectListItem> ddlIsActivated = new List<SelectListItem>
{
new SelectListItem
{
Text = "Activated",
Value = "0"
},
new SelectListItem
{
Text = "Not activated",
Value = "1"
}
};
ViewBag.ddlIsActivated = ddlIsActivated;
View :
<div class="row">
<div class="form-group col-xs-3">
#Html.DropDownList("IsActivated", ViewBag.ddlIsActivated as List<SelectListItem>, "Default")
</div>
</div>
When I click directly on the search button after the load, the DropDownList has "Default" as the first item and my URL looks like that :
http://localhost:51817/Log?SearchString=&IsActivated=
Is it possible to specify that all parameters with an empty value might not be passed on the URL ?
In case of the DropDownList, is it possible to avoid the param "IsActivated" when the "Default" is Selected ?
Sure one way is when the submit button is clicked or activated, you set the fields that are the default value to disabled. then they don't go in the post.
Example of using jQuery onsubmit:
How to use jQuery to onsubmit and check if the value is 2 - 5 characters?
You can try having a separate viewmodel, with properties for the list of options and the selected option. In the GET request action method, you can populate this list and render the view, and in the POST action method, you can exclude this property during model binding, so that only the selected item property will get bound. Thus, you don't need ViewBag or routeValues or querystring parameters, and your URL will always look clean.
EDIT:
This is how you might do it.
public class ViewModel
{
public int SelectedId { get; set; }
public Dictionary<int,string> OptionList { get; set; }
}
And then in your view,
#Html.DropDownListFor(model => model.SelectedId, new SelectList(Model.OptionList, "Key", "Value"), null, null)
In your GET action method,
Dictionary<int,string> OptionList = GetOptionList(); // Populate from DB
return View(new ViewModel { OptionList = OptionList });
Also, remember to [Bind(Exclude="OptionList")] in your POST action method.

passing multiple parameter to another action using anchor tag not using html.actionlink

I want to pass 2 or 3 parameters to another action result via query string using anchor tag instead of Html.ActionLink, because am doing dynamically in C# adding anchor tag to particular element.
here dt is table which contains the columnd are survey_id and vin .
i am trying to replace particular column value as hyperlink and passing params to another action. please refer below code.
string surveyId ,vin;
foreach (DataRow dr in dt)
{
// Url.Action(
surveyId = dr["Survey_Id"].ToString();
vin = dr["VIN"].ToString();
//dr["Survey_Id"] = "#Html.ActionLink(" + surveyId.ToString() + ",'Index','SurveySummary', new RouteValueDictionary(new { area = 'Corporate' }), new { #class = 'NNAHyperlink' })".ToString();
dr["VIN"] = "<a href='../corporate/surveysummary?vinid='"+vin+"class='HyperLink' style='text-decoration: underline;'>" + vin + "</a>";
}
corporate is controller and survey summary is one of the action result. i need to pass vin as well survey id dynamically to another action.
but i tried to pass vinid dynamically , it doesn't works correctly
it returns the URL like "http://xx.com/corporate/surveysummary?vinid="
how can i change this ?
I use url.action for this. try
#Url.Action("Action", "Controller", new { surveyId = dr["Survey_Id"].ToString(), vin = ...
Edit:
regarding your last comment it is probably easiest to use the viewbag for this. So in your controller:
ViewBag.SurveyId = dr['Survey_Id'].ToString();
ViewBag.Vin = vin;
and then on your view
<a href= '#Url.Action('Index', 'corporate/surveysummary', new { surveyId = ViewBag.SurveyId })'>#ViewBag.Vin</a>

How do I limit the length of characters in a textbox in MVC?

I would like to limit a textbox to 10 characters in MVC.
<label ID="lbl2" runat="server" Width="20px"></label>
<%=Html.TextBox("polNum") %>
<label ID="lbl1" runat="server" Width="10px"></label>
I know you can set the Max Length property in .net. How do I do that in MVC with a textbox generated this way?
You need to set some html properties...
something like:
<%=Html.TextBox("polNum",null, new {maxlength=10}) %>
good luck
Do it in plain HTML:
<%= Html.TextBox("polNum", null, new { #maxlength = "25" }) %>
(The null parameter is because you don't want a default value...)
<%=Html.TextBox("polNum", new { maxlength = 10 }) %>
http://msdn.microsoft.com/en-us/library/dd492984.aspx
HtmlHelper uses reflection to examine the anonymous type. It converts the fields of the type into attributes on the, in this case, TextBox control. The resulting HTML looks like
<Textbox id="polNum" maxlength =10 />
You can use the anonymous type to add other relevant attributes, such as
new { #class = "MyCssClass", type = "password", value="HurrDurr",
textmode="multiline" }
Use the overload of the TextBox method which is getting Html attributes :
Html.TextBox( "polNum", "value", new { maxlength="10" } );
Use below html for set max length of TextBox
In Html.TextBox second parameter is value for textbox So, you can pass "" or null
<%=Html.TextBox("polNum", "", new { maxlength = 10 }) %>
#Html.EditorFor(model => model.Telephone, new { htmlAttributes = new { #class = "form-control", #maxlength = "10" } })
Here i use html.Editor (do not use Textboxfor) For and i am use the Telephone number field in my database. i do not want the user to type more than ten numbers for telephone.

Categories