bootstrap form-group control-label missplaced - c#

I have a form-group where the label is missplaced, the label is too far down compared to the dropdownlist, see image.
This is my code:
<div class="form-group">
<div class="col-lg-2"></div>
#Html.LabelFor(model => model.deviceModel, "OS:", htmlAttributes:
new { #class = "col-lg-2 control-label" })
<div class="col-lg-4">
#Html.DropDownListFor(model => model.deviceModel, new SelectListItem[]{
new SelectListItem{Value = "1", Text="iOS"},
new SelectListItem{Value = "2", Text="Android"}},
new { htmlAttributes = new { #class = "form-control" } })
</div>
<div class="col-lg-2"></div>
</div>
What have i done wrong, why is it missplaced?

I am not into C# and Razor but based on my comment and looking at the code the newin front of the htmlAttributes for the Dropdownlist looks not right. It seems like the class is not applied to the Dropdownlist, which then could cause the shifting, probably caused by different margin parameters.
<div class="form-group">
<div class="col-lg-2"></div>
#Html.LabelFor(model => model.deviceModel, "OS:",
htmlAttributes: new { #class = "col-lg-2 control-label" })
<div class="col-lg-4">
#Html.DropDownListFor(model => model.deviceModel, new SelectListItem[]{
new SelectListItem{Value = "1", Text="iOS"},
new SelectListItem{Value = "2", Text="Android"}},
htmlAttributes: new { #class = "form-control" } )
</div>
<div class="col-lg-2"></div>

Related

DropDownList not showing SelectedValue in MVC Razor .net

I attempt to create the dropdown list in the View. The dropdown list for "CD_STATE_CLASS" "IN_DIVIDED" is working but for "TOLL_ROAD", it can return option values in the dropdown list, but the selected value defined in the function is not presented.When I log the TOLL_ROAD in the console, i can see the value but not in the dropdown list. I have no idea where the problem is because code looks identical. And also, when i save the form, it seems like selected TOLL_ROAD value in the dropdown list is not passing to the Road object.
private void GetEditRoadSelectLists(Road road)
{
var roadLookup = objMgr.GetRoadLookupDataBAL();
ViewBag.RoadTypes = roadLookup.RoadTypeLookup;
ViewData["CD_STATE_CLASS"] = new SelectList(roadLookup.StateClassLookup, "StateClassCode", "StateClassDescription", road.CD_STATE_CLASS);
ViewData["IN_DIVIDED"] = new SelectList(roadLookup.YesNoLookup, "YesNoShort", "YesNoLong", road.IN_DIVIDED);
ViewData["TOLL_ROAD"] = new SelectList(roadLookup.TollLookup, "Id","TOLL_ROAD", road.RoadsRamps.TOLL_ROAD);
//ViewData["TOLL_ROAD"] = new SelectList(roadLookup.TollLookup, "TOLL_ROAD", "TOLL_ROAD", road.RoadsRamps.TOLL_ROAD);
System.Diagnostics.Debug.WriteLine("toll"+road.RoadsRamps.TOLL_ROAD);
System.Diagnostics.Debug.WriteLine("YES" + road.IN_DIVIDED);
}
<div class="form-group row">
#Html.LabelFor(model => model.CD_STATE_CLASS, htmlAttributes: new { #class = "control-label col-md-2 required" })
<div class="col-md-4">
#Html.DropDownList("CD_STATE_CLASS", null, "-- Please select --", new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.CD_STATE_CLASS, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group row">
#Html.LabelFor(model => model.RoadsRamps.TOLL_ROAD, htmlAttributes: new { #class = "control-label col-md-2 required" })
<div class="col-md-4">
#Html.DropDownList("TOLL_ROAD", null, "-- Please select --", new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.RoadsRamps.TOLL_ROAD, "", new { #class = "text-danger" })
</div>
</div>

how to add the label in both right and left in asp.net text box

I try to add label in the right side as well of the text box to include the unit of the value.I would like to have like this
and This code gives like this.
this is my code
<div class="form-group ">
#Html.LabelFor(model => model.files, htmlAttributes: new { #class = "control-label col-md-3" })
<div class="col-md-8">
#{ Html.EnableClientValidation(false); }
#Html.EditorFor(model => model.files, new { htmlAttributes = new { #class = "form-control " } })<span>GB</span>
#{ Html.EnableClientValidation(true); }
#Html.ValidationMessageFor(model => model.files, "", new { #class = "text-danger" })
</div>
</div>
Thanks.
The class = "col-md-8" contains position:relative.
Since this line has class="form control " and your GB doesn't have a class. It goes to its natural position. Adding a class should tell it where to go.
#Html.EditorFor(model => model.files, new { htmlAttributes = new { #class = "form-control col-md-11" } })<span class="col-md-1">GB</span>

Form not posting when using unobtrusive validation

I've been trying to get this problem fixed for several hours now, but can't seem to find out what's wrong here.
I have a view with a few fields that I want to post to my controller. Now, I want to check for validation errors on the client side. I've used this in several projects before by adding:
- jquery.js (version 1.12.1)
- jquery.validate.js (version 1.14.0)
- jquery.validate.unobtrusive.js (version 3.2.3)
- jquery-unobtrusive-ajax.js (version 3.2.3)
As soon as a I reference jquery.validate.unobtrusive.js, the client sided validation works. But when I press the submit button, it just won't submit the form anymore. It just won't hit the controller.
My view is as following:
<div class="row">
<div class="col-md-12">
<div class="portlet light bordered">
<div class="portlet-title">
<div class="caption">
<span class="caption-subject bold uppercase font-dark">Auto toevoegen</span>
</div>
<div class="actions">
#Html.ActionLink("Terug", "Index", null, new { #class = "btn btn-circle green btn-outline btn-sm" })
</div>
</div>
<div class="portlet-body">
#using (Html.BeginForm("Create", "Car", FormMethod.Post, new { #class = "form-horizontal" }))
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
#Html.ValidationSummary(true)
#Html.HiddenFor(model => model.Id)
<div class="form-group">
#Html.LabelFor(model => model.Merk, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Merk, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Merk)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Type, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Type, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Type)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Kenteken, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Kenteken, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Kenteken)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Kilometervergoeding, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Kilometervergoeding, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Kilometervergoeding)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Name, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Name, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Name)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.AccountNumber, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.AccountNumber, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.AccountNumber)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.DefaultCar, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.DefaultCar)
#Html.ValidationMessageFor(model => model.DefaultCar)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Opslaan" class="btn blue" />
#Html.ActionLink("Annuleren", "Index", null, new { #class = "btn btn-white" })
</div>
</div>
</div>
}
</div>
</div>
</div>
My controller action looks like this:
[HttpPost]
[DisableValidation]
public ActionResult Create(CarViewModel model)
{
if(!ModelState.IsValid)
{
return View(model);
}
try
{
var tblCar = mapper.Map<CarViewModel, tblCar>(model);
_carAppService.Insert(tblCar);
return RedirectToAction("Index");
}
catch(Exception ex)
{
//Add proper error messages
Logger.Debug("Error occured during car adding process: " + ex.InnerException);
//add common container with errors
TempData["Error"] = "Er is iets fout gegaan :(";
return RedirectToAction("Index");
}
}
The scripts are loading in a bundle on startup and are loading in the order stated above. Now as soon as I remove the jquery.validate.unobtrusive from my bundle config, the submit button works. The client sided validation doesn't work anymore then though.
Does any know what's going on here? Thanks!
Alright I fixed the problem!
This answer will most likely only be useful is you make use of ASP.NET Boilerplate Zero template.
In ~/Common/Scripts a Javascript file was added (jquery-validation-custom.js).
The content messed with the submit button. I replaced it with my own version of a custom validation handler for modals and now it works!

Reset model values for partial view

I have a view which contains two partial views. One to select a customer and one to create a new customer both shown within tabs on the parent page. If somebody selects a customer, and then continues with the data entry form, but then has to go back to create a new customer instead, then the data from the selected customer remains. I can replace the data by entering the new data, however, the important bit is that the customer id remains and therefore none of the other data is taken into account. Is there a way for me to reset the customer object on click of e.g. an edit button without losing data from the rest of the page?
My parent view (relevant section) is:
<div class="form-group">
#Html.LabelFor(model => model.CommunicationModel.Communication.Customer.Name, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-12">
#if (Model.CommunicationModel.Communication.Customer.Name == null)
{
<input id="btnCust" type="button" value="Select" class="btn btn-default selectCustomer" />
}
else
{
<span id="custSpan" style="font-size:16px; font:bold;">
#Html.DisplayFor(model => model.CommunicationModel.Communication.Customer.Name)
#Html.HiddenFor(model => model.CommunicationModel.Communication.Customer.CustomerId)
<input id="btnEditCust" type="button" value="Edit" class="btn btn-default selectCustomer" />
</span>
}
<div id="customerTabs" style="display:none;">
<hr />
<p>
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active">Select Customer</li>
<li role="presentation">Create New Customer</li>
</ul>
</p>
<div class="tab-content">
<div id="tabs-1" role="tabpanel" class="tab-pane active">
#Html.Partial("~/Views/Communication/SelectCustomer.cshtml", Model.CustomerViewModel.AllCustomers)
</div>
<div id="tabs-2" role="tabpanel" class="tab-pane">
#Html.Partial("~/Views/Communication/CreateCustomer.cshtml", Model)
</div>
</div>
</div>
</div>
</div>
And my create customer partial view is:
#model CommunicationsLog.ViewModels.CommunicationViewModel
<div class="form-horizontal" id="addCustomerForm">
<div class="row">
<div class="col-md-12">
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.CommunicationModel.Communication.Customer.Name, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextBoxFor(model => model.CommunicationModel.Communication.Customer.Name, new { htmlAttributes = new { #class = "form-control" }, #id = "name" })
#Html.ValidationMessageFor(model => model.CommunicationModel.Communication.Customer.Name, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.CommunicationModel.Communication.Customer.ContactEmail, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextBoxFor(model => model.CommunicationModel.Communication.Customer.ContactEmail, new { htmlAttributes = new { #class = "form-control" }, #id = "email" })
#Html.ValidationMessageFor(model => model.CommunicationModel.Communication.Customer.ContactEmail, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.CommunicationModel.Communication.Customer.ContactTel, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextBoxFor(model => model.CommunicationModel.Communication.Customer.ContactTel, new { htmlAttributes = new { #class = "form-control" }, #id = "phone" })
#Html.ValidationMessageFor(model => model.CommunicationModel.Communication.Customer.ContactTel, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.CommunicationModel.Communication.Customer.CompanyName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextBoxFor(model => model.CommunicationModel.Communication.Customer.CompanyName, new { htmlAttributes = new { #class = "form-control" }, #id = "company" })
#Html.ValidationMessageFor(model => model.CommunicationModel.Communication.Customer.CompanyName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.CommunicationModel.Communication.Customer.Address, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextBoxFor(model => model.CommunicationModel.Communication.Customer.Address, new { htmlAttributes = new { #class = "form-control" }, #id = "address" })
#Html.ValidationMessageFor(model => model.CommunicationModel.Communication.Customer.Address, "", new { #class = "text-danger" })
</div>
</div>
</div>
</div>
Any help would be greatly appreciate. Im stumped :)
You're using Partials, meaning the html already been loaded and rendered when it reach the client.
To handle the case of Id is being sent, you should mark ..Customer.CustomerId has disabled="disabled" or readonly, this will avoid the Client side of submitting the Input tag, which will result of the Server side "think" (If you implemented it in that fashion) that this is a new customer.
You can easily achieve it using jQuery 1.6+:
$('#TheCustomerId').prop('disabled', true);
$('#TheCustomerId').prop('disabled', false);
Disable on < 1.6: Disable/enable an input with jQuery?

Button Horizontally inline with text boxes Razor View

I Have the following form on a view
#using (Html.BeginForm("AddInterest", "Club", FormMethod.Post))
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
<div class="row">
<div class="col-md-8">
<div class="well bs-component">
<div class="form-group">
<div class="col-md-10 col-md-offset-1">
<h3>Not listed? - Add extras here</h3>
</div>
</div>
#Html.HiddenFor(model => model.ClubTypeId)
#Html.HiddenFor(model => model.ClubId)
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
<div class="row col-md-offset-1 col-md-11">
<div class="col-md-4">
#Html.LabelFor(model => model.InterestName, htmlAttributes: new { #class = "control-label" })
#Html.EditorFor(model => model.InterestName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.InterestName, "", new { #class = "text-danger" })
</div>
<div class="col-md-5">
#Html.LabelFor(model => model.Description, htmlAttributes: new { #class = "control-label" })
#Html.EditorFor(model => model.Description, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Description, "", new { #class = "text-danger" })
</div>
<input type="submit" value="Add" class="btn btn- success" />
</div>
</div>
</div>
</div>
</div>
</div>
}
Everything is good apart from the submit button sits inline horizontally with the labels not the editor boxes, how can I get it to drop down?
i.e.
Label Label
Text Box Text Box Button
NOT
Label Label Button
Text Box Text Box
Its currently showing like this:
You should recreate your form following this pattern:
<div class="form-group">
#Html.LabelFor(model => model.InterestName, htmlAttributes: new { #class = "col-md-4 control-label" })
<div class="col-md-8">
#Html.EditorFor(model => model.InterestName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.InterestName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Description, htmlAttributes: new { #class = "col-md-4 control-label" })
<div class="col-md-8">
#Html.EditorFor(model => model.Description, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Description, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-4 col-md-8">
<input type="submit" value="Add" class="btn btn-success" />
</div>
</div>
More information at http://getbootstrap.com/css/#forms-horizontal
Edit
If you want them inline vertically:
<form class="form-inline">
<div class="form-group">
#Html.LabelFor(model => model.InterestName)
#Html.EditorFor(model => model.InterestName, new { htmlAttributes = new { #class = "form-control" } })
</div>
<div class="form-group">
#Html.LabelFor(model => model.Description)
#Html.EditorFor(model => model.Description, new { htmlAttributes = new { #class = "form-control" } })
</div>
<input type="submit" value="Add" class="btn btn-success" />
</form>
More information at http://getbootstrap.com/css/#forms-inline
Edit 2
If you want them inline horizontally but in two columns and submit button aligned to the right:
<form>
<div class="col-md-4">
<div class="form-group">
#Html.LabelFor(model => model.InterestName)
#Html.EditorFor(model => model.InterestName, new { htmlAttributes = new { #class = "form-control" } })
</div>
</div>
<div class="col-md-4">
<div class="form-group">
#Html.LabelFor(model => model.Description)
#Html.EditorFor(model => model.Description, new { htmlAttributes = new { #class = "form-control" } })
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<button type="submit" class="btn btn-default" style="margin-top: 25px;">Add</button>
</div>
</div>
</form>

Categories

Resources