I'm building a web application with a number of lengthy forms (~30 fields each). Many of the fields have long label names and I'd prefer to have short input fields. I'm arranging these fields into rows of 3-5 fields each.
An example where I'd like the label to overflow into the additional white space between form fields rather than wrap the text:
Code:
<div class="col-md-2">
#Html.LabelFor(model => model.Oopmaxind, htmlAttributes: new { #class = "control-label" })
#Html.EditorFor(model => model.Oopmaxind, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Oopmaxind, "", new { #class = "text-danger" })
</div>
Alternately, if anyone has an example of elegantly designed forms that have long labels and many fields, please share.
Thanks!
Related
Hopefully this is an easy one...
I am having an issue with Bootstrap 5 floating labels not working when I create HTML elements using Razor syntax.
If I use plain HTML they work as expected. Using razor the labels are appearing in the state you'd expect if the text box has focus (top left of input)
<div class="form-floating mb-3">
#Html.EditorFor(model => model.Recipient, new { htmlAttributes = new { #class = "form-control", #onchange = "javascript: Changed( this, 'recipient-name' );" } })
#Html.ValidationMessageFor(model => model.Recipient, "", new { #class = "text-danger" })
#Html.LabelFor(model => model.Recipient)
</div>
Here is an image of the above on load -
Code output in UI
Has anyone had this issue, know a way to get around it or spot what I am doing wrong? (I need the input tag to be populated from the model as the form can be used to create a new request or update and existing request)
Thanks
Do you want something like below?
<div class="form-floating">
<input asp-for="Recipient" class="form-control" />
<label asp-for="Recipient"></label>
<span asp-validation-for="Recipient" class="text-danger"></span>
</div>
Thanks but I figured out what I was doing wrong. The issue was simple...
ISSUE - There was no placeholder tag which this animation relies on.
RESOLUTION - Add #placeholder = "Recipient Name"
To provide a bit more info the text input looks different when in focus/not focused. This was the issue.
It should have looked like this when not focused - Not Focused
But it was looking like this - Focused
The code that fixed the issue is
<div class="form-floating mb-3">
#Html.EditorFor(model => model.Recipient, new { htmlAttributes = new { #class = "form-control", #onchange = "javascript: Changed( this, 'recipient-name' );", #placeholder = "Recipient Name" } })
#Html.ValidationMessageFor(model => model.Recipient, "", new { #class = "text-danger" })
#Html.LabelFor(model => model.Recipient)
</div>
I am new to Entity Framework/MVC and need to know how to modify a dropdown menu autogenerated by Visual Studio. I imagine it should be pretty simple, but I have not been able to figure it out. I have used the database-first approach and have the two following tables in my database:
public partial class RestaurantRating
{
public int RestaurantRatingId { get; set; }
public int RestaurantRatingScore { get; set; }
}
public partial class RestaurantType
{
public int RestaurantTypeId { get; set; }
public string RestaurantTypeDesc { get; set; }
}
I removed the extra details, but basically one of them will store restaurant ratings (the rating being an integer) and the other one will store restaurant types (what type of food they serve). The only really difference between the two is that the rating is an integer and they type description is a string.
Visual Studio autogenerated code for the CRUD operations for these and other tables. The HTML code in Create.cshtml for these two tables is as follows:
<div class="form-group">
#Html.LabelFor(model => model.RetaurantTypeId, "RetaurantTypeId", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("RetaurantTypeId", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.RestaurantTypeId, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.RestaurantRatingId, "RestaurantRatingId", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("RestaurantRatingId", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Id, "", new { #class = "text-danger" })
</div>
</div>
And the ViewBag information for these two tables in the Create Action result in the controller is the following:
ViewBag.RestaurantRatingId = new SelectList(db.RestaurantRating, "RestaurantRatingId", "RestaurantRatingId");
ViewBag.RestaurantTypeId = new SelectList(db.RestaurantType, "RestaurantTypeId", "RestaurantTypeDesc");
The problems and expected results are the following:
The dropdown menu for RestaurantType works as expected. It simply loads the different types into a dropdown menu and allows the user to select one of them. However, the RestaurantRating will load the RatingIds instead of the descriptions, which is what I need. I have tried changing the viewbag without success.
The HTML code automatically selects the first value for the dropdown menus, but it is possible to save NULL values to the database for these fields. How can I add an empty default value for the dropdown menus above, so that if the user selects the empty value (or does not touch the dropdown menu) a NULL value will be pushed to the database?
Any help is greatly appreciated. I will be happy to provide any additional code/information. Thank you so much!
You Just need to add an option label to your dropdown like this:-
#Html.DropDownList("RetaurantTypeId", null,"optionLable goes Here", htmlAttributes: new { #class = "form-control" })
and make sure on the other side that you are binding it to a nullable model property so the model binder will be able to set the model property value to Null.
hope this answer your question.
I have a create view that allows users to enter some basic information. I need to be able to let them upload an image. This is the code that is generated when I add the controller to the project for it:
<div class="form-group">
#Html.LabelFor(model => model.Picture, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Picture, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Picture, "", new { #class = "text-danger" })
</div>
</div>
I don't know what code to put in this segment to give me the "choose a file" control. I know there has to be something out there pre-made but for the life of me I can't find it. On a related note: what should I define the type of the image to be in the model I created for it? After looking around, I saw that people were suggesting byte[] but I don't know how to display that in my index view. Any help would be greatly appreciated.
I have one text area field called Description in my view. I wish to increase the size of that field.
My code
<div class="col-sm-3">
<div class="form-group">
<span style="color: #f00">*</span>
#Html.LabelFor(model => model.Description, new { #class = "control-label" })
#Html.TextAreaFor(model => model.Description, new { #class = "required", style = " rows=10, columns=40" })
#Html.ValidationMessageFor(model => model.Description)
</div>
</div>
My TextArea
I want to bring as like which is mention in the below image
So i gave Rows and columns in textarea field. It increase the size of the text area. But when i shrink the page to phone size means all fields got shrink . But this textarea field is not shrink up to the page size. This is the issue
I kept validation for my fields. I want to show that validation in Red color. I'm using Model validation.
Try This:
#Html.TextAreaFor(model => model.Description, 10,40, htmlAttributes: new {style="width: 100%; max-width: 100%;" })
While this is already answered, Someone out there might still need this.
#Html.TextAreaFor(model => model.comments,
new { #cols = "100", #rows = "8", #style="width:100%;"})
Rows and Cols does not come under style tag. Its an independent attribute for textarea. You can write it as below.
#Html.TextAreaFor(model => model.Description, new { #class = "required", #cols = 40, #rows = 10})
Remove textArea element from site.css file. In site.css textarea max-width set as 280px;
Do like
input
,select
/*,textarea*/
{
max-width: 280px;
}
#Html.TextAreaFor(m => m.Description, 10, 40, new { #class = "form-control required", style="max-width:75% !important;" })
#Html.TextAreaFor(model => model.Description, new { #class = "form-control", #cols = 10, #rows = 10,#style="resize:both;" })
#Html.TextAreaFor(model => model.Description,10,40, new { #class = "required"})
I'm working on the simple webpage where user will be able to change some data.
I'm using #Html.EditorFor for changing that data. But I have some problems.
Here You can see my HTML Code:
<div class="form-group">
#Html.LabelFor(model => model.DeviceUser, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.DeviceUser, new { #Value = ViewBag.id})
#Html.ValidationMessageFor(model => model.DeviceUser)
</div>
</div>
As You can see I'm trying to replace DeviceUser with new Id which is passed from Controler using ViewBag.
But for unknow reason for me this textbox always holds old value.
Can anyone suggest me how to fix it?
Try changing this
#Html.EditorFor(model => model.DeviceUser, new { #Value = ViewBag.id})
to
#Html.TextBoxFor(model => model.DeviceUser, new { #Value = ViewBag.id})