我有一個預訂頁面,其中包含一張預訂列表。每一行都有一個按鈕,以允許編輯預訂。點擊此按鈕時,會使用帶有多個字段的JavaScript顯示模式彈出窗體。我將預訂的ID
傳遞給工作正常的標題。 BookingModel
有一個列表BookingViewModels
存儲數據。使用值覆蓋表格數據
我試圖將字段的值設置爲預定模型中的值,即@Value = @item.Date
,但框中的數據不會被覆蓋,但是如果我在其下面打印日期,它將被正確打印。
如何在盒子內打印數據?
模式
public class BookingViewModels
{
public List<BookingViewModel> BookingModel { get; set; }
/// <summary>
/// Date
/// </summary>
[DataType(DataType.Date)]
[Display(Name = "Date")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MM-yyyy}")]
[Required(ErrorMessage = "This field is required")]
public string Date { get; set; }
/// <summary>
/// Start Time
/// </summary>
[DataType(DataType.Time)]
[Display(Name = "Start Time")]
[Required(ErrorMessage = "This field is required")]
public string StartTime { get; set; }
/// <summary>
/// End Time
/// </summary>
[DataType(DataType.Time)]
[Display(Name = "End Time")]
[Required(ErrorMessage = "This field is required")]
public string EndTime { get; set; }
}
public class BookingViewModel
{
public int BookingId { get; set; }
public string Date { get; set; }
public string StartTime { get; set; }
public string EndTime { get; set; }
public string inf1 { get; set; }
public string inf2 { get; set; }
}
查看
//the table with the edit buttons
@foreach (var item in Model.BookingModel)
{
@{var grid = new WebGrid(Model.BookingModel, canSort: false); }
<input type="button" id="btnaddbooking" class="btn btn-small btn-primary" value="Add Booking" data-toggle="modal" data-target="#modalbox" />
<hr />
<div class="panel panel-default pre-scrollable">
<table class="table table-striped table-bordered table-responsive table-condensed table-hover">
<thead>
<tr>
<th>Date</th>
<th>Start Time</th>
<th>Duration</th>
<th>End Time</th>
<th>inf1</th>
<th>inf2</th>
</tr>
</thead>
@foreach (var item in Model.BookingModel)
{
<tr>
<td>
@item.Date
</td>
<td>
@item.StartTime
</td>
<td>
@item.Duration
</td>
<td>
@item.EndTime
</td>
<td>
@item.inf1
</td>
<td>
@item.inf2
</td>
@using (Html.BeginForm("Delete", "Booking", FormMethod.Post))
{
@Html.AntiForgeryToken()
<td>
<button class="someButtonClass" data-toggle="modal" data-target="#[email protected]" />
</td>
<td>
<button class="someButtonClass2" type="submit" value="Delete" />
</td>
}
</tr>
}
</table>
</div>
}
//the modal form
@foreach (var item in Model.BookingModel)
{
using (Html.BeginForm("Submit", "Booking", FormMethod.Post))
{
<div id="[email protected]" class="modal">
<div class="modal-dialog">
<div class="modal-content">
<div id="modal-header" class="modal-header">
<h1>Edit Booking #@item.BookingId</h1>
</div>
<div class="modal-body">
<table id="booking-model-table" class="table table-condensed table-striped table-bordered">
<tr>
<td>Date:</td>
<td>
@{
@Html.EditorFor(m => m.Date, new { type = "time", @Value = @item.Date })
@Html.ValidationMessageFor(m => m.Date, "", new { @class = "text-danger" })
}
@item.Date
</td>
</tr>
<tr>
<td>Start Time:</td>
<td>
@{
@Html.EditorFor(m => m.StartTime, new { type = "time", Value = @item.StartTime })
@Html.ValidationMessageFor(m => m.StartTime, "", new { @class = "text-danger" })
}
</td>
</tr>
<tr>
<td>End Time:</td>
<td>
@{
@Html.EditorFor(m => m.EndTime, new { type = "time", Value = @item.EndTime })
@Html.ValidationMessageFor(m => m.EndTime, "", new { @class = "text-danger" })
}
</td>
</tr>
</table>
</div>
<div class="modal-footer">
@Html.AntiForgeryToken()
<button class="btn btn-default" data-dismiss="modal">Close</button>
<button class="btn btn-primary" type="submit" value="Submit">Ok</button>
</div>
</div>
</div>
</div>
}
}
的JavaScript
$(function() {
var modelbox = function() {
$("#modalbox").modal("show");
};
$("#btnaddbooking").on('click', modelbox);
});
此頁面顯示的表預訂,此表中的每一行都有一個編輯按鈕。每個編輯按鈕都鏈接到一個唯一的表單,以允許來自所選預訂的信息加載到該表單中。 感謝您使用value屬性的反饋,但是您能否提出另一種方式來做到這一點,這就是我所問的問題,因爲我不知道該怎麼做。 該表單適用於所有意圖和目的,用於預訂和驗證正確工作。它不能正確執行的唯一方法是將值預加載到表單框中。 – Ben
謝謝。我已經添加了關於第一個foreach循環的更多信息。 – Ben