好了,所以我有這個類:添加一個新行我的看法動態
public class BackstoreInventoryUtility
{
public BackstoreInventoryInfo Item { get; set; }
public List<ItemListingUtility> ListItemUtility { get; set; }
public BackstoreInventoryUtility()
{
Item = new BackstoreInventoryInfo();
ListItemUtility = new List<ItemListingUtility>();
}
}
而這裏的ListItemUtility
類:
public class ItemListingUtility
{
public int Quantity { get; set; }
public string Duration { get; set; }
public List<string> AvailableDurations { get; set; }
public ItemListingUtility()
{
AvailableDurations = new List<string>();
}
}
在視圖中我建設,我顯示1 BackstoreInventoryUtility
根據我的用戶當前瀏覽的BackstoreInventoryInfo
項目。
ListItemUtility
是一個允許用戶進行某種操作的類,例如在設定的時間內顯示設定的數量。
視圖呈現這樣的:
@model MyApp.Utilities.BackstoreInventoryUtility
@using (Html.BeginForm())
{
<div>
@if (Model.Item.Quantity > 0)
{
<input type="submit" value="Display"/>
}
@Html.HiddenFor(_item => _item.Item.BackstoreInventoryID)
<div class="bigFontSize bold formStyle">
<label class="center">Options will eventually be displayed here.</label>
<div>
<div class="float-left">Quantity Allocated:</div>
<div class="float-right">@Html.DisplayFor(_item => _item.Item.Quantity)
@Html.HiddenFor(_item => _item.Item.Quantity)
</div>
<div class="clear"></div>
</div>
<div class="formStyle" id="itemUtilityZone">
<label>Options</label>
@for (int i = 0; i < Model.ListItemUtility.Count; i++)
{
<div>
<div class="float-left">
Quantity To Display:
</div>
<div class="float-right">
@Html.TextBoxFor(_item => _item.ListItemUtility[i].Quantity, new { @class = "positive-integer numberTextBox" })
</div>
<div class="clear"></div>
</div>
}
</div>
@if (Model.Item.Quantity > 0)
{
<input type="submit" value="Display"/>
}
</div>
}
我想我的用戶動態地添加一個新行到視圖,然後在視圖提交時,所有行會被包括在內。
到目前爲止,我在一開始,我想這一點:
[HttpGet]
public ActionResult AddItemUtilityRow()
{
return PartialView(new ItemListingUtility());
}
其中呈現的局部視圖可以等同於表採用的股利。但我不確定我該如何做到這一點,我應該使用jQuery調用?我該怎麼做?
編輯好吧,jQuery的,所以我曾嘗試一些東西,在視覺上做什麼,我想:
<script type="text/javascript">
$(document).ready(function() {
$("#addUtility").click(function() {
$.get("@Url.Action("AddItemUtilityRow")", {
}, function(data) {
$('#itemUtilityZone').append(data);
});
});
});
</script>
所以,正如我所說,這工作,但只是部分,因爲當用戶只提交默認號碼列表中的項目被提交。我該如何做到這一點,以便每次用戶添加一個與模型相加的行並在以後提交?
哦。好。因此,我應該考慮發佈整個模型,然後重新顯示它,而不僅僅是增加一個新項目。或者有沒有辦法像我在我的帖子中那樣去做? – hsim
因爲我很樂意做客戶端。 – hsim
是的,關於服務器端路由......整個shebang上升,被修改,然後又回落。優點是重用您的視圖和其他代碼。如果你走的是客戶端路由,這肯定更性感,我會強烈推薦一些模板ala KnockoutJS。它會爲格式化帶來很多痛苦。你也可以嘗試混合這兩種方法,或許調用服務器並讓它返回行(格式化)並按照你開始的方式添加它。我唯一的評論就是,全力以赴,簡化一切。 – BlackjacketMack