Textboxes are different sizes - asp.net-mvc-5

I created my controllers and views using scaffolded item but when I run my view in the program I can see that my textboxes are different sizes.How will I get it to all be the same size?
this is my view:
#model FCproject.Models.Purchase
#{
ViewBag.Title = "Edit2";
}
<h2>Edit2</h2>
#using (Html.BeginForm(new { OrderDate = Model.OrderDate }))
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Purchase</h4>
<hr />
#Html.ValidationSummary(true)
#Html.HiddenFor(model => model.PurchaseID)
<div class="form-group">
#Html.LabelFor(model => model.CustomerID, "Customer Cell", new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("CustomerID", String.Empty)
#Html.ValidationMessageFor(model => model.CustomerID)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.DeliveryChoice, "DeliveryChoice", new { #class = "control-label col-md-2" })
<div class="col-md-10">
<div>
<label>#Html.RadioButton("DeliveryChoice", true) Deliver my order to me</label>
</div>
<div>
<label>#Html.RadioButton("DeliveryChoice", false) I will pick up my order</label>
</div>
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.OrderDueDate, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.OrderDueDate)
#Html.ValidationMessageFor(model => model.OrderDueDate)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.OrderTime, "Order Due Time", new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.OrderTime)
#Html.ValidationMessageFor(model => model.OrderTime)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.OrderAdress1, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.OrderAdress1)
#Html.ValidationMessageFor(model => model.OrderAdress1)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.OrderAdress2, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.OrderAdress2)
#Html.ValidationMessageFor(model => model.OrderAdress2)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.OrderAdress3, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.OrderAdress3)
#Html.ValidationMessageFor(model => model.OrderAdress3)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
here is a link to a picture of the screen as you can see it looks very messy because the textbox sizes are different, the 'Order Due Date' textbox is too thick the 'CustomerCell' textbox is too small, the 'Order Due Time' is too thick and short:
Edit screen link

The ASP.NET MVC project template uses bootstrap see (http://getbootstrap.com/)
for styling the HTML. The CSS file should be located at "Content\bootstrap.css".
Note that the view includes the following classes to style the form
<div class="form-horizontal">
<div class="form-group">
the submit button is styled using
<input type="submit" value="Save" class="btn btn-default" />
and some of the of the HtmlHelper Methods (such as LabelFor) contain code for additional styles that are added to the HTML.
For example
new { #class = "control-label col-md-2" }
in
#Html.LabelFor(model => model.CustomerID, "Customer Cell", new { #class = "control-label col-md-2" })
if you want to use bootstrap to style your form's controls, then you need to add the appropriate bootstrap CSS classes to the elements in the view.
Below are some examples (not related to your code) to style various elements with bootstrap.
To style a Drop-down list add,
Html.DropDownList("movieGenre", (SelectList)ViewBag.movieGenre, "All", new { #class = "form-control" })
this results in the following HTML with the class form-control:
<select class="form-control" id="movieGenre" name="movieGenre">
<option value="">All</option>
<option>Comedy</option>
<option>Western</option>
</select>
to style a textbox add
#Html.EditorFor(model => model.Genre, new { htmlAttributes = new { #class = "form-control" } })
to get output similar to
<input class="form-control text-box single-line" id="Genre" name="Genre" type="text" value="Comedy" />
you might also add the text-danger bootstrap CSS class to make the validation warning text red (or the color it's defined to be in the CSS file) like this:
#Html.ValidationMessageFor(model => model.Title, "", new { #class = "text-danger" })
To learn about bootstrap classes for form controls take a look at bootstrap's documentation.
To figure out where to place the new { #class = "form-control" } line in the HtmlHelpers take a look at its documentation (https://msdn.microsoft.com/en-us/library/system.web.mvc.htmlhelper_methods(v=vs.118).aspx)
You can also add your own CSS file after bootstrap's CSS and style the elements (including width) by yourself, but if you use bootstrap this is not recommended.
Once you style the form elements with bootstrap, their width should be the same
(go to bootstrap's website and click on CSS then Forms for more information).

Related

MVC5 disable textbox based on another textbox value

I have a mvc5 project with ability to create;edit and delete records.
There are 3 textboxes.
Based on value in textBox1, I want to disable TextBox3 and 4;
if frequency.Value='Weekly'
{
Disable TextBox_BiWeekly
Enable TextBox3_Weekly
}
if frequency.Value='BiWeekly'
{
Disable TextBox3_Weekly
Enable TextBox4_BiWeekly
}
This is part of code in Edit.cshtml
<div class="form-group">
#Html.LabelFor(model => model.frequency, htmlAttributes: new { #class = ".col-form-label col-md-2" })
<div class="col-md-10">
<div class="form-check">
#Html.EditorFor(model =>model.frequency)
#Html.ValidationMessageFor(model =>model.frequency, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Weekly, htmlAttributes: new { #class = ".col-form-label col-md-2" })
<div class="col-md-10">
<div class="form-check">
#Html.EditorFor(model =>model.Weekly)
#Html.ValidationMessageFor(model =>model.Weekly, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.BiWeekly, htmlAttributes: new { #class = ".col-form-label col-md-2" })
<div class="col-md-10">
<div class="form-check">
#Html.EditorFor(model =>model.BiWeekly)
#Html.ValidationMessageFor(model =>model.BiWeekly, "", new { #class = "text-danger" })
</div>
</div>
</div>
I am not sure how to disable or enable Textboxes
Thanks MR
Probably the best way to do this would be in JavaScript so that the textboxes can enable/disable on the fly based on the value of your frequency input.
Something like:
$(function() {
EnableDisableFrequency();
$('#Frequency').change(function() {
EnableDisableFrequency();
});
});
function EnableDisableFrequency() {
if ($('#Frequency').val() === "Weekly") {
$('#BiWeekly').prop('disabled', true);
$('#Weekly').prop('disabled', false);
} else if ($('#Frequency').val() === "BiWeekly") {
$('#BiWeekly').prop('disabled', false);
$('#Weekly').prop('disabled', true);
}
}
My JavaScript is a bit rusty but I'm pretty sure that should work.
I would however agree with Stephen Muecke that your Frequency input should be a dropdownlist.
#Html.DropDownListFor(m => m.Frequency, Model.FrequencyList)
Just define FrequencyList in your ViewModel as a List that contains all potential values which I assume is just Weekly and BiWeekly
FrequencyList = new List<SelectListItem>
{
new SelectListItem
{
Text = "Weekly",
Value = "Weekly"
},
new SelectListItem
{
Text = "BiWeekly",
Value = "BiWeekly"
}
};
The JavaScript can remain the same.

How to get UserName in a label in my Create.html?

I have an asp.net core 2.0 site using VS 2017 (ver. 15.4) & C#. I have a 'Comments' model that has been scaffoled. A user has to be logged in to create a new comment. In 'Create.cshtml' I want the 'Date' and the 'UserName' to be filled in a label- not an input. Just trying to keep a user from using a false name to create a comment. I need to use code in 'Create.html' to accomplish this. Here is what I have in 'Create.html':
Date (this works):
<div class="form-group">
#Html.LabelFor(model => model.Date, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.LabelFor(model => model.Date, DateTime.Now.ToShortDateString())
#Html.ValidationMessageFor(model => model.Date, "", new { #class = "text-danger" })
</div>
</div>
UserName (this does NOT work) :
<div class="form-group">
#Html.LabelFor(model => model.UserName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.LabelFor(model => model.UserName, new { htmlAttributes = new { #class = "control-label", #Value = User.Identity.Name } })
#Html.ValidationMessageFor(model => model.UserName, "", new { #class = "text-danger" })
</div>
</div>
I'm trying to get the UserName filled in using a label. Thanks for any suggestions. Jeff
Well couple problems on your approach:
There is no value attribute on a label element
Since you have the ViewModel for the Create.cshtml view, and you have a UserName property, there is no point to do LabelFor or TextBoxFor with no value and initialize the element value on the Razor view. You could have done so when initializing the ViewModel on the server side.
Fix
You can initialize the model property UserName with the value of User.Identity.Name on the server side, and remove the #value = User.Identity.Name on the view.
My 2 cents
If you just want to display the UserName and not expect it comes back as input, you don't have to put #Html.ValidationMessageFor() there. You are not getting a value when doing post back anyway.
And for the same reason, you can just use a regular <label> element with Razor 1 way binding to display the value, i.e.,
<div class="col-md-10">
<label class="control-label col-md-2">#Model.UserName</label>
</div>
I see you're using Bootstrap. There is a class from Bootstrap 4 called form-control-plaintext that will style an input element and make it look like a label, so that you can actually bind the value to an input as well.
<!-- Throwing in Tag Helper just for fun -->
<div class="form-group row">
<label asp-for="UserName" class="col-form-label col-md-2"></label>
<div class="col-md-10">
<input type="text" class="form-control-plaintext" asp-for="UserName"
readonly />
</div>
</div>

My datetime picker from my model is not working

The dateTime picker is not showing in the view for me to select a date for my ReturnDate.It wont give me the little arrow to select from a calendar (datetime picker) and it already fills in values. It just shows a textbox that contains : '0001/01/01 12:00:00 AM'
in my model this is my Returndate field:
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? ReturnedDate { get; set; }
my View is:
<div class="form-group">
#Html.LabelFor(model => model.ReturnedDate, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ReturnedDate)
#Html.ValidationMessageFor(model => model.ReturnedDate)
</div>
</div>
using jquery ui this is my new view:
#model FCproject.Models.Purchase
#{
ViewBag.Title = "Create";
}
<script src="~/Scripts/jquery-ui-1.11.4.min.js"></script>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<h4>Purchase</h4>
<hr />
#Html.ValidationSummary(true)
<div class="form-group">
#Html.LabelFor(model => model.ReturnedDate, new { #class = "control-label col-md-2", #id = "datepicker" })
<div class="col-md-10">
#Html.EditorFor(model => model.ReturnedDate)
#Html.ValidationMessageFor(model => model.ReturnedDate)
</div>
</div>
<script>
$(function () {
$("#datepicker").datepicker();
});
</script>
}
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
But it still shows a textbox with no datetime picker.
if you are running on IE that feature doesn't work, chrome works the best on ui's.
But if still gives you problem i'll suggest you use jquery datepicker:
your View (Modified):
<div class="form-group">
#Html.LabelFor(model => model.ReturnedDate, new { #class ="control-label col-md-2", #id = "datepicker" })
<div class="col-md-10">
#Html.EditorFor(model => model.ReturnedDate)
#Html.ValidationMessageFor(model => model.ReturnedDate)
</div>
</div>
<script>
$(function() {
$( "#datepicker" ).datepicker();
});
</script>
OR visit
http://api.jqueryui.com/datepicker/
Dont forget to reference your ui scripts e.g: (Depending on your script version)
<script src="~/Scripts/jquery-ui-1.11.4.min.js"></script>

EditorFor is not naming the created fields correctly when I cast the model field

I have a viewmodel that contains a Transaction which has a field called PaymentMethod. PaymentMethod points to an abstract class PaymentMethodInfo that will be one of two types, CheckPaymentMethodInfo or MoneyOrderPaymentMethodInfo. When I create the form I check which type it is and render the correct fields. The code looks like this:
#if (Model.Transaction.PaymentMethod.FundingMethod == FundingMethod.MoneyOrder) {
<div id="money_order_payment">
<div class="form-group">
#Html.LabelFor(model => ((MoneyOrderPaymentMethodInfo) model.Transaction.PaymentMethod).Originator,
new {#class = "control-label col-md-2"})
<div class="col-md-10">
#Html.EditorFor(model => ((MoneyOrderPaymentMethodInfo) model.Transaction.PaymentMethod).Originator,
new {htmlAttributes = new {#class = "form-control"}})
#Html.ValidationMessageFor(model => ((MoneyOrderPaymentMethodInfo) model.Transaction.PaymentMethod).Originator, "",
new {#class = "text-danger"})
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => ((MoneyOrderPaymentMethodInfo) model.Transaction.PaymentMethod).SerialNumber,
new {#class = "control-label col-md-2"})
<div class="col-md-10">
#Html.EditorFor(model => ((MoneyOrderPaymentMethodInfo) model.Transaction.PaymentMethod).SerialNumber,
new {htmlAttributes = new {#class = "form-control"}})
#Html.ValidationMessageFor(model => ((MoneyOrderPaymentMethodInfo) model.Transaction.PaymentMethod).SerialNumber, "",
new {#class = "text-danger"})
</div>
</div>
</div>
} else {
<div id="check_payment">
<div class="form-group">
#Html.LabelFor(model => ((CheckPaymentMethodInfo) model.Transaction.PaymentMethod).BankName,
new {#class = "control-label col-md-2"})
<div class="col-md-10">
#Html.EditorFor(model => ((CheckPaymentMethodInfo) model.Transaction.PaymentMethod).BankName,
new {htmlAttributes = new {#class = "form-control"}})
#Html.ValidationMessageFor(model => ((CheckPaymentMethodInfo) model.Transaction.PaymentMethod).BankName, "",
new {#class = "text-danger"})
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => ((CheckPaymentMethodInfo) model.Transaction.PaymentMethod).RoutingNumber,
new {#class = "control-label col-md-2"})
<div class="col-md-10">
#Html.EditorFor(model => ((CheckPaymentMethodInfo) model.Transaction.PaymentMethod).RoutingNumber,
new {htmlAttributes = new {#class = "form-control"}})
#Html.ValidationMessageFor(model => ((CheckPaymentMethodInfo) model.Transaction.PaymentMethod).RoutingNumber, "",
new {#class = "text-danger"})
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => ((CheckPaymentMethodInfo) model.Transaction.PaymentMethod).AccountNumber,
new {#class = "control-label col-md-2"})
<div class="col-md-10">
#Html.EditorFor(model => ((CheckPaymentMethodInfo) model.Transaction.PaymentMethod).AccountNumber,
new {htmlAttributes = new {#class = "form-control"}})
#Html.ValidationMessageFor(model => ((CheckPaymentMethodInfo) model.Transaction.PaymentMethod).AccountNumber, "",
new {#class = "text-danger"})
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => ((CheckPaymentMethodInfo) model.Transaction.PaymentMethod).CheckNumber,
new {#class = "control-label col-md-2"})
<div class="col-md-10">
#Html.EditorFor(model => ((CheckPaymentMethodInfo) model.Transaction.PaymentMethod).CheckNumber,
new {htmlAttributes = new {#class = "form-control"}})
#Html.ValidationMessageFor(model => ((CheckPaymentMethodInfo) model.Transaction.PaymentMethod).CheckNumber, "",
new {#class = "text-danger"})
</div>
</div>
</div>
}
When the form is rendered however the fields are named incorrectly and the submitted form contains null for the PaymentMethod.
<div id="check_payment">
<div class="form-group">
<label class="control-label col-md-2" for="BankName">BankName</label>
<div class="col-md-10">
<input class="form-control text-box single-line" id="BankName" name="BankName" type="text" value="" />
<span class="field-validation-valid text-danger" data-valmsg-for="BankName" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="RoutingNumber">RoutingNumber</label>
<div class="col-md-10">
<input class="form-control text-box single-line" id="RoutingNumber" name="RoutingNumber" type="text" value="" />
<span class="field-validation-valid text-danger" data-valmsg-for="RoutingNumber" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="AccountNumber">AccountNumber</label>
<div class="col-md-10">
<input class="form-control text-box single-line" id="AccountNumber" name="AccountNumber" type="text" value="" />
<span class="field-validation-valid text-danger" data-valmsg-for="AccountNumber" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="CheckNumber">CheckNumber</label>
<div class="col-md-10">
<input class="form-control text-box single-line" id="CheckNumber" name="CheckNumber" type="text" value="" />
<span class="field-validation-valid text-danger" data-valmsg-for="CheckNumber" data-valmsg-replace="true"></span>
</div>
</div>
</div>

Post to an external form from a controller in mvc5

I have a form in a mvc5 view with a button. I need to process this form in the controller and add a few more field values which is picked up from the controller and then posted to an external url.
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Deal</h4>
<hr />
#Html.ValidationSummary(true)
<div class="form-group">
#Html.LabelFor(model => model.First_Name, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextBoxFor(model => model.First_Name, new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.First_Name)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Last_Name, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextBoxFor(model => model.Last_Name, new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Last_Name)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" name="save" />
<input type="submit" value="Register Deal" class="btn btn-default" name="submit" />
</div>
</div>
Controller
public ActionResult Create([Bind(Include = "Id,Name,Company,Telephone,Fax,Email,Title,Status,OpportunityAmount,First_Name,Last_Name,City,State,Country,Zip")] Deal deal, String submit)
{
if (ModelState.IsValid)
{
// do some processing and submit to another external form
}
}
Any thoughts on how we can accomplish this ?
One use case would be
if an username is provided the user then i would need to query from database the first last name, age etc and submit it to registration form of another site
You can post using Web Request Method. E.g.
public void post()
{
string URL = "http://";
System.Net.WebRequest webRequest = System.Net.WebRequest.Create(URL);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
Stream reqStream = webRequest.GetRequestStream();
string postData = Request.QueryString; //you form data in get format
byte[] postArray = Encoding.ASCII.GetBytes(postData);
reqStream.Write(postArray, 0, postArray.Length);
reqStream.Close();
StreamReader sr = new StreamReader(webRequest.GetResponse().GetResponseStream());
string Result = sr.ReadToEnd();
}

Resources