我目前正在研究一个asp.net mvc项目,该项目允许用户从日历中仅选择可用日期以预订特定服务。我目前有一个Datepicker.cs模型,其中包含一个DateTime Date字段和一个int Id字段,并且我有一个DbContext类,用于持久化和检索此数据
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.Calendar;
import java.util.Date;
public class Database_title extends SQLiteOpenHelper {
Date currentTime = Calendar.getInstance().getTime();
private static final String Database_Name = "DatabaseDias.db";
private static final String TABLE_NAME_1 = "main";
private static final String TABLE_NAME_2 = "title";
private static final String TABLE_NAME_3 = "events";
private static final String COL1_1 = "ID";
private static final String COL1_2 = "prs_nmb";
private static final String COL1_3 = "pd";
private static final String COL1_4 = "groupp";
private static final String COL1_5 = "userid";
private static final String COL1_6 = "passw";
private static final String COL2_1 = "ID";
private static final String COL2_2 = "prs_nmb";
private static final String COL2_3 = "pd";
private static final String COL2_4 = "groupp";
private static final String COL2_5 = "date";
private static final String COL2_6 = "cd_omd";
private static final String COL2_7 = "area";
private static final String COL2_8 = "daily_shf";
private static final String COL2_9 = "chief";
private static final String COL2_10 = "veh_type";
private static final String COL2_11 = "driver_A";
private static final String COL2_12 = "co_driver_A";
private static final String COL2_13 = "veh_A";
private static final String COL2_14 = "km_A";
private static final String COL2_15 = "fuel_A";
private static final String COL2_16 = "driver_B";
private static final String COL2_17 = "co_driver_B";
private static final String COL2_18 = "veh_B";
private static final String COL2_19 = "km_B";
private static final String COL2_20 = "fuel_B";
private static final String COL3_1 = "ID";
private static final String COL3_2 = "date";
private static final String COL3_3 = "cd_omadas";
private static final String COL3_4 = "orig_event";
private static final String COL3_5 = "address";
private static final String COL3_6 = "announcer";
private static final String COL3_7 = "Events_descr";
public Database_title(Context context) {
super(context, Database_Name, null, 1);
Log.d("Database Operations", "Database created...");
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable_1 = "CREATE TABLE " + TABLE_NAME_1 + "(" + COL1_1 +" INTEGER PRIMARY KEY AUTOINCREMENT,"+ COL1_2 +" INTEGER,"+ COL1_3 +" TEXT,"+ COL1_4 +" TEXT,"+ COL1_5 +" TEXT,"+ COL1_6 +" TEXT)";
String createTable_2 = "CREATE TABLE " + TABLE_NAME_2 + "(" + COL2_1 +" INTEGER PRIMARY KEY AUTOINCREMENT,"+ COL2_2 +" INTEGER,"+ COL2_3 +" TEXT,"+ COL2_4 +" TEXT,"+ COL2_5 +" TEXT,"+ COL2_6 +" TEXT,"+ COL2_7 +" TEXT,"+ COL2_8 +" TEXT,"+ COL2_9 +" TEXT,"+ COL2_10 +" TEXT,"+ COL2_11 +" TEXT,"+ COL2_12 +" TEXT,"+ COL2_13 +" TEXT,"+ COL2_14 +" TEXT,"+ COL2_15 +" TEXT,"+ COL2_16 +" TEXT,"+ COL2_17 +" TEXT,"+ COL2_18 +" TEXT,"+ COL2_19 +" TEXT,"+ COL2_20 +" TEXT)";
String createTable_3 = "CREATE TABLE " + TABLE_NAME_3 + "(" + COL3_1 +" INTEGER PRIMARY KEY AUTOINCREMENT,"+ COL3_2 +" INTEGER,"+ COL3_3 +" TEXT,"+ COL3_4 +" TEXT,"+ COL3_5 +" TEXT,"+ COL3_6 +" TEXT,"+ COL3_7 +" TEXT)";
db.execSQL(createTable_1);
db.execSQL(createTable_2);
db.execSQL(createTable_3);
Log.d(Database_Name, "Hello onCreate(SQLiteDatabase db) " );
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME_1);
onCreate(db);
Log.d(Database_Name, "Hello onUpgrade(SQLiteDatabase db, int i, int i1) " );
}
public boolean addData_main(String item_1,String item_2,String item_3,String item_4,String item_5) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL1_2, item_1);
contentValues.put(COL1_3, item_2);
contentValues.put(COL1_4, item_3);
contentValues.put(COL1_5, item_4);
contentValues.put(COL1_6, item_5);
long result = db.insert(TABLE_NAME_1, null, contentValues);
if (result == -1) {
return false;
}else{
return true;
}
}
public Cursor getAllData_main() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from " + TABLE_NAME_1, null);
return res;
}
/*
public Cursor getData(String Col,String table_name){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + table_name;
Cursor data = db.rawQuery(query, null);
return data;
}
*/
public Cursor getItemID(String name){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT " + COL1_1 + " FROM " + TABLE_NAME_1 +
" WHERE " + COL1_2 + " = '" + name + "'";
Cursor data = db.rawQuery(query, null);
return data;
}
public boolean updateName_main(String prs_nmb, String pd, String groupp,String userid,String pass){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
//contentValues.put(COL1_1,"1");
contentValues.put(COL1_2,prs_nmb);
contentValues.put(COL1_3,pd);
contentValues.put(COL1_4,groupp);
contentValues.put(COL1_5,userid);
contentValues.put(COL1_6,pass);
db.update(TABLE_NAME_1,contentValues,"ID = 1",null);
return true;
}
public void deleteName(int id, String name){
SQLiteDatabase db = this.getWritableDatabase();
String query = "DELETE FROM " + TABLE_NAME_1 + " WHERE "
+ COL1_1 + " = '" + id + "'" +
" AND " + COL1_2 + " = '" + name + "'";
Log.d(Database_Name, "deleteName: query: " + query);
Log.d(Database_Name, "deleteName: Deleting " + name + " from database.");
db.execSQL(query);
}
}
我正在Create.cshtml页面上使用jQuery UI Datepicker向用户显示可用日期。当前,日历显示2个硬编码的可用日期。
现在我的问题是,是否有可能在asp.net mvc框架中用Datepickers Db的Date列中的所有日期填充此硬编码数组(甚至列表等)?即我需要日历来显示数据库中所有可用的日期
任何帮助将不胜感激!
我的Datepicker模型:
namespace Calendar.Models
{
public class CalendarDb : DbContext
{
public DbSet<Datepicker> Dates { get; set; }
}
}
该视图的控制器:
namespace Calendar.Models
{
public class Datepicker
{
public DateTime DtmDate { get; set; }
public int Id { get; set; }
public int UserId { get; set; }
}
}
我的Create.cshtml视图原样:
[HttpGet]
public ActionResult Create()
{
var model = _db.Dates.ToList();
return View(model);
}
答案 0 :(得分:2)
由于控制器中的行var model = _db.Dates.ToList();
,您的模型类型是一个列表。您需要调整视图的模型声明以使其匹配。
更改
@model Calendar.Models.Datepicker
到
@model List<Calendar.Models.Datepicker>
现在,您的模型属于正确的类型,您可以将日期嵌入到JavaScript中。我们需要使用Linq选择日期,并将其转换为JS期望的正确字符串格式,然后使用一些字符串方法将它们连接在一起。更改此:
var enableDays = ["11-07-2019", "12-07-2019"];
到
var enableDays = [' @Html.Raw(string.Join("','", Model.Select(d => d.DtmDate.ToString("MM-dd-yyyy"))) '];
然后全部运行。在呈现的HTML中,您应该看到很好地嵌入到JavaScript中的数据库中的日期,这应该在datepicker中启用正确的日期。显然,您需要处理一些极端情况,例如表中没有行,或者其中一行中的日期为null。但这应该可以帮助您入门。
答案 1 :(得分:-1)
这是您如何做到的想法。
首先创建一个返回List<DateTime>
的方法
例子
[HttpPost]
public ActionResult GetDates()
{
return Json(CalendarDb.Dates.Select(x=> x.DtmDate.ToString("dd-MM-yyyy")));
}
现在,当您创建日期选择程序时,您将需要此列表,例如enableDays
var enableDays = [];
$.ajax({
url: "/GetDates",
method: "POST" ,// post or get
async: false,
success:function(dates){ enableDays =dates; }
});
// now create your DatePicker.
现在,如果您要使用mvc modol解决方案,可以这样做。
创建一个包含日期的modol,然后简单地将其转换为json
enableDays =@Html.Raw(Json.Encode(modol.Dates.ToString("dd-MM-yyyy")))
// now create your DatePicker.