gpt4 book ai didi

asp.net-mvc - MVC5 将项目添加到列表

转载 作者:行者123 更新时间:2023-12-02 23:33:40 25 4
gpt4 key购买 nike

给定一个模型:

public class Receipt: BaseEntity
{
[Required(AllowEmptyStrings = false,ErrorMessage = "Please Select A Store")]
public Store Store { get; set; }
public List<Item> Items { get; set; }
public Item NewItem { get; private set; }
[Required(AllowEmptyStrings = false,ErrorMessage = "ReceiptModel.EntryDate is required")]
public DateTime EntryDate { get; set; }
public Guid EntryOwner { get; set; }
public string Name { get; set; }

public double ReceiptTotal()
{
return Items.Sum(item => item.ItemPrice);
}
public Receipt()
{
Items = new List<Item>();
EntryOwner = Guid.Empty;
EntryDate = DateTime.Now;
Store = new Store();
NewItem = new Item();
}
}

还有一个 Controller :

public class ReceiptController : Controller
{
//
// GET: /Receipt/

public ActionResult Index()
{
Receipt receipt = new Receipt();
return View(receipt);
}

[HttpPost]
public ActionResult AddItem(Receipt receipt)
{
receipt.Items.Add(receipt.NewItem);
if (ModelState.IsValid)
{
return View("Index", receipt);
}
return View("Index", receipt);
}

和 View :更新 View - 删除所有部分页面并合并为单个页面

<form class="form-horizontal" method="POST" action="@Url.Action("AddItem","Receipt")">
<div class="jumbotron">
<div id="main" class="panel panel-primary">

<div class="panel-heading"> <h1 class="panel-title">Receipt Builder</h1></div>
<div class="panel-body">

<fieldset>
@Html.LabelFor(model => model.Name)
@Html.EditorFor(model => model.Name)

@Html.LabelFor(model => model.Store.Name)
@Html.EditorFor(model => model.Store.Name)

@Html.LabelFor(model => model.EntryDate)
@Html.EditorFor(model => model.EntryDate)
</fieldset>

</div>


</div>
</div>

<table class="table table-bordered table-condensed table-hover table-responsive table-striped">
<thead>
<tr>
<td>
<label class="control-label">#</label>
</td>
<td>
<label class="control-label">Item</label>
</td>
<td>
<label class="control-label">Unit</label>
</td>
<td>
<label class="control-label">Quantity</label>
</td>
<td>
<label class="control-label">UnitPrice</label>
</td>
<td>
<label class="control-label">Category</label>
</td>
<td>
<label class="control-label">Total</label>
</td>
</tr>
<tr>

<td>
<button class="btn-sm btn-danger" style="border-radius: 50%; background-color: red;" name="Submit" id="Submit" type="submit">+</button>
</td>

<td>
@Html.EditorFor(model => model.NewItem.Name)
</td>

<td>
@Html.EditorFor(model => model.NewItem.UnitType)
</td>

<td>
@Html.EditorFor(model => model.NewItem.Quantity)
</td>

<td>
@Html.EditorFor(model => model.NewItem.UnitPrice)
</td>

<td>
@Html.EditorFor(model => model.NewItem.Category.Name)<br/>
@Html.EditorFor(model => model.NewItem.Category.IsTaxable)
</td>

<td>
@Html.EditorFor(model => model.NewItem.ItemPrice)
</td>


</tr>
</thead>
<tbody>
@foreach (var item in Model.Items)
{
<tr>
<td>
@Model.Items.ToList().IndexOf(item) + 1
</td>
<td>
@Html.LabelFor(titem => item.Name)
</td>
<td>
@Html.LabelFor(titem => item.UnitType)
</td>
<td>
@Html.LabelFor(titem => item.Quantity)
</td>
<td>
@Html.LabelFor(titem => item.UnitPrice)
</td>
<td>
@Html.LabelFor(titem => item.Category.Name)
</td>
<td>
@Html.LabelFor(titem => item.ItemPrice)
</td>
</tr>
}
</tbody>

<tfoot></tfoot>
</table>

最佳答案

感谢This Link,我终于能够解决这个问题

正如所建议的那样,这确实是一个具有约束力的问题。似乎对于诸如列表之类的复杂集合,我们实际上无法像我希望的那样利用 IEnumerable 的好处。这是由于数据在名称不明确的帖子中实际传递的方式所致。这是最终有效的代码(只需更新 View )

 @for (int i = 0; i < Model.Items.Count(); i++)
{
<tr>
<td>
@(i+1)
</td>
<td>
@Html.TextBoxFor(m => m.Items[i].Name)
</td>
<td>
@Html.TextBoxFor(m => m.Items[i].UnitType)
</td>
<td>
@Html.TextBoxFor(m => m.Items[i].Quantity)
</td>
<td>
@Html.TextBoxFor(m => m.Items[i].UnitPrice)
</td>
<td>
@Html.TextBoxFor(m => m.Items[i].Category.Name)
</td>
<td>
@Model.Items[i].ItemPrice
</td>
</tr>
}

生成的 HTML 使用其索引命名每个元素,即 Item[N].Name现在可以正确绑定(bind)post数据了。

关于asp.net-mvc - MVC5 将项目添加到列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20027021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com