Here is my property in my model:
[Display(Name = "Date / Time:")]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy HH:mm}", ApplyFormatInEditMode = true)]
public Nullable<System.DateTime> DayTime { get; set; }
For my application it is necessary to include the time.
Here is my .cshtml
<div class="form-group">
#Html.LabelFor(model => model.DayTime, htmlAttributes: new { #class = "control-label col-md-2 required" })
<div class="col-md-10">
#Html.EditorFor(model => model.DayTime, new { htmlAttributes = new { #class = "form-control datepicker" } })
#Html.ValidationMessageFor(model => model.DayTime, "", new { #class = "text-danger" })
</div>
</div>
I am using jQuery Datepicker.
When the user initially goes to the Create page, I have it setup so that it will automatically have today's date and current time already in the EditorFor as so:
public ActionResult Create()
{
DailySummary daily = new DailySummary();
daily.DayTime = DateTime.Now;
return View(daily);
}
Now that all works fine, until I want to change the date and time... I am able to change the Date portion but I am unable to add the HH:mm part of the DateTime... like the EditorFor isn't allowing me to space from the Date portion to enter the time.
How do I fix this so that I can enter the time portion along with the date?
After searching around I found Bootstrap 3 Datepicker v4
I went into Nuget and downloaded the .CSS version of Bootstrap.v3.Datetimepicker.CSS.
After referencing the newly downloaded files in my _Layout View I followed the simple instructions in the link given above, and it works like a charm.
Related
I have 2 pages. A create page and an edit page. When I choose a date on the create page and submit it, it all goes right. But when I read the date out the database and set it in the input field, it won't display the date.
date field in create & edit page:
<div class="form-group">
#Html.LabelFor(model => model.activity.Date, htmlAttributes: new { #class = "control-label" })
#Html.EditorFor(model => model.activity.Date, new { htmlAttributes = new { #class = "form-control", onclick = "this.showPicker()", required = "required" } })
#Html.ValidationMessageFor(model => model.activity.Date, "", new { #class = "text-danger" })
</div>
[DataType(DataType.Date)]
public DateTime Date { get; set; }
model.Date = Convert.ToDateTime(dt.Rows[0][2]);
this what i get in edit page
what i want:
I have an asp.net mvc using entity framework web app. A specific table in the app has a text column we want to force users to select entries from a dropdown to avoid spelling issues. There are only two selections, Buy Item and Raw Components. We don't want to go through the trouble of creating an actual table and then relate that table with the original. When creating a new record or editing an existing record I would like to have the selected Text and not the selected ID saved in the record. The reason for this is there are many records in the table already AND a report deck associated with the table that would all have to be updated if the ID was saved back to the record. In the examples I've included below, you see I've used the ViewBag method although the Model method (for me) has the same issues.
To start I created a class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ProdMan4.Models
{
public class ItemTypes
{
public int Id { get; set; }
public string Title { get; set; }
}
}
Added the following to the controller for this table...
private List<ItemTypes> GetITs()
{
var itemtypes = new List<ItemTypes>();
itemtypes.Add(new ItemTypes() {Id = 1, Title = "Buy Item" });
itemtypes.Add(new ItemTypes() {Id = 2, Title = "Raw Components" });
return itemtypes;
}
public ActionResult Create()
{
ViewBag.ItemTypesSelectList = new SelectList(GetITs(),"Id", "Title");
return View();
}
Then Added the dropdown on the Create View as follows...
<div class="form-group">
#Html.LabelFor(model => model.ItemType, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("Title", ViewBag.ItemTypesSelectList as SelectList, "Select Type", new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.ItemType, "", new { #class = "text-danger" })
</div>
</div>
I have two issues.
I can create a new item but it saves the ID to the SQL table
The edit view, while setup just like the create view for this column, throws the following error.
System.InvalidOperationException: 'The ViewData item that has the key 'Id' is of type 'System.Int32' but must be of type 'IEnumerable'.'
At this point I'm a bit lost. Any direction would be appreciated.
Use Html.DropDownListFor like this
#Html.DropDownListFor(m=>m.ItemType, ViewBag.ItemTypesSelectList as SelectList,"--- select type --- ",new { #class = "form-control" })
In my application, to load some data to the view (combo boxes) I have been using TempData. I want to know if it is okay to use TempData for that purpose?
My current code is here; first I called data to a list in controller:
List<Request_Types> RequestTyleList = db.Request_Types.Where(r => r.Status == true).ToList();
List<SelectListItem> ReqTypeDropDown = RequestTyleList.Select(r => new SelectListItem { Text = r.Request_Type, Value = r.Id.ToString() }).ToList();
Then I am assigning this data to TempData:
TempData["RequestTyleList"] = ReqTypeDropDown;
In the view I called that temp data and assigning to the combo box
#{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
List<SelectListItem> ReqType = (List<SelectListItem>)TempData.Peek("RequestTyleList");
}
-----------------
<div class="form-group row">
#Html.LabelFor(model => model.ReqType, htmlAttributes: new { #class = "control-label col-md-3" })
<div class="col-sm-8">
#Html.DropDownListFor(model => model.ReqType, ReqTypes, "Select Request Type", new { #class = "js-dropdown" })
#Html.ValidationMessageFor(model => model.ReqType, "", new { #class = "text-danger" })
</div>
</div>
If I want to access those same data in Edit, I again create a list and putting data to the list and transfer to TempData and again call the same data from the view. Still I have 5 to 8 items of data on the list, I want to know when there are 100 items of data in TempData, will my system get slow? Are there any potential performance issues?
While surfing this on the internet, I got that same will do in the Sessions, but I don't know will it be suitable for this? Or else is there any good way to do this without dropping any performance of the system, like in one controller if I call and stores data, I can access those data from any view.
#smc developments, please use below Model and html markup to show dropdown list for users
public class User
{
public int SelectedUserId{get;set;}
public IEnumerable<SelectListItem> Users { get; set; }
}
#Html.DropDownListFor(x => Model.SelectedUserId, new SelectList(Model.Users, "Value", "Text"), htmlAttributes: new { #class = "form-control", id = "User"})
I am working on a tutorial from the Microsoft website with the end result like this image:
https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions/mvc-music-store/mvc-music-store-part-5/_static/image4.png
The price textbox in the View is:
<div class="form-group">
#Html.LabelFor(model => model.AlbumToEdit.Price, "Price", new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.AlbumToEdit.Price)
#Html.ValidationMessageFor(model => model.AlbumToEdit.Price)
</div>
</div>
Which renders as:
<input class="text-box single-line" id="AlbumToEdit_Price" name="AlbumToEdit.Price" type="text" value="9.99">
If I inspect the HTML via DevTools and add the below:
<input id="AlbumToEdit_Price" name="AlbumToEdit.Price" type="hidden" value="33.33">
And click the Save button to save the album details, the binding process is taking the 33.33 in consideration, regardless what I insert in the Price textbox, probably because both <input> tags share the same id and name.
How can I avoid this?
Controller code follows:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(AlbumToEditViewModel postedViewModel)
{
if (ModelState.IsValid)
{
db.Entry(postedViewModel.AlbumToEdit).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
AlbumToEditViewModel albumToEditViewModel = new AlbumToEditViewModel
{
AlbumToEdit = postedViewModel.AlbumToEdit,
Artists = new SelectList(db.Artists, "ArtistId", "Name", postedViewModel.AlbumToEdit.ArtistId),
Genres = new SelectList(db.Genres, "GenreId", "Name", postedViewModel.AlbumToEdit.GenreId)
};
return View(albumToEditViewModel);
}
I am trying to add a state field to the user registration on the default MVC SPA template. I want to use a dropdown that doesn't link to a database field, or anyother crazy stuff other than having them statically/manually created as they are not going to change.
Is there any easy to implement solution or a way that I can implement this easily without altering the defaults too much?
You sound lazy ;-). This should do the job:
Add this property to the RegisterViewModel in the AccountViewModel.cs:
[Display(Name = "State")]
public string State { get; set; }
This will take take of posting the state value. Then add this to the Register.cshtml, below the confirm password field:
#{ var states = new List<SelectListItem>
{
new SelectListItem { Text = "Example 1", Value="Example1" },
new SelectListItem { Text = "Example 2", Value="Example2" }
};
}
<div class="form-group">
#Html.LabelFor(m => m.State, new {#class = "col-md-2 control-label"})
<div class="col-md-10">
#Html.DropDownListFor(m => m.State, states, "-- Select Status --", new { #class = "form-control"})
</div>
</div>
This results in: