In my Create view, there is a Date picker to select the date for the user.
Then the edit view, I want to pass the same date values to the edit view.
This is my view property looks like
#Html.EditorFor(model => model.Date_of_birth, new { htmlAttributes = new { #class = "form-control js-datepicker", Type = "Date" } })
It shows the date picker, but not shows the passed date value.
You should fill the model at action in that controller which you returning Edit view then #Html.EditorFor(model => model.Date_of_birth, new { htmlAttributes = new { #class = "form-control js-datepicker", Type = "Date" } })
will fill the data for you.
Related
I removed the time selection in the calendar widget and the start date in the fields also without time.
But the main problem is that when you select a date, the current time is still inserted in the field. Need only to display the date, without the time.
cshtml
<div class="form-group">
#Html.LabelFor(model => model.DateTo, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.DateTo, "{0:d}", new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.DateTo, "", new { #class = "text-danger" })
</div>
js
$("#DateTo").datetimepicker({
timepicker: false,
pickTime: false,
closeOnDateSelect: true,
dateFormat: "yy/mm/dd"
});
Model
[Display(Name = "DateTo", ResourceType = typeof(Resources.Resource))]
[DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}", ApplyFormatInEditMode = true)]
[Required]
public DateTime DateTo { get; set; }
jQuery DateTimePicker plugin v2.4.5
If there are simple alternative widgets for asp net, please advise.
To display only the date without the time, just do it this way:
$("#DateTo").datepicker({
dateFormat: "yy/mm/dd",
closeOnDateSelect: true
});
Because you're going through .datetimepicker where it picks up date and time, .datepicker picks up only thedate.
I am using the date picker in admin lte but no mater what I do the date time picker will show.
#Html.LabelFor(model => model.DateReported)
#Html.EditorFor(model => model.DateReported, new { htmlAttributes = new { #class = "form-control
datetimepicker" } })
#Html.ValidationMessageFor(model => model.DateReported)
You will see the generated html is as follows.
<input class="form-control datetimepicker text-box single-line valid" data-val="true" data-val-
required="The DateReported field is required." id="DateReported" name="DateReported" type="datetime-
local" value="0001-01-01T00:00:00.000" aria-required="true" aria-describedby="DateReported-error"
aria-invalid="false">
But even though I have in my scripts done this.
And I have tried this.
#Html.LabelFor(model => model.DateReported)
#Html.EditorFor(model => model.DateReported, new { htmlAttributes = new { #class = "form-control
dateicker" } })
#Html.ValidationMessageFor(model => model.DateReported)
<script type="text/javascript">
$(function () {
$('.DateRaised').datepicker();
$('.DateReported').datepicker();
});
Even if I set it as such Presume its because it gets datetime data type from my model how do I show only date part in textbox.
#Html.LabelFor(model => model.DateReported)
#Html.EditorFor(model => model.DateReported, new { htmlAttributes = new { #class = "form-control
datepicker" } })
#Html.ValidationMessageFor(model => model.DateReported)
But this still procduces this view.
have you tried fixing it from the Model ?
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime DateReported{ get; set; }
You should mention the date format on initializing datepicker in Jquery function:
$(function () {
$('.DateRaised').datepicker({
format: 'mm/dd/yyyy',
});
});
in .net MVC 5 with razor syntax I am trying to update the file but after click on update, the file name is not showing inside the textbox, for more clear picture please refer the image,
cshtml code is
<div class="form-group">
#Html.ReqLabelFor(model => model.file_path, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-5 offset-md-5">
#*#Html.TextBox("upload", null, new { type = "file", accept = "image/*", #class = "form-control" })*#
#Html.TextBox("file", null, new { type = "file", #class = "form-control" })
#Html.ValidationMessageFor(model => model.file_path, "", new { #class = "text-danger" })
</div>
</div>
And c# code is
File fileDownload = await db.File.Where(x => x.id == id).FirstOrDefaultAsync();
where file_path is of string type.
But actually It should come like below image
This question already has an answer here:
Submit same Partial View called multiple times data to controller?
(1 answer)
Closed 5 years ago.
I have a drop-down list (where multiple options can be selected) and two input boxes inside another div with id 'showInvestmentForm'.Upon selection of drop-down items,I want to produce those text boxes with respect to the number of drop-down items selected.
I want these text boxes to have different Names but I cant find a way to provide them different Names in such a way that their input values get passed to the controller.
This is a part of my Controller:-
foreach(var data in propertyViewModel.PropertyInvestors)
{
FounderInvestment founderInvestment = new FounderInvestment
{
Id = propertyViewModel.FounderInvestmentViewModel.Id ?? 0,
InstallmentPeriod = propertyViewModel.FounderInvestmentViewModel.InstallmentPeriod,
InvestorId = Convert.ToInt32(data),
PropertyId = property.Id,
Investment = propertyViewModel.FounderInvestmentViewModel.Investment
};
_founderInvestmentQueryProcessor.Create(founderInvestment);
}
This is my dropdown:-
<div class="form-group">
#Html.LabelFor(model => model.PropertyInvestors, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownListFor(model => model.PropertyInvestors, (IEnumerable<SelectListItem>)#ViewBag.Investors, "Select", htmlAttributes: new {#id="dropdown", #class = "form-control",#multiple="multiple" })
#Html.ValidationMessageFor(model => model.PropertyInvestors, "", new { #class = "text-danger" })
</div>
</div>
These are the textboxes:-
<div id="showInvestmentForm" style="display:none">
<div class="form-group">
#Html.LabelFor(model => model.FounderInvestmentViewModel.Investment, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.FounderInvestmentViewModel.Investment, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.FounderInvestmentViewModel.Investment, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.FounderInvestmentViewModel.InstallmentPeriod, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.FounderInvestmentViewModel.InstallmentPeriod, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.FounderInvestmentViewModel.Investment, "", new { #class = "text-danger" })
</div>
</div>
</div>
If I understood correctly your problem is that input texts are not posted to your Controller.
Would be usefult if you write the Controller's code to check the full scenario.
Anyway a common mistake, that could be your case, when using Mvc helpers is that when you point to a nested property, the generated name of the input is not the final property name but a path to that property.
#Html.EditorFor(model => model.FounderInvestmentViewModel.InstallmentPeriod, ..)
In your case that code produce an input with name FounderInvestmentViewModel_InstallmentPeriod, and not just InstallmentPeriod.
If in your controller you expect a parameter named "InstallmentPeriod" that would not be received, because the page posts a parameter named "FounderInvestmentViewModel_InstallmentPeriod"
I am following this example to use jQuery UI Datepicker in my MVC project.
Model
[DataType(DataType.Date)]
public DateTime RequestDate { get; set; }
Razor
<div class="form-group">
#Html.LabelFor(model => model.RequestDate, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.RequestDate)
#Html.ValidationMessageFor(model => model.RequestDate, "", new { #class = "text-danger" })
</div>
Then as per tutorial created Date helper and pasted this line
#Html.TextBox("", String.Format("{0:d}", (string)Model.ToShortDateString()), new { #class = "datefield", #type = "date" })
When i run the application, i get run time exception for above line. Model is null. I am not sure why? and How does it bind to my model?
Additional information: Cannot perform runtime binding on a null
reference
DatePickerReady.js
if (!Modernizr.inputtypes.date) {
$(function () {
$(".datefield").datepicker();
});
}
I think your problem is caused by the casting part of (string)Model.ToShortDateString(), no need for the casting here as the ToShortDateString will return a string anyways. Try this instead:
#Html.TextBox("", String.Format("{0:d}", Model.ToShortDateString()),
new { #class = "datefield", #type = "date" })
And you need to make sure to name your editor template a name matching your data type (DateTime.cshtml in your case) or use the UIHint attribute if you like to customize the name, and make sure you add your template under the proper EditorTemplates folder.