This question already has answers here:
MVC4 input field placeholder
(10 answers)
Closed 7 years ago.
In my ViewModel for my partial view I have Email defined like this:
[Display(Name = "Email")]
[EmailAddress]
public string Email { get; set; }
In the view I have this:
#Html.TextBoxFor(m => m.Email, new {#class = "form-control"})
#Html.ValidationMessageFor(m => m.Email, "", new { #class = "text-danger" })
I would like something like 'example#company.com' to appear in the textbox as a placeholder or example. But right now it is empty.
You can set a placeholder attribute (HTML5) to the form element. Browsers which supports this feature will add a placeholder text to your textbox.
#Html.TextBoxFor(s=>s.Email, new { #class="form-control", placeholder="sample#ss.com"})
Use this to add a placeholder attribute to your HTML:
#Html.TextBoxFor(m => m.Email, new {#class = "form-control", placeholder = "example#company.com"})
Related
How do I make the date field #Html.EditorFor receive date coming from a ViewBag.
Controller
var searchDateBD = db.Sequence.ToList().Select(l => l.DateFlow).Max();
ViewBag.date = searchDateBD;
View
#Html.EditorFor(model => model.Sequence.DateFlow,
ViewBag.date,
new { htmlAttributes = new { #class = "form-control input-lg" } })
In this way above developed by me is not correct.
How to do so that the field receives the date coming from the viewbag of the controller
You don't need ViewBag here. You can set the model property in the controller action and pass the model object to view back in the line returning View like:
model.Sequence.DateFlow = db.Sequence.Select(l => l.DateFlow).Max();
return View(model);
You also do not need to materialize the result by calling ToList() and leave the Max item to be calculates on the database server.
and in your view it would be like:
#Html.EditorFor(model => model.Sequence.DateFlow,
new { htmlAttributes = new { #class = "form-control input-lg" } })
You can overwrite the value using attribute #Value:
htmlAttributes = new { #class = "form-control input-lg", #Value = ViewBag.date }
Note that #Value is written with an uppercase letter V.
When I add any regular expression pattern in MVC for textarea it's not working. When I click on submit button the wrong value is added to database
My View Code:
#Html.TextAreaFor(model => model.MyEntity.Item, new { #required = "required", #class = "form-control english_only", #placeholder = "Item", #maxlength = "120", #Pattern = #"[^A-Za-z0-9]" })
My Model Code:
[StringLength(150)]
public string Item{ get; set; }
This is just for any other who have this problem...
Based on Developers comments:
HTML5 <textarea> element does not support pattern attribute
so the solution is by MVC built in validation (with jquery.validate.js) and jquery.validate.unobtrusive.js)
So this problem solve by change my model to be:
[StringLength(150)]
[RegularExpression("^[0-9]*$", ErrorMessage = "UPRN must be numeric")]
public string Item{ get; set; }
and also include the jquery.validate.js in my view or partial view
Ore simply change my html helper to be text box instead of text Area as following:
#Html.EditorFor(model => model.Item, new { htmlAttributes = new { #class = "form-control english_only", #placeholder = "Only english text", #required = "required", #Pattern = #"^[a-zA-Z0-9$#$!%*?&#^-_. +]+$", #title = "please insert english text only", #autocomplete= "off", #maxlength = "120" } })
This question already has answers here:
Enabling & disabling a textbox in razor view (ASP.Net MVC 3)
(4 answers)
Closed 5 years ago.
I'm developing an ASP.NET MVC 5 app with Razor, C# and .NET Framework 4.7.
I want to make it a textbox not editable if Model.IsChinaProduct is true.
I have this piece of code in a View:
#Html.TextBoxFor(m => m.Configurations[index].PkgRatio, new { #class = "productClass", #onkeydown = "IsValidKey(event);" #if (Model.IsChinaProduct) disabled})
I want to add the disabled attribute if Model.IsChinaProduct is true, but that code shows me the following error:
Error CS0746 Invalid anonymous type member declarator. Anonymous type
members must be declared with a member assignment, simple name or
member access.
How can I add the disabled attribute if Model.IsChinaProduct is true?
UPDATE:
Maybe disabled is not the right attribute.
AFAIK you can't, because there is no disabled="false", meaning you should do something like that:
#{
var htmlAttributes = Model.IsChinaProduct ? (object)
new { #class = "productClass", readonly = "readonly" }
: new { #class = "productClass", #onkeydown = "IsValidKey(event);" };
}
#Html.TextBoxFor(m => m.Configurations[index].PkgRatio, htmlAttributes)
For setting it ReadOnly, try this:
#{
object displayMode = (Model.IsChinaProduct) ? new { #class = "productClass", #onkeydown = "IsValidKey(event);" }
: new { #class = "productClass", #onkeydown = "IsValidKey(event);" readonly = "readonly"};
#Html.TextBoxFor(m => m.Configurations[index].PkgRatio, displayMode)
}
Dont use TextBoxFor if IsChinaProduct = true
Try DisplayFor combine with HiddenFor instead
like this
#if (Model.IsChinaProduct)
{
#Html.HiddenFor(m => m.Configurations[index].PkgRatio)
#Html.DisplayFor(m => m.Configurations[index].PkgRatio)
}
else
{
#Html.TextBoxFor(m => m.Configurations[index].PkgRatio)
}
I am trying to properly display a (nullable) Date-type-only field. For this field, my Model and ViewModel classes are defined as....
[DisplayFormat(DataFormatString = "{0:M/dd/yyyy}")]
[DataType(DataType.Date)]
public DateTime? PeriodEnd { get; set; }
In my details view (based on model), it's showing the date properly (excluding the time element):
#Html.DisplayNameFor(model => model.PeriodEnd)
Problem: In my edit view (based on ViewModel), it's showing the time also, which I'm trying to exclude. Here's how it's defined.
#Html.TextBoxFor(model => model.PeriodEnd, new { #class = "datepicker" })
I also tried....
#Html.TextBoxFor(model => model.PeriodEnd.Value().ToShortDateString(),
new { #class = "datepicker" })
... but that produced an error.
Is there a better way to accomplish this?
Well, first, you're not utilizing the data annotation. Something like DataType.Date doesn't do anything on its own. Using editor templates (Html.EditorFor), it can be utilized to provide an date type input, but if you use Html.TextBoxFor, it's basically ignored.
You can fix that by either 1) using Html.EditorFor instead or 2) explicitly setting the type: #Html.TextBoxFor(m => m.PeriodEnd, new { type = "date", #class = "datepicker" }).
This was the solution:
#Html.TextBoxFor(m => m.PeriodEnd, "{0:M/d/yyyy}", new { #class = "datepicker" })
Thanks to all for your feedback!
I am trying to get #String.Format("{0:0.00}",Model.CurrentBalance) into this #Html.TextBoxFor(model => model.CurrentBalance, new { #class = "required numeric", id = "CurrentBalance" })
I just want the currency to show up as .00 inside of my textbox but am having no luck. Any ideas on how I do this?
string.format("{0:c}", Model.CurrentBalance) should give you currency formatting.
OR
#Html.TextBoxFor(model => model.CurrentBalance, new { #class = "required numeric", id = "CurrentBalance", Value=String.Format("{0:C}",Model.CurrentBalance) })
#Html.TextBoxFor(model => model.CurrentBalance, "{0:c}", new { #class = "required numeric", id = "CurrentBalance" })
This lets you set the format and add any extra HTML attributes.
While Dan-o's solution worked, I found an issue with it regarding the use of form-based TempData (see ImportModelStateFromTempData and ExportModelStateToTempData). The solution that worked for me was David Spence's on a related thread.
Specifically:
[DisplayFormat(DataFormatString = "{0:C0}", ApplyFormatInEditMode = true)]
public decimal? Price { get; set; }
Now if you use EditorFor in your view the format specified in the annotation should be applied and your value should be comma separated:
<%= Html.EditorFor(model => model.Price) %>