【问题标题】:No values comes into FormCollection while passing data from View to controller将数据从 View 传递到控制器时没有值进入 FormCollection
【发布时间】:2014-02-27 06:41:04
【问题描述】:

我正在尝试将实体保存为编辑模式。 我想使用 FormCollection 将值从 View 传递到控制器,但没有值进入控制器。

这里是标记:

 @using (Html.BeginForm("Edit", "Product", FormMethod.Post, new { enctype = "multipart/form-data", id = "frmCreate" }))
{
    <div class =" row-fluid Span12" style ="margin-bottom :15px">
        <div class="span6" >
            <div class ="span4">
                <label>Product Name</label>
            </div>
            <div class ="span6">
                 <input type="text" value="@ViewBag.Name" id="ProductName" name="ProductName" style="height:20px; width:100%;" />
            </div>
        </div>
        <div class="span6" >
            <div class="span4" >
            <label > Product Code</label>
        </div>
        <div class="span6" >
            <input  type="text" value="@ViewBag.SectionCode" id="SectionCode"/> 
        </div>
    </div>
    <div class="span11" style="text-align:right">
        <input class="btn btn-primary" type="submit" value="Save" id="Edit"/>
        <button class="btn btn-default">Cancel</button>
    </div>
           }   

这是我的控制器代码

[HttpPost]
public ActionResult Edit(FormCollection form)
{
    Product product = new Product();
    product.SectionCode = Convert.ToString(form["SectionCode"]);
    product.Name = Convert.ToString(form["Name"]);

    return RedirectToAction("SaveData", oProductNew);
}

没有值进入 FormCollection。

【问题讨论】:

  • 我在你的 html 源代码中没有看到任何形式,也没有它的 html 助手。你如何声明表单并使用 post 制作它?
  • 你有什么理由使用FormCollection
  • 您的 SectionCode 输入字段没有名称。
  • 我想,我忘了把名字放在标签里...我认为 id 就足够了...

标签: c# asp.net-mvc-4 http-post formcollection


【解决方案1】:

可能,您的视图代码不属于表单。将其放入表单并尝试提交。

【讨论】:

    【解决方案2】:

    试试这个怎么样。在您的 [HttpGet] 方法中,您执行以下操作:

        [HttpGet]
        public ActionResult Edit()
        {
            Product product = new Product();
            return View(product);
        }
    

    对于[HttpPost],将您的代码更新为(这应该可以很好地用于绑定):

        [HttpPost]
        public ActionResult Edit(Product product)
        {
            return RedirectToAction("SaveData", product);
        }
    

    最后您可以更新您的视图以将产品作为视图模型:

        @model YourNamespace.Product @* Page View Model *@      
        @using (Html.BeginForm("Edit", "Product", FormMethod.Post, new { enctype = "multipart/form-data", id = "frmCreate" }))
        {
            <div class =" row-fluid Span12" style ="margin-bottom :15px">
                <div class="span6" >
                    <div class ="span4">
                        <label>Product Name</label>
                    </div>
                    <div class ="span6">
                        @Html.TextBoxFor(x => x.ProductName, new { @Style = "height:20px; width:100%;" }) @* TextBoxFor Extension Method *@
                    </div>
                </div>
                <div class="span6" >
                    <div class="span4" >
                    <label > Product Code</label>
                </div>
                <div class="span6" >
                    @Html.TextBoxFor(x => x.SectionCode)  @* TextBoxFor Extension Method *@          
                </div>
            </div>
            <div class="span11" style="text-align:right">
                <input class="btn btn-primary" type="submit" value="Save" id="Edit"/>
                <button class="btn btn-default">Cancel</button>
            </div>
        }   
    

    @Html 扩展方法将创建自动发生数据绑定所需的所有名称和 ID。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-04
      • 2014-02-19
      • 1970-01-01
      • 1970-01-01
      • 2020-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多