MVC project: Convert user input date mmddyyyy to yyyy/mm/dd - c#

I have a textbox in a MVC project that prompts a user to enter a date as mmddyyyy. I have the code set up so that the user can only input numbers (ie, no "/"s or "-"s). I need to then convert this data to yyyy-mm-dd to ensure that correct data is being added to the database once the form is submitted.
I realize I will probably need to use DateTime.Parse to do this, but I can't for the life of me figure out how to add it to the following code:
<div class="form-group">
#Html.LabelFor(model => model.Buy2IDExpireDate, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextBoxFor(model => model.Buy2IDExpireDate, new { id = "coBuyerIDExpireDate", #class = "form-control" })
#Html.ValidationMessageFor(model => model.Buy2IDExpireDate, "", new { #class = "text-danger" })
</div>
</div>
Would it look something like this?
string str = Date.Parse(Buy2IDExpireDate).ToString("yyyy-MM-dd");
If so where do I put it in the above code, and do I need to write extra code to ensure that the newly formatted date is stored by the database?

You can add value attribute like this:
#Html.TextBoxFor(model => model.Buy2IDExpireDate,
new { #Value = Model.Buy2IDExpireDate.ToString("yyyy-MM-dd") })

Related

How do I properly bind the html to the model data

So I am developing my first webpage using ASP.NET MVC and I managed to create a fully working registration page which send the data to the database and stored the user. Simple.
However I didn't really like the look and feel of the element it created for me so I thought I could change it out.
Original code WORKING
<div class="form-group">
#Html.LabelFor(model => model.Firstname, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Firstname, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Firstname, "", new { #class = "text-danger" })
</div>
</div>
My new code NOT WORKING
<div class="row">
<div class="input-group bb-none">
<i class="fas fa-address-card"></i>
<input type="text" placeholder="Firstname">
</div>
</div>
I am 99.9% sure that it's a binding issue. I want the data I put into my new textbox
<input type="text" placeholder="Firstname">
To carry over the data to the model.
What's the part that binds it in the first option?
Tag helpers will resolve down to html and put the property name as both the id and name within the input. The model binder then binds to that.
#Html.TextBoxFor( m => m.Firstname, new { placeholder = "Firstname" })

Setting focus on EditorFor [duplicate]

I would like to autofocus on an editorfor in my application, but I can't seem to do that. I have successfully used autofocus on a textbox, but I would like to use an editorfor to keep my application's look universal.
Any solutions to this would be much appreciated, thank you.
My attempt:
#Html.EditorFor(model => model.Description, new { htmlAttributes = new { #class = "form-control" }, autofocus = "" })
This s because you are using EditorFor instead of something specific like TextBoxFor.
#Html.TextBoxFor(model => model.Name, new { htmlAttributes = new {
#class = "form-control" }, autofocus="autofocus"})
Or you can do that using jQuery:
<div class="editor-field focus">
#Html.EditorFor(model => model.Description)
#Html.ValidationMessageFor(model => model.Description)
</div>
$(function() {
$('.focus :input').focus();
});
Update:
As you know TextBoxFor always creates a textbox with type input, But EditorFor is a little bit smart, it renders markup based on the datatype of the property.
Using .Net Framework 4.5 and MVC 5, this works for me:
#Html.EditorFor(model => model.Description, new {
htmlAttributes = new {
#class = "form-control",
autofocus = true
}
})
You put the autofocus attribute in the wrong spot.
#Html.EditorFor(model => model.Description,
new { htmlAttributes = new { #class = "form-control" }, autofocus = "" })
Try this instead:
#Html.EditorFor(model => model.Description,
new { htmlAttributes = new { #class = "form-control", autofocus = "" } })
I've been following this thread and I may have stumbled on an answer to your question about autofocus on EditorFor - this is all Asp.Net 4.5 and MVC 5, not that it matters.
In the Scripts folder I have a jQuery script file:
$(function(){
$('.someclassname').focus();
});
I add the script name to the BundleConfig and render it in the view.
In the view I add the classname to the EditorFor <div class="col-md-10" someclassname">
I then add the type="text" autofocus="autofocus" to the EditorFor's #class. So, new{#class="form-control", type="text", autofocus="autofocus"
That's pretty much it, when the DOM loads the .someclassname field gets the cursor focus...
PS. In fact if you just do (3) it works also...

ASP.NET MVC Passing URL id between views of different controllers

So I decided to make an auction house web application as my first asp.net mvc project and I cannot figure out how to pass a parameter between two views that belong to different controllers. In the first view, Details of AuctionHouseController, I have:
<a class="btn btn-default" href="#Url.Action("Create", "Auctions", new { id = Model.ItemId })">Start Auction ยป</a>
and a URL: http://localhost:2142/AuctionHouse/Details/123
And here is the Details method:
public ActionResult Details(int id)
{
var item = _auctionhDbc.Items.Find(id);
return View(item);
}
I want to pass the id part of the URL - the "123" to the view where the button leads - Create of AuctionsController, where I have:
<div class="form-group">
#Html.LabelFor(model => model.Item.ItemId, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Item.ItemId, new { htmlAttributes = new { #class = "form-control", #Value = " " } })
#Html.ValidationMessageFor(model => model.Item.ItemId, "", new { #class = "text-danger" })
</div>
</div>
I want to place the "123" as the default value (#Value) of the Html Editor field. How can I do that?
Assuming you are using strongly typed views, your model for the Create view will already have the value of 123 in ItemID. The problem is, your model is of type Items, yet you are trying to use EditorFor for model.Item.ItemID.
Thus, instead of your line
#Html.EditorFor(model => model.Item.ItemId,
new { htmlAttributes = new { #class = "form-control", #Value = " " } })
if you use
#Html.EditorFor(model => model.ItemId,
new { htmlAttributes = new { #class = "form-control" } })
you will already have passed the value there. Make sure you use strongly typed views by putting:
#model YourNameSpace.Items
in the beginning of your view.

MVC DisplayFormat Date + Time

I'm trying to display the date in this format: yyyy-MM-dd HH:mm, but it doesn't work (of course yyyy-MM-dd works fine, problem is with time)
MODEL:
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd HH:mm}", ApplyFormatInEditMode = true)]
public DateTime eventstart { get; set; }
VIEW:
<div class="form-group">
#Html.LabelFor(model => model.eventstart, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.eventstart, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.eventstart, "", new { #class = "text-danger" })
</div>
</div>
I'm assuming that you're generating an input with type="date". There's going to be two issues to be aware of with that:
As #JonSkeet pointed out, if you want to work with date and time, then you need to use DataType.DateTime. That will cause the input to be rendered as type="datetime". With an input of type "date", any time component will be discarded at best or at worst will not allow the value to parsed correctly for the browser date control, which brings us to:
The HTML5 datetime input types ("datetime", "date", "time") require the value to be in ISO format, i.e. YYYY-MM-DDTHH:MM:SSZ. The browser control will display the date/time in the user's local format based on parsing the ISO-formatted date, but it must be given the value in ISO format, first. If it's not, then it treats it as null, and will show the input value as empty.
Change:
#Html.EditorFor(model => model.eventstart,
new { htmlAttributes = new { #class = "form-control" } })
To:
#Html.EditorFor(model => model.eventstart,
new { htmlAttributes = new { #class = "form-control", #type="date" } })
Sure it will work.

How to Combine 2 textbox data in 1 textbox in MVC5

I have a MVC5 project, I have First Name and Last Name as 2 separate textboxes. I need to combine these 2 and shows as one textbox as Customer Name how I can do that?
This is what I have now that shows 2 text boxes:
<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{disabled = "disabled" })
#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{disabled = "disabled" })
#Html.ValidationMessageFor(model => model.Last_Name)
</div>
</div>
If the fields are truly combined then you'd add a property to your model representing the new single field:
public string CustomerName { get; set; }
and use it in your view:
#Html.TextBoxFor(model => model.CustomerName, new{disabled = "disabled" })
#Html.ValidationMessageFor(model => model.CustomerName)
(Though if, when saving back to the server, you need to parse the values back out into two separate fields then that can get tricky. Don't make too many assumptions about names. But if you must, then that parsing should likely happen in the setter for this property and the getter should dynamically display the concatenated values as below.)
If, on the other hand, it should be a read-only display of the combined values, you'd create a read-only property to view the other values:
public string CustomerName
{
get { return string.Format("{0} {1}", First_Name, Last_Name); }
}
and you can simply display it in the view:
#Html.DisplayFor(model => model.CustomerName)
or just bind directly to the value in your own markup:
<span>#Model.CustomerName</span>
(In this approach you might also write some JavaScript to update the client-side displayed value as the values in the other fields change.)
It really depends on what you want to do with this field, if it saves back to the model or is only for display purposes.

Categories