I used [prompt("name")] in Controller and
#Html.EditorFor(model => model.Email,
new {placeholder=ViewData.ModelMetadata.Watermark})`
in view but nothing showed
I also used help of Html5 Placeholders with .NET MVC 3 Razor EditorFor extension?
but nothing happened
I need Placeholder for #EditorFor not for Textbox
Any Help Appreciated
You can write HTML attributes into Html.EditorFor using the following syntax. Make sure you use # before class and placeholder so that the renderer knows this is HTML markup.
#Html.EditorFor(model => model.Email, new {
htmlAttributes = new {
#class = "form-control",
#placeholder = "Email Address"
}
})
EditorFor doesn't accept HtmlAttribut argument you have to use TextBoxFor instead of it ;)
try this:
#Html.TextBoxFor(model => model.Email, new {placeholder=ViewData.ModelMetadata.Watermark})`
Work for me just with TextBoxFor.
#Html.TextBoxFor(model => model.emailContact, new {#placeholder = "Your Placeholder Text" }
Related
I have a textbox inside a form tag like this:
#Html.TextBoxFor(m => m.Email, new { #class = "form-control", #type = "email", #aria_describedby = "emailHelp", #text=Request.QueryString["Email"], #value=Request.QueryString["Email"] })
<label>Email</label>
#Html.ValidationMessageFor(model => model.Email, "", new { #class = "text-danger", #style = "float:right;" })
Now here is the trick... I have an url that goes like this:
example.com/Registration?Email=someemail#example.com
Now I'm trying to set the value of my textbox automatically by setting the value like this:
#text=Request.QueryString["Email"], #value=Request.QueryString["Email"]
But this doesn't works... The textbox is still empty after the page is loaded...
And I have looked into the html for example and I can see for example text attribute of my input text tag being set to someemail#example.com
What am I doing wrong here?
Okay I found a way to do it. Turns out this is the valid way to do it:
public ActionResult Registration()
{
var regViewModel = new UserRegistrationViewModel { Email = Request.QueryString["Email"] };
return View(regViewModel);
}
I'm currently writing an ASP.Net MVC application
I've successfully made an input box in CSHTML.
#Html.EditorFor(model => model.Test, new { htmlAttributes = new { #class = "form-control"} })
I now want to take the content of a string from the controller and add is as placeholder text. I've tried the following
#Html.EditorFor(model => model.Test, new { htmlAttributes = new { #class = "form-control", placeholder = #Html.DisplayFor(model => model.Test) } })
But that set the placeholder like so: placeholder=""
If I instead use #Html.DisplayNameFor it sets the placeholder to the variable's name Test, but I want the actual string content. I'm aware that one can use the Viewbag to pass text to the view, but there must be a way of doing it this way.
Any help is greatly appreciated.
You can't use #Html.DisplayFor() as a placeholder but you can use Html.DisplayNameFor().
Example:
#Html.EditorFor(model => model.Test, new { htmlAttributes = new { #class = "form-control", #placeholder = "Your Place Holder" } })
OR
#Html.EditorFor(model => model.Test, new { htmlAttributes = new { #class = "form-control", placeholder = #Html.DisplayNameFor(model => model.Test) } })
I think you need to add the [Display (name="Your Text")] attribute in the propriety of the model like this:
[Display (Name="Name Of Client:")]
Public string Name {get; set;}
Make a Property in your Model which you are passing to view.
Assign the string into that property.
Model.SomeName= YourVariableName;
Then Try Like This :-
#Html.EditorFor(model => model.Test, new { htmlAttributes = new { #class = "form-control", placeholder = #Model.SomeName }
I inherited an MVC site and was asked to combine two pages into one. Since both Views have their own View Model, I thought instead of pushing the two VMs together that I could use a Partial View to display, in this case, the Clock Group in a View called Clock_Detail. However, I need to get the GroupId to populate the data for the Clock Group.
So, having rarely used Partial Views I’m confused on how to get this to work.
I created a new View called _ClockGroup.cshtml and just copied and pasted some basic code from the other page just to see if I could get anything to display.
In my ClockDetail controller I added the following.
public PartialViewResult ClockGroup(int groupId)
{
ClockGroupViewModel vm = DAL.GetClockGroupDetail(groupId);
return PartialView(vm);
}
Here is _ClockGroup.cshtml
#model site.Models.ViewModels.ClockGroupViewModel
<div class="form-group">
#Html.LabelFor(m => m.GroupId, htmlAttributes: new { #class = "control-label col-md-2" })
#Html.EditorFor(m => m.GroupId, new { htmlAttributes = new { #class = "form-control", disabled = "disabled", #readonly = "readonly" } })
</div>
<div class="form-group">
#Html.LabelFor(m => m.GroupName, htmlAttributes: new { #class = "control-label col-md-2" })
#Html.EditorFor(m => m.GroupName, new { htmlAttributes = new { #class = "form-control", autofocus = "autofocus" } })
#Html.ValidationMessageFor(m => m.GroupName, "", new { #class = "text-danger" })
</div>
This is from the clock_detail.cshtml view. It uses the following Model and then I added the Partial to this view.
#model site.Models.ViewModels.ClockDetailViewModel
#Html.Partial("_ClockGroup")
Of course I get an error that the model passed a dictionary type “site.Models.ViewModels.ClockDetailViewModel” but the dictionary requires a model item of time “site.Models.ViewModels.ClockGroupViewModel".
Will I need to go ahead and combine the two ViewModels together and just use the single ViewModel?
When you do not specify an object as the second parameter:
#Html.Partial("_ClockGroup")
It automatically passes the current model which is of type ClockDetailViewModel
Your partial requires type ClockGroupViewModel
The fix would be when you call the partial to pass in the ClockGroupViewModel property of your ClockDetailViewModel object:
#Html.Partial("_ClockGroup", Model.ClockGroupViewModelProperty)
If your ClockDetailViewModel class does not have a ClockGroupViewModel property, you will need to add that to your ViewModel and populate the data.
I want to give a unique id to htmleditfor method. I want to add Jquery datepicker in it. Please guide...
#Html.EditorFor(model => model.AdExpiryDate, new { htmlAttributes = new { #class = "form-control", placeholder = "MM/DD/YYYY" } })
Your generated html will look like this
<input type="text" id="AdExpiryDate" value="AdExpiryDate" />
You just need to add following javascript code at bottom of your view
<script>
$('#AdExpiryDate').datepicker();
</script>
As others have said, MVC will automatically add the property name from your model to the id and name attributes on the HTML element. However if you want to specify your own id for the element, just update your htmlAttributes:
#Html.EditorFor(model => model.AdExpiryDate, new { htmlAttributes = new { #class = "form-control", placeholder = "MM/DD/YYYY", id = "ad-expiry-date" } })
Then just update your javascript to target the new id that you added:
<script>
var expireDate = $('#ad-expiry-date').val();
alert(expireDate);
</script>
Probably a simple question, but i cant seem to find the answer.
using MVC 2 i have a series of Html.ValidationFor controls. I want to assign a CSS class to the text and cant seem to do it.
<%= Html.TextBoxFor(Model => Model.Chest, new { #class = "textBoxMeasure" })%>
<%= Html.ValidationMessageFor(Model => Model.Chest) %>
if i try the same method as textboxfor i get errors because it requires a string, when i put a string in it still wont work!
thanks
I added a comment to the accepted answer, but I cannot to format it for better view. So, here is my already formatted comment from the accepted response.
I had similar case and I used solution from accepted answer. But I desired to use message from model annotation. I tried this:
#Html.ValidationMessageFor(Model => Model.Chest, null, new { #class = "text-danger" });
and it correctly worked for me. I used MVC4 with bootstrap.
There's a variant that takes htmlAttributes as the third argument (the second is the message that should be displayed, or you can use null for the default validation message)
Html.ValidationMessageFor(
Model => Model.Chest,
"Please enter a value",
new { #class = "redText" })
For more info see http://msdn.microsoft.com/en-us/library/ee721293%28v=vs.98%29.aspx
Use the classes assigned to the span tag holding the message. If the field is valid the class is field-validation-valid. If there is an error its field-validation-error.
I use
.field-validation-error {
color:Red;
}
simplest way to do this to put #Html.ValidationMessageFor in div tag and apply css to div tag
<div style="font-size:15px !important;">
#Html.ValidationMessageFor(model => model.Password, "", new { #class = "text-danger" })
</div>
This way the color range will be wider.
#Html.ValidationMessageFor(x => x.WriterName, "", new { #style = "color:red" })