How to change text input to select input in chtml? - asp.net

I have a text form
<form asp-action="Create">
<div class="form-horizontal">
<h4>Flight</h4>
<hr />
<div asp-validation-summary="ValidationSummary.ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="FlyDate" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="FlyDate" class="form-control" />
<span asp-validation-for="FlyDate" class="text-danger" />
</div>
</div>
<div class="form-group">
<label asp-for="FlyFrom" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="FlyFrom" class="form-control" />
<span asp-validation-for="FlyFrom" class="text-danger" />
</div>
</div>
<div class="form-group">
<label asp-for="FlyTo" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="FlyTo" class="form-control" />
<span asp-validation-for="FlyTo" class="text-danger" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
</form>
I want to change FlyFrom and FlyTo to select options where users can select cites name (Tokyo, Osaka...). Currently they are text field. How can I do it?

There are two ways to populate based on your requirement.
If values are constant you can directly hard code two drop downs.
<select>
<option>State1</option>
<option>State1</option>
</select>
if values are coming from DB you can fill those to values ViewBag object on index method
ViewBag.FromState=<<your list of states>>
ViewBag.ToDate==<your list of states>>
.cshtml
#Html.DropDownList("FromState",ViewBag.FromStaes)

Related

Positions of labels appearing at top instead of left

I am using bootstrap to style my admin using the adminlte theme I am trying to get my labels left to my textboxes but I am using the following
<div class="col-md-10">
<div class="nav-tabs-custom">
<ul class="nav nav-tabs">
<li class="active">Prodct Info</li>
<li>Product Images</li>
<li>Seo</li>
<li>Settings</li>
</ul>
<div class="tab-content">
<div class="active tab-pane" id="activity">
<form asp-controller="Products" asp-action="Create" asp-route-returnurl="#ViewData["ReturnUrl"]" method="post" class="form-horizontal" role="form">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="ProductName" class="control-label">Product Name</label>
<input asp-for="ProductName" class="form-control" />
<span asp-validation-for="ProductName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="LongDescription" class="control-label"></label>
<textarea id="editor1" name="editor1" rows="10" cols="80">
This is my textarea to be replaced with CKEditor.
</textarea>
</div>
<div class="form-group ">
<label asp-for="OldPrice" class="control-label"> Old Price</label>
<input asp-for="OldPrice" class="form-control" />
<span asp-validation-for="OldPrice" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="NewPrice" class="control-label">Price</label>
<input asp-for="NewPrice" class="form-control" />
<span asp-validation-for="NewPrice" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="SKU" class="control-label">Sku (Product No)</label>
<input asp-for="SKU" class="form-control" />
<span asp-validation-for="SKU" class="text-danger"></span>
</div>
</div>
Which gives me a layout as such But I want the labels of the controls to be to the left with equal padding
This is what I am trying to achieve does anybody no how to.
You've got the form-horizontal class added to <form>, but I think you also need to add grid column classes to define the widths of each label and input.
For example, add the class col-sm-2 to <label>, and then wrap the input and span inside
<div class="col-sm-10">
</div>
Using classes col-sm-* will cause the label and input to stack if the viewport is < 768px. If you want them to be horizontal always, then use col-xs-*.
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<!--Stacked if width < 768px -->
<form class="form-horizontal">
<div class="form-group">
<label asp-for="OldPrice" class="col-sm-2 control-label">Old Price</label>
<div class="col-sm-10">
<input asp-for="OldPrice" class="form-control" />
<span asp-validation-for="OldPrice" class="text-danger"></span>
</div>
</div>
</form>
<!-- Never stacked -->
<form class="form-horizontal">
<div class="form-group">
<label asp-for="OldPrice" class="col-xs-2 control-label">Old Price</label>
<div class="col-xs-10">
<input asp-for="OldPrice" class="form-control" />
<span asp-validation-for="OldPrice" class="text-danger"></span>
</div>
</div>
</form>
Source: https://getbootstrap.com/docs/3.3/css/#forms-horizontal
Use the form-inline class with each form-group as per Bootstrap's documentation. An example from your code would be:
<div class="form-group form-inline">
<label asp-for="OldPrice" class="control-label"> Old Price</label>
<input asp-for="OldPrice" class="form-control" />
<span asp-validation-for="OldPrice" class="text-danger"></span>
</div>
Edit: I missed the part where you wanted equal spacing. In that case Patrick O'Grady's answer should work. However, you have closed your outer div tag before closing the form. Here is the complete code you gave with the proper tags and closures:
<div class="col-md-10">
<div class="nav-tabs-custom">
<ul class="nav nav-tabs">
<li class="active">Prodct Info</li>
<li>Product Images</li>
<li>Seo</li>
<li>Settings</li>
</ul>
<div class="tab-content">
<div class="active tab-pane" id="activity">
<form asp-controller="Products" asp-action="Create" asp-route-returnurl="#ViewData["ReturnUrl"]" method="post" class="form-horizontal" role="form">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="ProductName" class="control-label col-sm-2">Product Name</label>
<div class="col-sm-10">
<input asp-for="ProductName" class="form-control" />
<span asp-validation-for="ProductName" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="LongDescription" class="control-label col-sm-2"></label>
<div class="col-sm-10">
<textarea id="editor1" name="editor1" rows="10" cols="80">This is my textarea to be replaced with CKEditor.</textarea>
</div>
</div>
<div class="form-group ">
<label asp-for="OldPrice" class="control-label col-sm-2"> Old Price</label>
<div class="col-sm-10">
<input asp-for="OldPrice" class="form-control" />
<span asp-validation-for="OldPrice" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="NewPrice" class="control-label col-sm-2">Price</label>
<div class="col-sm-10">
<input asp-for="NewPrice" class="form-control" />
<span asp-validation-for="NewPrice" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="SKU" class="control-label col-sm-2">Sku (Product No)</label>
<div class="col-sm-10">
<input asp-for="SKU" class="form-control" />
<span asp-validation-for="SKU" class="text-danger"></span>
</div>
</div>
</form>
</div>

Bootstrap Input Group pushes control little to left

I am trying to layout a form using bootstrap 3 but for some reason the input is pushed to left.
My markup looks like this
<div class="jumbotron">
<form name="invoiceForm" class="form-horizontal" role="form" data-toggle="validator">
<div class="form-group">
<label class="control-label col-sm-3" for="txt_job_date" >Date work performed</label>
<p class="input-group col-sm-3">
<input type="text"
class="form-control"
datepicker-popup="{{format}}" ng-model="dt"
is-open="opened" min-date="minDate"
max-date="'2015-06-22'"
datepicker-options="dateOptions"
date-disabled="disabled(date, mode)"
ng-required="true"
close-text="Close"
id="txt_job_date"
ng-model="job_date"
/>
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></button>
</span>
</p>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="txt_job_ref_no" required-asterix="" >Ref No</label>
<div class="col-sm-2">
<input type="text" class="form-control" ng-model="ref_no" id="txt_job_ref_no" placeholder="123RTE" required="">
</div>
<div role="alert">
<span class="error has-error alert-danger" ng-show="invoiceForm.txt_job_ref_no.$error.required">
Ref no is required!</span>
</div>
</div>
</form>
</div>
Here is the link to fiddle
Many thanks in advance!
This should help. Wrap the labels and elements in cols, and the form in a .container inside the .jumbotron. You were over-complicating the layout.
<div class="jumbotron">
<div class="container">
<form name="invoiceForm" class="form-horizontal" role="form" data-toggle="validator">
<div class="col-sm-12">
<div class="form-group">
<label class="control-label" for="txt_job_date">Date work performed</label>
<div class="input-group">
<input type="text"
class="form-control"
datepicker-popup="{{format}}" ng-model="dt"
is-open="opened" min-date="minDate"
max-date="'2015-06-22'"
datepicker-options="dateOptions"
date-disabled="disabled(date, mode)"
ng-required="true"
close-text="Close"
id="txt_job_date"
ng-model="job_date"
/>
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></button>
</span>
</div>
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<label class="control-label" for="txt_job_ref_no" required-asterix="" >Ref No</label>
<input type="text" class="form-control" ng-model="ref_no" id="txt_job_ref_no" placeholder="123RTE" required="">
</div>
</div>
</form>
</div>
</div>
http://jsfiddle.net/j8x15dbs/1/
Edit: Missed part about form being horizontal. Added new code below to reflect that.
This has the layout you're looking for
<div class="jumbotron">
<div class="container">
<form class="form-horizontal">
<div class="form-group">
<label class="control-label col-sm-3" for="txt_job_date">Date work performed</label>
<div class="col-sm-7">
<div class="input-group">
<input type="text"
class="form-control"
datepicker-popup="{{format}}" ng-model="dt"
is-open="opened" min-date="minDate"
max-date="'2015-06-22'"
datepicker-options="dateOptions"
date-disabled="disabled(date, mode)"
ng-required="true"
close-text="Close"
id="txt_job_date"
ng-model="job_date"
/>
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></button>
</span>
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label" for="txt_job_ref_no" required-asterix="">Ref No</label>
<div class="col-sm-7">
<input type="text" class="form-control" ng-model="ref_no" id="txt_job_ref_no" placeholder="123RTE" required="required">
</div>
</div>
</form>
</div>
</div>
http://jsfiddle.net/j8x15dbs/2/
A real easy thing to do - and a great way to learn - is to just copy the examples off of Bootstrap's site and replace their values with yours. :)
remove <col-sm-2> from before the input tag. As good practice, always use <div class="row"> before using <col-sm-12> and don't use rows or columns inside <div class="form-group">
Please read about bootstrap scaffolding to understand how to write good markup.

bootstrap form styling with elements left and right

Okay. I'm trying to recreate this form in particular using as much default bootstrap styling as possible.
The problem I'm having is having the left and right sidelooking like this.
I'm using a Modal class for the dark background.
<div class="panel-footer modal_custom">
<div class="container">
<h2>Talk to us</h2>
<h4>What's on your mind?</h4>
<form action="" role="form">
<div class="form-group col-lg-3">
<label for="form-elem-1"> Name</label>
<input type="text" id="form-elem-1" class="form-control" placeholder="Enter Your name" />
</div>
<div class="form-group col-lg-3">
<label for="form-elem-2"> Telephone</label>
<input type="text" id="form-elem-2" class="form-control" placeholder="Enter Your telephone number" />
</div>
<div class="form-group col-lg-3">
<label for="form-elem-3"> Email</label>
<input type="text" id="form-elem-3" class="form-control" placeholder="Enter Your e-mail" />
</div>
<div class="form-group col-lg-3">
<label for="form-elem-4"> Company</label>
<input type="text" id="form-elem-4" class="form-control" placeholder="Enter Your company name" />
</div><br />
<div class="form-group>
<label for="form-elem-4"> Enquiry</label>
<input type="text" id="form-elem-4" class="form-control" placeholder="Enter Your enquiry" />
</div>
</form>
</div>
</div>
I don't need much help on the color and those smaller things. just on how to position the Enquiry to the right and making it looks as close to the example as possible.
There isn't any css yet that affects much of the code above. And I'm failry new to the fomr function. How would I have the form actually work too.
Is this what you need, now you have add some your styles for button, and background
<div class="panel-footer modal_custom">
<div class="container">
<h2>Talk to us</h2>
<h4>What's on your mind?</h4>
<form action="" role="form">
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="form-group">
<label for="form-elem-1"> Name</label>
<input type="text" id="form-elem-1" class="form-control" placeholder="Enter Your name" />
</div>
<div class="form-group">
<label for="form-elem-2"> Telephone</label>
<input type="text" id="form-elem-2" class="form-control" placeholder="Enter Your telephone number" />
</div>
<div class="form-group">
<label for="form-elem-3"> Email</label>
<input type="text" id="form-elem-3" class="form-control" placeholder="Enter Your e-mail" />
</div>
<div class="form-group">
<label for="form-elem-4"> Company</label>
<input type="text" id="form-elem-4" class="form-control" placeholder="Enter Your company name" />
</div>
</div>
<div class="col-md-6 col-sm-6">
<div class="form-group">
<label class="col-md-3" for="comment">Comment:</label>
<div class="col-md-9">
<textarea class="form-control" rows="13" id="comment"></textarea>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12 text-right">
<div class="form-group">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
Working fiddle
http://jsfiddle.net/52VtD/9098/
Try to re-build that form using responsive Table. Set the table with two columns and get the required design without change in CSS.

Bootstrap 3 inline-form with different widths for input boxes

I have some bootstrap 3.0 based markup that produces an inline form like this:
The code for this is:
<div class="container">
<form class="form-inline" role="form" id="gh_save">
<div class="form-group">
<input type="text" placeholder="Github username" class="form-control" id="gh_user">
</div>
<div class="form-group">
<div class="col-lg-6">
<input type="text" placeholder="API Key" class="form-control" id="gh_apikey">
</div>
</div>
<button type="submit" class="btn btn-success"> Save </button>
</form>
I am trying to increase the width of the second box (API Key) to be twice what it is now, but no matter what I change the width of the div holding the second input is changing the width. Can someone show me how this done?
when I manually set the width of the second INPUT, I get this:
<form class="form-horizontal">
<div class="form-group">
<div class="col-xs-4">
<input type="text" placeholder="Github username" class="form-control" id="gh_user"/>
</div>
<div class="col-xs-6">
<input type="text" placeholder="API Key" class="form-control" id="gh_apikey" />
</div>
<div class="col-xs-2">
<button type="submit" class="btn btn-success"> Save </button>
</div>
</div>
</form>
DEMO
PS : close input tags as <input class="" />
Final edit here, I guess external bootstrap 3.1.1 was not getting included thus you got the result in 3 different lines, take a look at this
Final Demo
<link rel="stylesheet" type="text/css" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<div class="container">
<form class="form-inline" role="form" id="gh_save">
<div class="form-group">
<div class="col-xs-4">
<input type="text" placeholder="Github username" class="form-control" id="gh_user"/>
</div>
</div>
<div class="form-group">
<div class="col-xs-6">
<input type="text" placeholder="API Key" class="form-control" id="gh_apikey"/>
</div>
</div>
<div class="form-group">
<div class="col-xs-2">
<button type="submit" class="btn btn-success"> Save </button>
</div>
</div>
</form>
</div>
You should be able to target your ID within your css and set your desired width like so:
#gh_apikey{width:400px;}

make a panel form in the center of the page

so I was trying to create a form panel with Twitter Bootstrap into the center of the page, and hopefully even when the browser is resizing, it still be at the center,
here's what I tried on jsfiddle, the problem is I can't seem to make it center, anyone had ideas? the full code is in below
<div class="container">
<div class="row">
<div class="panel panel-default">
<div class="panel-body">
<legend>User Login</legend>
<form role="form">
<div class="form-group">
<label for="inputEmail">
Username</label>
<input id="inputEmail" type="text" class="form-control" placeholder="ex: John.Doe"
required="" />
</div>
<div class="form-group">
<label for="inputPassword">
Password</label>
<input id="inputPassword" type="password" class="form-control" placeholder="*******"
required="" />
</div>
<div class="checkbox">
<label>
<input id="chkRememberMe" type="checkbox" />
Remember Me
</label>
</div>
<button id="btnSubmit" type="submit" class="btn navbar-custom">
Submit</button>
</form>
</div>
</div>
</div>
</div>
the preview:
Please replace
<div class="panel panel-default">
with this
<div class="panel panel-default" style="max-width:400px;margin-left:auto; margin-right:auto;">
You can also use columns to size and center your panel, for example:
<div class="container">
<div class="row">
<div class="col-md-offset-3 col-md-6">
<div class="panel panel-default">
<div class="panel-body">
<legend>User Login</legend>
<form role="form">
<div class="form-group">
<label for="inputEmail">Username</label>
<input id="inputEmail" type="text" class="form-control" placeholder="ex: John.Doe" required="" />
</div>
<div class="form-group">
<label for="inputPassword">Password</label>
<input id="inputPassword" type="password" class="form-control" placeholder="*******" required="" />
</div>
<div class="checkbox">
<label>
<input id="chkRememberMe" type="checkbox" />
Remember Me
</label>
</div>
<button id="btnSubmit" type="submit" class="btn navbar-custom">Submit</button>
</form>
</div>
</div>
</div>
</div>
</div>
Try setting the margins in the div's style attribute:
style="margin-left:auto;margin-right:auto;"
Hope it works...

Resources