【问题标题】:Passing @Html.DisplayFor() - values from view to controller将 @Html.DisplayFor() - 值从视图传递到控制器
【发布时间】:2022-04-20 11:21:03
【问题描述】:

我正在上这些课程:

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 传递到控制器?我希望将 foreach 循环中的所有值发送到控制器

我的控制器将 ProductsViewModel 作为参数。但是我在model.Products 发帖后的值是空的。

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

【问题讨论】:

  • DisplayFor() 不会为回发创建输入。您需要使用生成&lt;input&gt;&lt;select&gt; 元素的方法,例如EditorFor()TextBoxFor()HiddenFor() ....
  • @StephenMuecke 感谢您的回答。我试图在我的 foreach 循环中的每个属性上设置一个 HiddenFor() ,但在 postBack 到控制器上仍然是 null 。我错过了什么?
  • 您没有正确构建循环。你需要for(int i = 0; i &lt; Model.Projects.Count; i++) { @Html.HiddenFor(m =&gt; m.Projects[i].Name) ..。但是你到底为什么要发回一大堆隐藏的输入?
  • 从性能的角度来看,从数​​据库中再次获取集合通常总是会更好,尽管我不确定为什么在保存模型时需要再次获取集合跨度>
  • 为了性能,我建议您使用 AJAX (jquery $.get() 函数将值传递给操作方法,该方法调用数据库根据值过滤结果( s) 并返回可以在视图中更新的局部视图 - 即您不需要重建整个页面。

标签: c# asp.net-mvc asp.net-mvc-5


【解决方案1】:

@Html.DisplayFor 只显示视图中的值。

如果您希望在发布时将值传递回控制器,那么您还需要一个 @Html.HiddenFor

【讨论】:

    【解决方案2】:

    在这里,我将描述如何使用示例应用程序在 ASP.Net MVC 3 中发布集合。发布(将 HTML 字段值从视图传递到控制器)背后的基本思想是将呈现的 HTML 控件的名称字段与控制器的操作方法中的接收参数相匹配。
    ASP.Net MVC - How to Post a Collection

    【讨论】:

      【解决方案3】:

      正如我所见,您没有在 foreach 循环中修改您的值 - 因此您正在回发与您的视图收到的相同值以供显示。在这种情况下,保持模型类不变,但按如下方式更改索引视图和主控制器

      查看:

         @model Models.ProductViewModel
      
      
      
         @using (Html.BeginForm("Index", "Home", FormMethod.Post))
         {
          <table class="table">
      
          @foreach (var item in Model.Products)
          {
              <tr>
                  <td>
                      @item.ArticleNr
                  </td>
                  <td>
                      @item.Name
                  </td>
              </tr>
          }
          </table>
      
        <input type="submit" value="submit" />
        }
      

      控制器:

          [HttpGet]
          public ActionResult Index()
          {           
              return View(new ProductViewModel()); // this model contains products you want to display
          }
      
          [HttpPost]
          public ActionResult Index(ProductViewModel model)
          {
              // do smth with your model here
              return View();
          } 
      

      我怀疑您的问题是由于您的操作没有属性 [HttpGet] 和 [HttpPost]

      【讨论】:

        【解决方案4】:

        我有一个例子。这是 View 在按钮提交时发回表单值,并且这个值是从另一个控制器操作绑定的。

         @using (Html.BeginForm("Add", "Product", FormMethod.Post))
        {
            @Html.ActionLink("CheckOut", "Checkout", new { @class = "btn btn-success" })
            foreach (var item in Model)
            {
                <div class="row">
                    <div class="col-lg-3">
                        <img id="img" src="@Html.DisplayFor(modelItem => item.img)" alt="img" />
                    </div>
                    <div class="col-lg-3">
                        <input type="text" name="name" value="@item.name" border="0" />
                        <br />
                        <input id="text1" name="quantity" type="number" placeholder="Quantity" class="form-control text-muted" style="width:100px" />
                        <br />
                        <span>Price(per Kg):&nbsp;&nbsp;</span> &#8377;
                        <input type="text" name="price" value="@item.price" border="0" />
                        <br />
                        <input id="Button1" type="submit" value="ADD" />
                    </div>
                    <div class="col-lg-6">
                    </div>
                </div>
                <hr />
                <br />
            }
        }

        这是控制器中接收值的操作方法

         public ActionResult Add(string name,int quantity,int price)
            {
                Session["name"] = name;
                Session["quantity"] = quantity;
                Session["price"] = price;
                Session["Total"] = quantity * price;
                return RedirectToAction("UserView");
            }
        

        我认为这会有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多