我有两个“Html.TextBoxFor”,它链接到一个模型字段(强类型)。在页面加载时,它在文本框中显示值0001-01-01 00:00:00。我想以“dd-MM-yyyy”格式获取当前日期并将其放入文本框中。因此,当您加载页面时,应该会看到08-09-2015的日期。
要做到这一点,我需要改变格式。我到目前为止尝试的是添加这行代码:
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
在我的模型中的字段。这实际上有效,但在页面加载时,文本框仍然有“0001-01-01 00:00:00”,而不是“0001-01-01”
同样获取当前日期,是使用Jquery的最佳方法吗?
文本框的一些代码
<label>Departure date</label>
<br />
@Html.TextBoxFor(m => m.DepartureDate, new { @class = "textbox" })
<label>Return date</label>
<br />
@Html.TextBoxFor(m => m.ReturnDate, new { @class = "textbox" })
答案 0 :(得分:2)
你必须这样做:
private final TextWatcher vesselWatcher = new TextWatcher()
{
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
Log.e("beforeTextChanged ", " here !!! ");
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
public void afterTextChanged(Editable s)
{
if (s.length() == 0)
{
txtVesselName.setVisibility(View.GONE);
Log.e("afterTextChanged View ", " is gone !!!");
btnPost.setClickable(false);
btnPost.setEnabled(false);
Log.e("afterTextChanged ", " btnPost.setEnabled(false); !!!");
}
else
{
if(notations.contains(etxtVesselCode.getText().toString().trim()))
{
Log.e("AAA strNotations ", " Match !!! = " + etxtVesselCode.getText().toString().trim());
String notations = etxtVesselCode.getText().toString().trim();
SQLiteDatabase db = dbhelper.getWritableDatabase();
Cursor cursor = db.rawQuery("select * from VesselList where Notation " + "= ? ", new String[]{notations});
if (cursor.moveToFirst())
{
do
{
strVesselsTypeName = cursor.getString(cursor.getColumnIndex("VesselsTypeName"));
Log.e("strVesselsTypeName ", "= " + strVesselsTypeName );
SharedPreferences sp;
sp = getApplicationContext().getSharedPreferences(MyPREFERENCES, 0);
SharedPreferences.Editor e = sp.edit();
e.putString("xyz", etxtVesselCode.getText().toString().trim());
e.commit();
Log.e("Match","!!!!");
txtVesselName.setText(strVesselsTypeName);
btnPost.setClickable(true);
btnPost.setEnabled(true);
Log.e("afterTextChanged ", " btnPost.setEnabled(true); !!!");
} while (cursor.moveToNext());
} db.close();
}
else
{
Log.e("Not "," Match !!");
txtVesselName.setText("Wrong Code !!");
btnPost.setClickable(false);
btnPost.setEnabled(false);
}
}
}
};
或者您可以在获取Model属性时执行此操作:
@Html.TextBoxFor(m => m.DepartureDate, new {
Value=Model.DepartureDate == DateTime.MinValue ? DateTime.Now.ToString("dd-MM-yyyy") : Model.DepartureDate ,
@class = "textbox"
})
然后你现在不需要使用private DateTime _departureDate
public DateTime DepartureDate
{
get
{
return _departureDate == DateTime.MinValue ? DateTime.Now : _departureDate;
}
set
{
_departureDate = value;
}
}
属性,你必须像你的OP一样写:
Value
答案 1 :(得分:1)
使用jQuery也是一个很好的解决方案。它将同时丰富用户界面。
jQuery代码将是这样的,
<label>Departure date</label>
<br />
@Html.TextBoxFor(m => m.DepartureDate, new { @class = "textbox",@id="datepicker" })
<label>Return date</label>
<br />
@Html.TextBoxFor(m => m.ReturnDate, new { @class = "textbox" })
添加以下脚本和样式:
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script>
$(function () {
$("#datepicker").datepicker();
$("#ReturnDate").datepicker();
});
</script>
默认情况下,Razor View Engine会为TextBox Editor创建一个id作为模型属性名称。这就是为什么
$("#ReturnDate").datepicker();
也会奏效。 希望这会有所帮助:)
答案 2 :(得分:0)
将Date的值与来自控制器DateTime.Now的model一起传递。