我有一張TimeTable桌子和一張Product桌子。每個產品將有4個時間表。


在我的更新產品頁面,它應該能夠檢索最新的時間表并將每個時間表拆分到相應的下拉串列中。目前,當我單擊更新頁面時,它只顯示我不想要的空日歷。我希望日歷填充資料庫時間。我的想法是拆分 [day] [time] [am/pm] 然后推送到下拉串列,但我不知道如何實作。任何愿意指導我的人將不勝感激,謝謝。
aspx
<tr>
<td>
<label class="col-sm-2 control-label">Timetable1:</label>
</td>
<td>
<div class="auto-style1">
<label>Every</label>
<asp:DropDownList ID="weekTime1" runat="server">
<asp:ListItem>Monday</asp:ListItem>
<asp:ListItem>Tuesday</asp:ListItem>
<asp:ListItem>Wednesday</asp:ListItem>
<asp:ListItem>Thursday</asp:ListItem>
<asp:ListItem>Friday</asp:ListItem>
<asp:ListItem>Saturday</asp:ListItem>
<asp:ListItem>Sunday</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="startTime1" runat="server">
<asp:ListItem>10</asp:ListItem>
<asp:ListItem>11</asp:ListItem>
<asp:ListItem>12</asp:ListItem>
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
<asp:ListItem>7</asp:ListItem>
<asp:ListItem>8</asp:ListItem>
<asp:ListItem>9</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="startAP1" runat="server">
<asp:ListItem>AM</asp:ListItem>
<asp:ListItem>PM</asp:ListItem>
</asp:DropDownList>
<label>- </label>
<asp:DropDownList ID="endTime1" runat="server">
<asp:ListItem>10</asp:ListItem>
<asp:ListItem>11</asp:ListItem>
<asp:ListItem>12</asp:ListItem>
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
<asp:ListItem>7</asp:ListItem>
<asp:ListItem>8</asp:ListItem>
<asp:ListItem>9</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="endAP1" runat="server">
<asp:ListItem>AM</asp:ListItem>
<asp:ListItem>PM</asp:ListItem>
</asp:DropDownList>
</div>
</td>
</tr>
<tr>
<td>
<label class="col-sm-2 control-label">Timetable2:</label>
</td>
<td>
<div class="auto-style1">
<label>Every</label>
<asp:DropDownList ID="weekTime2" runat="server">
<asp:ListItem>Monday</asp:ListItem>
<asp:ListItem>Tuesday</asp:ListItem>
<asp:ListItem>Wednesday</asp:ListItem>
<asp:ListItem>Thursday</asp:ListItem>
<asp:ListItem>Friday</asp:ListItem>
<asp:ListItem>Saturday</asp:ListItem>
<asp:ListItem>Sunday</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="startTime2" runat="server">
<asp:ListItem>10</asp:ListItem>
<asp:ListItem>11</asp:ListItem>
<asp:ListItem>12</asp:ListItem>
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
<asp:ListItem>7</asp:ListItem>
<asp:ListItem>8</asp:ListItem>
<asp:ListItem>9</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="startAP2" runat="server">
<asp:ListItem>AM</asp:ListItem>
<asp:ListItem>PM</asp:ListItem>
</asp:DropDownList>
<label>- </label>
<asp:DropDownList ID="endTime2" runat="server">
<asp:ListItem>10</asp:ListItem>
<asp:ListItem>11</asp:ListItem>
<asp:ListItem>12</asp:ListItem>
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
<asp:ListItem>7</asp:ListItem>
<asp:ListItem>8</asp:ListItem>
<asp:ListItem>9</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="endAP2" runat="server">
<asp:ListItem>AM</asp:ListItem>
<asp:ListItem>PM</asp:ListItem>
</asp:DropDownList>
</div>
</td>
</tr>
<tr>
<td>
<label class="col-sm-2 control-label">Timetable3:</label>
</td>
<td>
<div class="auto-style1">
<label>Every</label>
<asp:DropDownList ID="weekTime3" runat="server">
<asp:ListItem>Monday</asp:ListItem>
<asp:ListItem>Tuesday</asp:ListItem>
<asp:ListItem>Wednesday</asp:ListItem>
<asp:ListItem>Thursday</asp:ListItem>
<asp:ListItem>Friday</asp:ListItem>
<asp:ListItem>Saturday</asp:ListItem>
<asp:ListItem>Sunday</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="startTime3" runat="server">
<asp:ListItem>10</asp:ListItem>
<asp:ListItem>11</asp:ListItem>
<asp:ListItem>12</asp:ListItem>
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
<asp:ListItem>7</asp:ListItem>
<asp:ListItem>8</asp:ListItem>
<asp:ListItem>9</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="startAP3" runat="server">
<asp:ListItem>AM</asp:ListItem>
<asp:ListItem>PM</asp:ListItem>
</asp:DropDownList>
<label>- </label>
<asp:DropDownList ID="endTime3" runat="server">
<asp:ListItem>10</asp:ListItem>
<asp:ListItem>11</asp:ListItem>
<asp:ListItem>12</asp:ListItem>
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
<asp:ListItem>7</asp:ListItem>
<asp:ListItem>8</asp:ListItem>
<asp:ListItem>9</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="endAP3" runat="server">
<asp:ListItem>AM</asp:ListItem>
<asp:ListItem>PM</asp:ListItem>
</asp:DropDownList>
</div>
</td>
</tr>
<tr>
<td>
<label class="col-sm-2 control-label">Timetable4:</label>
</td>
<td>
<div class="auto-style1">
<label>Every</label>
<asp:DropDownList ID="weekTime4" runat="server">
<asp:ListItem>Monday</asp:ListItem>
<asp:ListItem>Tuesday</asp:ListItem>
<asp:ListItem>Wednesday</asp:ListItem>
<asp:ListItem>Thursday</asp:ListItem>
<asp:ListItem>Friday</asp:ListItem>
<asp:ListItem>Saturday</asp:ListItem>
<asp:ListItem>Sunday</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="startTime4" runat="server">
<asp:ListItem>10</asp:ListItem>
<asp:ListItem>11</asp:ListItem>
<asp:ListItem>12</asp:ListItem>
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
<asp:ListItem>7</asp:ListItem>
<asp:ListItem>8</asp:ListItem>
<asp:ListItem>9</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="startAP4" runat="server">
<asp:ListItem>AM</asp:ListItem>
<asp:ListItem>PM</asp:ListItem>
</asp:DropDownList>
<label>- </label>
<asp:DropDownList ID="endTime4" runat="server">
<asp:ListItem>10</asp:ListItem>
<asp:ListItem>11</asp:ListItem>
<asp:ListItem>12</asp:ListItem>
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
<asp:ListItem>7</asp:ListItem>
<asp:ListItem>8</asp:ListItem>
<asp:ListItem>9</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="endAP4" runat="server">
<asp:ListItem>AM</asp:ListItem>
<asp:ListItem>PM</asp:ListItem>
</asp:DropDownList>
<asp:CustomValidator ID="repeatTimeValidation" runat="server" ErrorMessage="Repeated Time Found." ForeColor="Red" Display="Dynamic" OnServerValidate="repeatTimeValidation_ServerValidate"></asp:CustomValidator>
</div>
</td>
</tr>
。CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Helpers;
namespace Bracelet
{
public partial class manViewProduct1 : System.Web.UI.Page
{
BraceletDataContext bc = new BraceletDataContext();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string id = Request.QueryString["Id"];
Product p = bc.Products.SingleOrDefault(x => x.ProductID == id);
if (p != null)
{
txtProdID.Value = p.ProductID;
txtID.Text = p.ProductID;
imgProduct.ImageUrl = "Photos/" p.Image;
txtViewCategory.Text = p.Category.CategoryName;
txtViewCurrent.Text = p.CurrentStock.ToString();
txtViewDescription.Text = p.Description;
txtViewName.Text = p.ProductName;
txtViewUnitPrice.Text = p.UnitPrice.ToString("0.00");
imgProd.ImageUrl = "Photos/" p.Image;
txtName.Text = p.ProductName;
txtDescription.Text = p.Description;
txtPrice.Text = p.UnitPrice.ToString("0.00");
txtLeft.Text = p.CurrentStock.ToString();
startTime.Value = p.Start;
endTime.Value = p.End;
start1.Text = p.Start;
end1.Text = p.End;
}
var timeList = bc.TimeTables.OrderByDescending(x => x.Id).Where(x => x.ProductID.Contains(id)).ToList();
txtTime1.Text = timeList[0].Time.ToString();
txtTime2.Text = timeList[1].Time.ToString();
txtTime3.Text = timeList[2].Time.ToString();
txtTime4.Text = timeList[3].Time.ToString();
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
string id = Request.QueryString["Id"];
Product p = bc.Products.SingleOrDefault(x => x.ProductID == id);
if (p != null)
{
p.ProductStatus = 0;
bc.SubmitChanges();
}
Response.Redirect("manListProduct.aspx");
}
protected void btnCancel_Click(object sender, EventArgs e)
{
Server.Transfer("manListProduct.aspx");
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
string id = Request.QueryString["Id"];
float price = float.Parse(txtPrice.Text);
string name = txtName.Text;
string description = txtDescription.Text;
string productName = txtName.Text;
string productDescription = txtDescription.Text;
string wTime1 = weekTime1.Text;
string sTime1 = startTime1.Text;
string sAP1 = startAP1.Text;
string eTime1 = endTime1.Text;
string eAP1 = endAP1.Text;
string wTime2 = weekTime2.Text;
string sTime2 = startTime2.Text;
string sAP2 = startAP2.Text;
string eTime2 = endTime2.Text;
string eAP2 = endAP2.Text;
string wTime3 = weekTime3.Text;
string sTime3 = startTime3.Text;
string sAP3 = startAP3.Text;
string eTime3 = endTime3.Text;
string eAP3 = endAP3.Text;
string wTime4 = weekTime4.Text;
string sTime4 = startTime4.Text;
string sAP4 = startAP4.Text;
string eTime4 = endTime4.Text;
string eAP4 = endAP4.Text;
string start1 = startTime.Value;
string end1 = endTime.Value;
//Every Friday 5am - 6pm
string time1 = "Every " wTime1 " " sTime1 sAP1 " - " eTime1 eAP1;
string time2 = "Every " wTime2 " " sTime2 sAP2 " - " eTime2 eAP2;
string time3 = "Every " wTime3 " " sTime3 sAP3 " - " eTime3 eAP3;
string time4 = "Every " wTime4 " " sTime4 sAP4 " - " eTime4 eAP4;
List<string> timeList = new List<string>();
timeList.Add(time1);
timeList.Add(time2);
timeList.Add(time3);
timeList.Add(time4);
foreach (string t2 in timeList)
{
TimeTable tt = new TimeTable
{
ProductID = id,
Time = t2,
};
bc.TimeTables.InsertOnSubmit(tt);
}
Product p = bc.Products.SingleOrDefault(x => x.ProductID == id);
if (p != null)
{
p.ProductName = name;
p.Description = description;
p.UnitPrice = price;
p.Start = start1;
p.End = end1;
bc.SubmitChanges();
}
Response.Redirect("manListProduct.aspx");
}
}
protected void btnReset_Click(object sender, EventArgs e)
{
string id = Request.QueryString["Id"];
Product p = bc.Products.SingleOrDefault(x => x.ProductID == id);
txtName.Text = p.ProductName;
txtDescription.Text = p.Description;
txtPrice.Text = p.UnitPrice.ToString("0.00");
startTime.Value = p.Start;
endTime.Value = p.End;
}
protected void btnAdd_Click(object sender, EventArgs e)
{
string id = Request.QueryString["Id"];
int stock = int.Parse(txtStock.Text);
Product p = bc.Products.SingleOrDefault(x => x.ProductID == id);
if (p != null)
{
p.CurrentStock = p.CurrentStock stock;
bc.SubmitChanges();
}
Response.Redirect("manListProduct.aspx");
}
protected void btnUpphoto_Click(object sender, EventArgs e)
{
string id = Request.QueryString["Id"];
Response.Redirect("manUpdateProductPhoto.aspx?Id=" id);
}
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
string name = args.Value;
string id = Request.QueryString["Id"];
if (bc.Products.Any(u => u.ProductID != id && u.ProductName.ToUpper() == name.ToUpper()))
{
args.IsValid = false;
}
}
protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
{
string name = args.Value;
string id = Request.QueryString["Id"];
if (bc.Products.Any(u => u.ProductID != id && u.Description.ToUpper() == name.ToUpper()))
{
args.IsValid = false;
}
}
protected void repeatTimeValidation_ServerValidate(object source, ServerValidateEventArgs args)
{
string wTime1 = weekTime1.Text;
string sTime1 = startTime1.Text;
string sAP1 = startAP1.Text;
string eTime1 = endTime1.Text;
string eAP1 = endAP1.Text;
string wTime2 = weekTime2.Text;
string sTime2 = startTime2.Text;
string sAP2 = startAP2.Text;
string eTime2 = endTime2.Text;
string eAP2 = endAP2.Text;
string wTime3 = weekTime3.Text;
string sTime3 = startTime3.Text;
string sAP3 = startAP3.Text;
string eTime3 = endTime3.Text;
string eAP3 = endAP3.Text;
string wTime4 = weekTime4.Text;
string sTime4 = startTime4.Text;
string sAP4 = startAP4.Text;
string eTime4 = endTime4.Text;
string eAP4 = endAP4.Text;
//Every Friday 5am - 6pm
string time1 = "Every " wTime1 " " sTime1 sAP1 " - " eTime1 eAP1;
string time2 = "Every " wTime2 " " sTime2 sAP2 " - " eTime2 eAP2;
string time3 = "Every " wTime3 " " sTime3 sAP3 " - " eTime3 eAP3;
string time4 = "Every " wTime4 " " sTime4 sAP4 " - " eTime4 eAP4;
Console.WriteLine(time1);
Console.WriteLine(time2);
if (String.Equals(time1, time2))
{
args.IsValid = false;
}
else if (String.Equals(time1, time3))
{
args.IsValid = false;
}
else if (String.Equals(time1, time4))
{
args.IsValid = false;
}
else if (String.Equals(time2, time3))
{
args.IsValid = false;
}
else if (String.Equals(time2, time4))
{
args.IsValid = false;
}
else if (String.Equals(time3, time4))
{
args.IsValid = false;
}
}
}
}
uj5u.com熱心網友回復:
你的總體需求是將資料庫中的時間拆分為一個,然后從資料庫中獲取并顯示在頁面上,對嗎?
你可以得到它然后拆分它。我寫了一個簡單的demo,可能對你有幫助:
控制器:
public IActionResult Index()
{
string time = "Every Monday 10 AM - 11 Am";
string[] ssize = time.Split(null);
ArrayList arlist = new ArrayList();
foreach (String s in ssize)
{
arlist.Add(s);
}
string data = (string)arlist[0];
ViewBag.data = data;
string data1 = (string)arlist[1];
ViewBag.data1 = data1;
string data2 = (string)arlist[2];
ViewBag.data2 = data2;
string data3 = (string)arlist[3];
ViewBag.data3 = data3;
string data4 = (string)arlist[4];
ViewBag.data4 = data4;
string data5 = (string)arlist[5];
ViewBag.data5 = data5;
string data6 = (string)arlist[6];
ViewBag.data6 = data6;
return View();
}
我省略了從資料庫中獲取時間的操作。得到后可以按空格分割,遍歷加入alist,然后根據下標獲取資料,最后顯示在頁面上。
看法:
@ViewBag.data
<select><option>@ViewBag.data1</option></select>
<select><option>@ViewBag.data2</option></select>
<select><option>@ViewBag.data3</option></select>
@ViewBag.data4
<select><option>@ViewBag.data5</option></select>
<select><option>@ViewBag.data6</option></select>
結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/363730.html
標籤:C# 网站 asp.net-mvc asp.net核心
上一篇:關于razor,.NETMVC對razor和.NETrazor頁面的支持有什么區別,有沒有什么叫做.NETMVCrazor頁面?
