How to convert the DateTime property,
<Label Text="{Binding PaymentDate}"></Label>
<Label Text="{Binding PaymentDate, StringFormat=D}"></Label>
Please let me know what string format to use. The above logic didn't work.
<Label Text="{Binding PaymentDate, StringFormat=D}"></Label>
You have a couple of option:
Option 1:
You do a String format in the XAML:
<Label Text="{Binding ActualDate, StringFormat='{0:MMMM d, yyyy HH:mm}'}" Font="15,Bold" />
Option 2:
In you model have a string property e.g. DateFormatted which is a GET property that returns the string formatted representation of your Actual Date property:
public string DateFormatted { get { return ActualDate.ToLongDateString(); } }
Related
I'm new to razor and .net, i have a page on an ASP Net Razor project where i popup a modal with a form inside. Inside the form there is a datepicker and i want to set the system date when the page loads and evenly keep it editable.
i've already tried with
#{ string dateNow = DateTime.Now.ToString("dd MM yyyy"); }
<input asp-for="intervento.Data" class="form-control" type="date" value="#dateNow" />
it retrieves the correct date but can't display it. how can i solve this?
Your date should be in the following format yyyy-MM-dd.
So it would look like this:
<input type="date" value="2020-10-23">
Presumably, Intervento.Data is a DateTime property of your PageModel? If so, you should add a DataType attribute to specify that it represents a date, not a time:
[DataType(DataType.Date)]
public DateTime Data { get; set; }
Then you don't need to specify the type on the input. The tag helper will generate the correct value based on the attribute. Also, if you set a default value on the property:
[DataType(DataType.Date)]
public DateTime Data { get; set; } = DateTime.Now;
you won't need to set the value of the input either. ASP.NET will take care of formatting the value correctly to an ISO 8601 format that the input can work with. So the following will suffice:
<input asp-for="intervento.Data" class="form-control" />
I have a DateTime property in my model decorated as
[Display(Name = "Valid From")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy}", ApplyFormatInEditMode = true)]
public DateTime ValidFrom { get; set; }
When I am using it in html's table's td its format is rendering correctly. This is how I am using it when it renders correctly - #Html.DisplayFor(modelItem => item.ValidFrom)
Its not working in edit/create mode. This is how it created in view by default by scaffolding
<input asp-for="ValidFrom" class="form-control" />
When edit/create form loads it has
Tried by specifying explicit date format but not working
<input asp-for="ValidFrom" class="form-control" data-date-format="dd-MMM-yyyy" />
This is how it renders on the form. Can you please guide what actually missing. Also, how can I set date format of date picker control to dd-MMM-yyyy?
<input asp-for="ValidFrom" class="form-control" />
By using above code, it will generate a <input type= "date" /> element, by default the date format is "mm/dd/yyyy". To change the date format, you could try to change the date type to text type, like this:
<input asp-for="ValidFrom" class="form-control" data-date-format="dd-MMM-yyyy" type="text" placeholder="dd-MMM-yyyy" />
Then, the output as below:
Then, when you change the date, you could use Bootstrap-datepicker or JQuery UI datepicker to select date and change the date format.
[Note] If you are using both Bootstrap and JQuery UI, you might meet the JQuery conflict error, here is a similar thread, you could check it.
It looks like you are not specifying the type on your input
Your date format looks incorrect
I have found issues using asp-for with DateTime model properties (although these may work with string values)
Here is what I have been using in MVC Core 3. Hopefully this will be of help
<input type="date" value="#Model.DateTo.ToHtmlInputDate()" asp-for="DateTo" min="#Model.EarliestDate.ToHtmlInputDate()" max="#Model.LatestDate.ToHtmlInputDate()" onchange="$('form').submit();" class="form-control">
Here is my static helper to extend DateTime
public static string ToHtmlInputDate(this DateTime d)
{
return d.ToString("yyyy-MM-dd");
}
By setting false to the DisplayFormat attribute, I could have it to work in .net 6 and .net 5 MVC
[DataType(DataType.Date)]
[Display(Name = "Date saisine")]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = false)]
public DateTime? Birth {get; set;}
How I send an static value inside ConverterParameter such as a number?
<Label Style="{DynamicResource ListItemDetailTextStyle}" Text="{Binding Information, Converter={StaticResource CutStringConverter}, ConverterParameter={100}}" />
You probably need to include the type then. So either do it inline like this: ConverterParameter={x:Int32 100}.
Or write it more verbose:
<Label>
<Label.Text>
<Binding Path="Information" Converter="{StaticResource CutStringConverter}">
<Binding.ConverterParameter>
<x:Int32>100</x:Int32>
</Binding.ConverterParameter>
</Binding>
</Label.Text>
</Label>
Or, to be complete, add a static resource to your page (or whatever the container is), like:
<ContentPage.Resources>
<x:Int32 x:Key="IntHundred">100</x:Int32>
</ContentPage.Resources>
And reference that: ConverterParameter={StaticResource IntHundred}
I am not new to MVC so I am a bit baffled at why I can't change the URL of my POST when I click the submit button.
I have this simple view called PandoraRemovalTool.cshtml
#{
ViewBag.Title = "PandoraRemovalTool";
}
#using (Html.BeginForm("PandoraGetDocsList"))
{
<h2>Pandora Removal Tool</h2>
#Html.Label("Member number:")
#Html.TextBox("txtMemberNumber")
<br />
<input type="submit" value="Search"/>
}
Because of it's simplicity I am not using a model, I only want to POST the txt value. However the URL is a bit odd. It's pointing to this path in the site:
<form action="/Administration/PandoraRemovalTool?Length=18" method="post" novalidate="novalidate">
<h2>Pandora Removal Tool</h2>
<label for="Member_number:">Member number:</label>
<input id="txtMemberNumber" name="txtMemberNumber" type="text" value=""/>
<br>
<input type="submit" value="Search"/>
</form>
I don't understand where it's getting length=18 from. I want to post to this method:
[HttpPost]
public ActionResult PandoraGetDocsList(string txtMemberNumber)
{
return RedirectToAction("PandoraRemovalTotal2", new {MemberNum = txtMemberNumber });
}
public ActionResult PandoraRemovalTotal2(string MemberNum)
{
return View();
}
Some help please?
Replace #using (Html.BeginForm("PandoraGetDocsList")) to #using (Html.BeginForm())
PandoraGetDocsList is the string of length 18 which you are getting in your post
If you want to redirect it to the action PandoraGetDocsList then do like this:
#using (Html.BeginForm("PandoraGetDocsList", "Administration", new { txtMemberNumber = someString }))
Explanation:
Html.BeginForm does not accept a parameter as string.
There is no overload that accepts just a string as parameter. It's using the BeginForm(this HtmlHelper htmlHelper, Object routeValues) overload and because of that, it attempts to serialize your string which is passed as an object.
In the case of a string object, the only public property is Length, and since there will be no routes defined with a Length parameter it appends the property name and value as a query string parameter.
The overload you're looking for is BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName).
#using (Html.BeginForm("PandoraGetDocsList", "controller name here"))
Use the overload.
#using (Html.BeginForm("PandoraGetDocsList", null))
I'm trying to use a Checkboxlist in MonoRail to represent a many to many table relationship. There is a Special table, SpecialTag table, and then a SpecialTagging table which is the many to many mapping table between Special and SpecialTag.
Here is an excerpt from the Special model class:
[HasAndBelongsToMany(typeof(SpecialTag),
Table = "SpecialTagging", ColumnKey = "SpecialId", ColumnRef = "SpecialTagId")]
public IList<SpecialTag> Tags { get; set; }
And then in my add/edit special view:
$Form.LabelFor("special.Tags", "Tags")<br/>
#set($items = $FormHelper.CreateCheckboxList("special.Tags", $specialTags))
#foreach($specialTag in $items)
$items.Item("$specialTag.Id") $Form.LabelFor("$specialTag.Id", $specialTag.Name)
#end
The checkboxlist renders correctly, but if I select some and then click Save, it doesn't save the special/tag associations to the SpecialTagging table (the entity passed to the Save controller action has an empty Tags list.) One thing I noticed was that the name and value attributes on the checkboxes are funky:
<label for="special_Tags">Tags</label><br>
<input id="3" name="special.Tags[0]" value="UCampus.Core.Models.SpecialTag" type="checkbox"> <label for="3">Buy 1 Get 1 Free</label>
<input id="1" name="special.Tags[1]" value="UCampus.Core.Models.SpecialTag" type="checkbox"> <label for="1">Free</label>
<input id="2" name="special.Tags[2]" value="UCampus.Core.Models.SpecialTag" type="checkbox"> <label for="2">Half Price</label>
<input id="5" name="special.Tags[3]" value="UCampus.Core.Models.SpecialTag" type="checkbox"> <label for="5">Live Music</label>
<input id="4" name="special.Tags[4]" value="UCampus.Core.Models.SpecialTag" type="checkbox"> <label for="4">Outdoor Seating</label>
Anyone have any ideas?
Thanks!
Justin
The checkboxlist renders correctly
it seems to me that you could also render something like
<input type="checkbox" name="special.Tags" value="1"/>
<input type="checkbox" name="special.Tags" value="2"/>
which make it simpler (no index to output for the name, it will be properly resolved as an array via controller action parameter binding
also, in your sample, the fact that all checkboxes having the same value UCampus.Core.Models.SpecialTag is probably not right, you may want to output actual primary key identifier from the tags (not sure, could you display the class you are binding back on the form handling action?)
I was able to get it working by specifying the id and text attributes...
$Form.LabelFor("special.Tags", "Tags")<br/>
#set($items = $FormHelper.CreateCheckboxList("special.Tags", $specialTags, "%{value='Id', text='Name'}"))
#foreach($specialTag in $items)
$items.Item("$specialTag.Id") $Form.LabelFor("$specialTag.Id", $specialTag.Name)
#end