I have a text area to show the description and my model for the same looks like
[Display(Name = "CopyrightHolder_AdditionalInfo_Label", ResourceType = typeof(Resource))]
public string AdditionalInfo { get; set; }
And in Razor i am using the form like this
<div class="form-group">
#Html.LabelFor(model => model.AdditionalInfo)
#Html.TextAreaFor(model => model.AdditionalInfo, new { #class = "form-control", #rows = "5", #placeholder = "i wnat to load from resource file this text", #maxlength = "545" })
#Html.ValidationMessageFor(model => model.AdditionalInfo)
</div>
I am also planning to show a placeholder on this text area which should load from resource file
Is there any way to do the same ?
you can add it like below:
#Html.TextAreaFor(
model => model.AdditionalInfo,
new {
#class = "form-control",
#rows = "5",
#placeholder = #Resources.Strings.YourKeyString,
#maxlength = "545"
}
)
Related
I need to retrieve the value of the startdate so that I can combine the startdate with the start time and get the datetime. How do I retrieve the data or the value of the #html.EditorFor?
I have already tried using jQuery to get the data but I can't find the way to link the jQuery and the values inside the #{} up.
#Html.EditorFor(model => model.StartDate, new { htmlAttributes = new {
#class = "form-control border-input", #type = "date", id = "modelId" }
})
#Html.ValidationMessageFor(model => model.StartDate, "", new { #class =
"text-danger" })
#Html.EditorFor(model => model.StartTime, new { htmlAttributes = new {
#class = "form-control border-input", #type = "time", id = "modelId2" } })
#Html.ValidationMessageFor(model => model.StartTime, "", new { #class =
"text-danger" })
I expect that I can combine the date and time together
I am rendering a drop down list box using my enums and I only have 3 options, but for some reason it is displaying four. The top and default option is simply blank/empty and I want this removed. I want the top/default value to be 'Option1'.
Enums:
public enum EventType
{
[Display(Name = "Option 1")]
Option1,
[Display(Name = "Option 2")]
Option2,
[Display(Name = "Option 3")]
Option3
}
View:
#Html.EnumDropDownListFor(model => model.EventType, null, new { #id = "eventType", #class = "form-control" })
Thanks for your help.
Your second parameter is the "Option Label" which is used to set the first item in the dropdown. From the documentation: "The text for a default empty item"
Use an overload that doesn't take in the option label:
#Html.EnumDropDownListFor(model => model.EventType, new { #id = "eventType", #class = "form-control" })
UPDATE
I just tried your code. When I do both:
#Html.EnumDropDownListFor(model => model.EventType, null, new { #id = "eventType", #class = "form-control" })
And
#Html.EnumDropDownListFor(model => model.EventType, new { #id = "eventType", #class = "form-control" })
I get:
The only time I get another option in the dropdown is when I pass in a string as the second parameter, such as "Select..."
Any chance you are doing something with javascript to add an item to the dropdown?
I also had the same problem.Solved this starting enum from 0 not 1 .
public enum EventType
{
[Display(Name = "Option 1")]
Option1,
[Display(Name = "Option 2")]
Option2,
[Display(Name = "Option 3")]
Option3
}
#Html.EnumDropDownListFor(model => model.EventType,
new {#id = "eventType", #class = "form-control" })
I spent some time getting the above to work and was unsuccessful.
I found an alternative way :
HTML:
#Html.DropDownList("types",
Helper.Gettypes(),
new { #class = "form-control", #title = "--- Choose ---" })
Code:
public static List<SelectListItem> Gettypes()
{
var enums = Enum.GetNames(typeof(WorkLogType)).ToList();
var selectLIst = enums.Select(x => new SelectListItem {Value = x, Text = x}).ToList();
return selectLIst;
}
I try to show/hide a tinymce with radobutton. Like yes/no. So there are two radio buttons. yes - will show the tiny mce and no will hide the tinymce.
I have this:
showing tiny mce:
<div class="form-group">
#Html.Label(Resources.Entity.Product.PdfMessage, new { #class = "text-bold control-label col-md-2" })
<div class="col-lg-6 col-md-8 col-sm-10 ">
#Html.EditorFor(model => mailModel.PdfMessage, new { htmlAttributes = new { #class = "form-control tiny-mce", data_lang = System.Globalization.CultureInfo.CurrentUICulture.Name, id = "tinyMCE" } })
#Html.ValidationMessageFor(model => mailModel.PdfMessage)
#*#Html.TextArea("MailProductHandlers_message", mailModel.Message, new { #class = "form-control", #rows = 15 })*#
</div>
</div>
these are my radio buttons:
<div class="form-group">
#Html.Label(Resources.Entity.Product.GeneratePDF, new {#class = "text-bold control-label col-md-2" })
<div class="col-lg-6 col-md-8 col-sm-10 ">
#Html.Label(Resources.Entity.Product.GeneratePDFYes) #Html.RadioButtonFor(model => model.IsChecked, "Yes", new { #checked = true, id = "radioButton" })
#Html.Label(Resources.Entity.Product.GeneratePDFNo) #Html.RadioButtonFor(model => model.IsChecked, "No", new { #checked = true, id = "radioButton2" })
</div>
</div>
and this is my javascript:
<script>
$(document).ready(function () {
$("input[Id='radioButton']").click(function () {
if ($(this).is(':checked')) {
$('#tiny-mce').show();
}
else {
$('#tiny-mce').hide();
}
});
});
</script>
if I do this: $('#mceu_61').hide(); it hides the editor. but after I press on yes, it shows the editor. but if I then press No it doesnt hide anymore. And I have this:
#Html.EditorFor(model => mailModel.PdfMessage, new { htmlAttributes = new { #class = "form-control tiny-mce", #id = "mceu_61", data_lang = System.Globalization.CultureInfo.CurrentUICulture.Name } })
Your missing an #symbol for the id attribute:
Modify your script as well like this:
***EDIT some thing seems off about the radio buttons only one should be checked and they should have the same name **
you can use the # to denote and ID in Jquery by the way instead off looking it up by attribute
EDIT I changed a few things around, I don't think you need two individual ids I grouped them with a class, you should be able to check the on change event from that class instead of checking on both ids
#Html.EditorFor(model => mailModel.PdfMessage, new { htmlAttributes = new { #class = "form-control tiny-mce", #id = "tinyMCE", #data_lang = System.Globalization.CultureInfo.CurrentUICulture.Name } })
#Html.RadioButtonFor(model => model.IsChecked, "Yes", new { #checked = true, #class = "pdfchecker" })
// only one should be checked
#Html.RadioButtonFor(model => model.IsChecked, "No", new { #checked = false, #class = "pdfchecker" })
<script>
// this is short hand for document ready
$(function () {
//# is to denote an ID, . is to denote a class in jQuery the change function is hit when a radio button is change check the name to make sure they are apart of the same grouping
$(".pdfchecker").change(function () {
if ($(this).is(':checked')) {
$('#tiny-mce').show();
}
else {
$('#tiny-mce').hide();
}
});
</script>
I need to disable an input dynamically when its value is equal to "*". How can I achieve this using the MVC Razor?
#Html.TextBoxFor(m => m.Digits[0], new { #class = "form-control input-lg label_16", #placeholder =
"1st", (Model.Digits[0] == "*" ? "disabled" : "") })
The above code doesn't compile
Is this possible?
Try using ternary operator
#Html.TextBoxFor(m => m.Digits[0], Model.Digits[0] == "*" ? (object)new { #class = "form-control input-lg label_16", #placeholder =
"1st", #disabled = "disabled" } : new { #class = "form-control input-lg label_16", #placeholder =
"1st" })
in the code above, the second parameter of #Html.TextBoxFor helper method will be based on the value of Model.Digits[0]. If it's * then the parameter would include the disabled attribute
new { #class = "form-control input-lg label_16", #placeholder =
"1st", #disabled = "disabled" }
otherwise
new { #class = "form-control input-lg label_16", #placeholder =
"1st" }
I am making a registration form for my website, and currently i have it like this
and the code for that is :
#Html.Label("Firstname")
#Html.TextBoxFor(model => model.Firstname, new {#required = "require", #style = "width:75%;", #class = "form-control" })
#Html.ValidationMessageFor(model => model.Firstname, null, new { #style = "color:red;" })
<br />
#Html.Label("Surname")
#Html.TextBoxFor(model => model.Lastname, new { #required = "require", #style = "width:75%;", #class = "form-control" })
#Html.ValidationMessageFor(model => model.Lastname, null, new { #style = "color:red;" })
<br /> . . .
What i am trying to do is to add the title for example "First Name" inside the textbox, when data is entered it would disappear, To look like this
I have tried adding:
#name ="FirstName"
#label ="FirstName"
#title ="FirstName"
but all of those did nothing.
You are talking about placeholder. It's a HTML attribute, just add it to the custom htmlAttributes parameters, for exemple:
#Html.TextBoxFor(model => model.Firstname, new {#required = "require", #style = "width:75%;", #class = "form-control", placeholder = "First Name" })
It seems you are looking for the Html5 placeholder attribute
#Html.TextBoxFor(model => model.Firstname, new {#required = "require", #style = "width:75%;", #class = "form-control", placeholder = "First Name" })
This will add default value to your TextBox
#Html.TextBoxFor(model => model.Firstname, new {#required = "require",
#style = "width:75%;",
#class = "form-control",
#value = "FirstName"})
Or if you want it to dissapear on entering the textbox
#Html.TextBoxFor(model => model.Firstname, new {#required = "require",
#style = "width:75%;",
#class = "form-control",
placeholder = "FirstName"})