gpt4 book ai didi

c# - 传递 @Html.DisplayFor() - 从 View 到 Controller 的值

转载 作者:行者123 更新时间:2023-11-30 18:28:09 24 4
gpt4 key购买 nike

我正在上这些课:

public class ProductViewModel
{
public IEnumerable<Product> Products { get; set; }

}

public class Product
{
public int ArticeNr { get; set; }
public string Name { get; set; }

}

还有这个观点

@model ProductsViewModel

@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<table class="table">

@foreach (var item in Model.Products) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.ArticleNr)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
</tr>
}
</table>

<input type="submit" value="submit" name="submitButton" />
}

如何将我的所有值从 Model.Products 传递到 Controller ?我希望将 foreach 循环中的所有值发送到 Controller

我的 Controller 将 ProductsViewModel 作为参数。但是我在 model.Products 中发布后的值是空的。

        public ActionResult Index(ProductsViewModel model) //here, model.Products is always null after post
{
//LOGIC
}

最佳答案

@Html.DisplayFor 只是在 View 中显示值。

如果你想在发布时将值传递回 Controller ,那么你还需要一个@Html.HiddenFor

关于c# - 传递 @Html.DisplayFor() - 从 View 到 Controller 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25563190/

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