Set Current Date when binding to SQL Server - binding

I need to auto populate the date in a textbox while binding to a data base.
Note I know how to set a binding as well as how to set current date. What I do not know is how to set both with one textbox.
This allows me to bind and save to DB, but not display current date:
<asp:TextBox ID="txtDateWorked" runat="server" Text='<%# Bind("DateWorked","{0:d}")%>/>
This allows me to set date, but not bind:
<asp:TextBox ID="txtDateWorked" runat="server" Text='<%# DateTime.Now.ToString("MM/dd/yyyy") %> />
I need to do both and cannot find this answer.

In the databind you can set the column value using:
DateTime now = DateTime.Now;
(pass now to your DB here)
To display without saving to the DB, you can set the textbox value in the pageload.
DateTime now = DateTime.Now;
txtDateTime.Text = String.Format("{0:t}", Now);

Related

MVC6 select tag helper for edit applying current value

I have a viewmodel with a state dropdownlist.
The form is being populated from a database and I everything works except I dont know how to apply the current value for state as the default value for the select helper tag?
This is the CSHTML for the select:
<select asp-for="State" asp-items="#Model.StatesList" > </select>
This provides the all the states when you do a dropdown however is there a way to set the value to the current value for Model eg #Model.state so if you dont select any new dropdown value it takes the original value for the record?

how to set radio button for edit in grails

I am using grails 2.1.1 I have a domain where I want to save religion of member. I am saving it there is no problem. But when I go to edit page then it does not checked the original value.Suppose I am saving a religion for Buddhist and at the time of edit I want to be checked the value of it. But it is checking for Muslim. I want the field to be checked on edit page. Can anyone please help me on this please ??!!! Here are my attempts below ::
my domain class >>>
class RadioButton {
String religion
static constraints = {
}
}
my form page >>>
<div class="fieldcontain ${hasErrors(bean: radioButtonInstance, field: 'religion', 'error')} ">
<label for="religion">
<g:message code="radioButton.religion.label" default="Religion"/>
</label>
<g:radio name="religion" value="muslim" checked="checked"/> Muslim <br/>
<g:radio name="religion" value="hindu"/> Hindu<br/>
<g:radio name="religion" value="christian"/> Christian<br/>
<g:radio name="religion" value="buddhist"/> Buddhist
</div>
You may want to consider the radioGroup tag within Grails instead of manually authoring your radio buttons.
However, if you decide to continue manually authoring your radio buttons you will need to account for selecting the current value. For example:
<g:radio name="religion" value="muslim" checked="${radioButtonInstance?.religion.equals('muslim')}"/>
In the above example you will notice that the checked attribute is being set to a boolean value (which is correct according to the documentation).
I think radioGroup is by far a better solution for you as you are using grails.
They main problem is that you are not passing the currently set religion to the GSP. There is nothing telling the radio group which religion has already been set by the user, instead Muslim has been hard-coded with the checked="checked".
Judging from your first line which sets the class of the div if the bean has an error, I assume you can access the currently set religion from the radioButtonInstance. Using the radioGroup tag you pass the currently set value as ${radioButtonInstance?.religion}, then we set the values and the labels you need, as shown:
<g:radioGroup name="religion"
values="['Muslim', 'Hindu', 'Christian', 'Buddhist']"
labels="['Muslim', 'Hindu', 'Christian', 'Buddhist']"
value="${radioButtonInstance?.religion}">
<p>${it.label}: ${it.radio}</p>
</g:radioGroup>
I would, however, suggest that you set the available religions as an enum class rather than hard coding it onto the GSP, as you might want to reuse it. You could then pass it as a variable in the model through the controller, calling it, for example, religions, then your radioGroup would look like:
<g:radioGroup name="religion"
values="${religions}"
labels="${religions}"
value="${radioButtonInstance?.religion}">
<p>${it.label}: ${it.radio}</p>
</g:radioGroup>

bind database value in html input type textbox in mvc

i need to know that how to bind value in html text box in mvc from database.
i am using datepicker in that input type textbox.
<input type="text" name="objmember.BirthDate" data-beatpicker="true" class="span4" />
i am able to submit the value from this textbox in database field but while edit it i need to already show the date in this input type text box.on the other hand other textbox are showing the value fine.
#Html.TextBoxFor(m => m.objmember.Name, new { placeholder = "Name", #class = "span4" })
i am using viewModel in this view.but writing name="objmember.BirthDate" is not binding the value with this textbox.
above default textbox of mvc works fine with it.but on using input type text it is not binding the value. i have to use this datepicker in input type field because it is not working with #Html.Textbox.
Please give me some solution to this problem.

How do I generate a unique ID for a custom date picker model template?

I have over-ridden the default Date template using the following edit template:
#model DateTime
#Html.TextBox("", Model.ToString("d"),
new { #class = "datepicker" })
..which generates:
<input class="datepicker" id="FromDate" name="FromDate" type="text" value="08/05/2013">
I then attach a jquery datepicker to all items with the class of 'datepicker'.
The issue I have is that the id of FromDate sometimes conflicts with other inputs on the same page, especially when this form is displayed modally.
I'm wondering if there is a useful strategy to generating some sort of unique prefix for the textbox that still allows mvc to bind the edited value back to 'FromDate' in my model?
if it's coming from DB, it should have an identity value, if its there then you can use that id. other wise go for GUID.
using Guid.NewGuid() and concat with the id value.
#Html.TextBox("fromDate_"+Guid.NewGuid(), Model.ToString("d"), new { #class = "datepicker" })

ASP.NET MVC Format of Hidden DateTime

I have a model with a DateTime property that in one place is placed in a hidden input field.
#Html.HiddenFor(m => m.StartDate)
Which generates the following HTML:
<input id="StartDate" name="StartDate" type="hidden" value="1/1/2011 12:00:00 AM" >
The problem is that the time is included in the value and my custom date validation expects a date in the format of ##/##/#### thus causing validation to fail. I can easily alter my custom date validation to make this situation work but I would rather make it so that the hidden field puts the value in the correct format.
I have tried using the DisplayFormat attribute on the model property but that doesn't seem to change the format of the hidden input.
I do realize that I could just create the hidden input manually and call StartDate.ToString("MM/dd/yyyy") for the value but I am also using this model in a dynamically generated list of items so the inputs are indexed and have ids like Collection[Some-Guid].StartDate which would make it a bit more difficult to figure out the id and name of the input.
Is there anyway to make the 'value' value come out in a specific format when rendering the field on the page as a hidden input?
You could use a custom editor template:
public class MyViewModel
{
[UIHint("MyHiddenDate")]
public DateTime Date { get; set; }
}
and then define ~/Views/Shared/EditorTemplates/MyHiddenDate.cshtml:
#model DateTime
#Html.Hidden("", Model.ToString("dd/MM/yyyy"))
and finally in your view use the EditorFor helper:
#model MyViewModel
#Html.EditorFor(x => x.Date)
This will render the custom editor template for the Date property of the view model and consequently render the hidden field with a value using the desired format.

Resources