gpt4 book ai didi

asp.net-mvc - ASP.NET MVC3 View 模型在回发到 Controller 时不为空但缺少数据

转载 作者:行者123 更新时间:2023-12-05 00:35:20 26 4
gpt4 key购买 nike

这是我的情况:

我有一个名为 OrderFromCategory 的 Controller 操作,它用数据填充 View 模型(在这种情况下, View 模型只是另一个 View 模型类型的项目列表)。这将传递给强类型 View ,该 View 显示项目列表并让用户更改每个项目的数量。在 Post 上,我的 post Controller 操作接收回模型对象,但列表中没有对象。我错过了接线吗?不知所措,任何帮助将不胜感激!非常感谢!

另外,我的验证是否正确连接?在我让模型数据持久化之前无法测试!

这是代码:

View 模型:

public class MenuItemsModel
{
public MenuItemsOrderModel()
{
items = new List<MenuItemOrderModel>();
}

public List<MenuItemOrderModel> items { get; set; }
}

public class MenuItemOrderModel
{
public int ItemID { get; set; }

[Display(Name = "Name")]
public string ItemName { get; set; }

[Display(Name = "Description")]
public string Description { get; set; }

[Display(Name = "Price")]
[DataType(DataType.Currency)]
public decimal Price { get; set; }

[Display(Name = "Quantity")]
public int Quantity { get; set; }

public string Qualifier { get; set; }
public int Minimum { get; set; }
}

Controller :
    public ActionResult OrderFromCategory(string id)
{
ViewData["category"] = id;
Models.MenuItemsModel model = new Models.MenuItemsModel();

foreach (Models.MenuItem item in Models.MenuItemRepository.GetMenuItemsForCategory(id))
{
model.items.Add(new Models.MenuItemOrderModel()
{
ItemID = item.ItemID,
Description = item.Description,
ItemName = item.Name,
Price = item.Price,
Qualifier = item.PriceQualifier,
Minimum = item.MinimumQuantity,
Quantity = ((namespace.Models.Order)Session["order"]).GetItemQuantity(item.ItemID)
});
}

return View(model);
}

[HttpPost]
public ActionResult OrderFromCategory(string id, Models.MenuItemsModel model)
{
//check for user inputs in all items
foreach (string inputKey in Request.Form.AllKeys)
{
Models.MenuItem item = Models.MenuItemService.GetMenuItem(int.Parse(inputKey));
int minimum = item.MinimumQuantity;
int quantity = string.IsNullOrEmpty(Request.Form[inputKey]) ? 0 : int.Parse(Request.Form[inputKey]);

if(quantity != 0 && quantity < minimum)
{
ModelState.AddModelError(string.Format("Quantity", item.ItemID), string.Format("Minimum of {0} required for order", minimum));
}

if (!ModelState.IsValid)
{
return View(model);
}

Models.OrderService.UpdateItemInOrder((Models.Order)Session["order"], item.ItemID, quantity);
}

return RedirectToAction("PlaceOrder");
}

看法:
@model namespace.Models.MenuItemsModel
@{
ViewBag.Title = "Order from " + ViewData["category"];
}

<h2>Order from @ViewData["category"] </h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>


@using (Html.BeginForm())
{
@Html.ValidationSummary(true, "Order was unsuccessful. Please correct the errors and try again.")
<div>
<fieldset>

@foreach (namespace.Models.MenuItemOrderModel item in Model.items)
{
<div class="item_group">

<div class = "item_name">
@item.ItemName
</div>
<div class = "item_description">
@item.Description
</div>
<div class = "item_price">
$@item.Price /@item.Qualifier (@item.Minimum miminum)
</div>
<div class = "item_quantity">
<div class="editor-label">
Quantity
</div>
<div class="editor-field">
@Html.TextBox(item.ItemID.ToString(), item.Quantity.ToString(), new { @class = "quantity_textbox", @type = "number"})
@Html.ValidationMessage("Quantity", "*")
</div>
</div>

</div>
}

<p>
<input type="submit" value="Continue" />
</p>

</fieldset>
</div>
}

最佳答案

您的表单中似乎没有用于 View 模型字段的相应输入字段。此外,您的数量文本框的命名是错误的。尝试使用以下内容替换 View 中的 foreach 循环:

@for (int i = 0; i < Model.items.Count; i++)
{
@Html.HiddenFor(x => x.items[i].ItemID)
@Html.HiddenFor(x => x.items[i].ItemName)
@Html.HiddenFor(x => x.items[i].Description)
@Html.HiddenFor(x => x.items[i].Price)
@Html.HiddenFor(x => x.items[i].Qualifier)
@Html.HiddenFor(x => x.items[i].Minimum)

<div class="item_group">
<div class = "item_name">
@Model.items[i].ItemName
</div>
<div class = "item_description">
@Model.items[i].Description
</div>
<div class = "item_price">
$@Model.items[i].Price /@Model.items[i].Qualifier (@Model.items[i].Minimum miminum)
</div>
<div class = "item_quantity">
<div class="editor-label">
Quantity
</div>
<div class="editor-field">
@Html.TextBoxFor(x => x.items[i].Quantity, new { @class = "quantity_textbox", @type = "number" })
@Html.ValidationMessageFor(x => x.items[i].Quantity, "*")
</div>
</div>
</div>
}

关于asp.net-mvc - ASP.NET MVC3 View 模型在回发到 Controller 时不为空但缺少数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9517963/

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