asp.net-mvc – MVC5将项目添加到列表中
发布时间:2020-12-16 06:33:02 所属栏目:asp.Net 来源:网络整理
导读:给出一个模型: public class Receipt: BaseEntity{ [Required(AllowEmptyStrings = false,ErrorMessage = "Please Select A Store")] public Store Store { get; set; } public ListItem Items { get; set; } public Item NewItem { get; private set; } [R
|
给出一个模型:
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();
}
}
和一个控制器: 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);
}
并且视图:更新后的视图 – 删除了所有部分页面并合并为一个页面 <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,我终于能够解决这个问题了
这确实是一个具有约束力的问题.似乎对于像Lists这样的复杂集合,我们实际上无法像我希望的那样利用IEnumerable的好处.这是由于数据在具有不明确名称的帖子中实际传递的方式.这是最终的代码(只需要更新视图) @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现在可以正确绑定发布数据. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – ASP.NET MVC 3自定义授权
- asp.net-mvc – 为什么我们使用ViewModels?
- 如何组合WebResource.axd和ScriptResource.axd文件,以减少对
- asp.net – 有没有办法使用System.Net.Mail.SendAsync()捕获
- vs文件上传失败--超过最大字符限制
- asp.net – 什么内容类型强制下载文本响应?
- asp.net-mvc – MVC3区域中的相对内容路径
- 配置 – ASP.NET Web Api:项目需要SQL Server Express
- .net – ashx vs aspx文件下载
- telerik – ASP.NET MVC2富文本编辑器
推荐文章
站长推荐
- asp.net-mvc-3 – 如何用另一个替换局部视图?
- 为ASP.NET应用程序实现后台服务的最佳方法是什么
- asp.net – 为什么Telerik控件的某些WebResource
- Global.asax中的ASP.NET MVC Application_Error处
- asp.net-mvc – 在视图ASP.NET MVC 4中禁用所需的
- asp.net – 如何打印网页的某个块/部分?
- asp.net – 在IIS7上设置虚拟目录
- ASP.NET中的自定义元素与自定义子元素
- asp.net – Moq Parent没有默认构造函数.必须显式
- asp.net – web配置错误:无法识别的属性’xmlns
热点阅读
