【问题标题】:FormMethod.Post returning 0FormMethod.Post 返回 0
【发布时间】:2018-02-23 20:02:21
【问题描述】:

我在使用 FormMethod Post 时遇到问题,我正在尝试发布一个值 (id) 并将其存储在 Session 变量中,但该值返回 0。

这是我的代码。

 @foreach (var item in Model)
                {
                    using (@Html.BeginForm("Index", "ProductSelec", FormMethod.Post))
                    {

                        @Html.HiddenFor(modelItem => item.id, new { value = "@Html.DisplayFor(modelItem => item.id)" })

                        <div class="AppOpt">
                            <button type="submit" name="submit" style="background-image: url('../Content/ICONS/SystemApp/@Html.DisplayFor(modelItem => item.img)');border-radius: 20px;background-size: cover;background-repeat: no-repeat;" class="AppImg">
                                <div class="OptNameRec">
                                    <div class="OptIcon">
                                        <img src='~/Content/ICONS/@Html.DisplayFor(modelItem => item.icon)'>
                                    </div>
                                    <div>
                                        <p>@Html.DisplayFor(modelItem => item.nombre)</p>
                                    </div>
                                    <div class="clear"></div>
                                </div>
                                <div class="OptImage"></div>
                            </button>
                        </div>
                    }
                }

表单在 foreach 中,因为我正在从数据库动态创建元素。

我想存储被点击的item.id。

这是我的控制器

        public ActionResult Index()
    {
        return View(db.aplicaciones.ToList());
    }

    public ActionResult ProductFamily()
    {
        return View();
    }

    [HttpPost]
    public int Index(aplicaciones aplicaciones)
    {
        Session["appID"] = aplicaciones.id;

        return aplicaciones.id;

    }

这是我的模型。

  public partial class aplicaciones
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public aplicaciones()
    {
        this.appNfam = new HashSet<appNfam>();
    }

    public int id { get; set; }
    public string nombre { get; set; }
    public string icon { get; set; }
    public string img { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<appNfam> appNfam { get; set; }
}

我试图创建另一个模型,但是当我添加时,foreach 没有从数据库中读取值。

希望你能帮助我。

【问题讨论】:

  • 为什么foreach前面有一个“@”?

标签: c# html post entity-framework-6 asp.net-mvc-5


【解决方案1】:

将您的控制器方法更改为:

[HttpPost]
public int Index(int id)
{
    Session["appID"] = id;

    return id;
}

将您的 Html.BeginForm 更改为:

@using (Html.BeginForm("Index", "ProductSelec", new { id = item.id },FormMethod.Post, new { })

您还应该能够删除隐藏字段,因为 ID 将在您的表单操作中自行发布。

【讨论】:

  • 这正是我想要的,但我还有另一个问题,当我更改我的 Html.BeginForm 时,我认为发生了一些奇怪的事情,加载按钮后,代码添加了这个。 System.Web.Mvc.Html.MvcForm System.Web.Mvc.Html.MvcForm。算了,我解决了,谢谢你的回答。
  • 去掉 Html.BeginForm 前的 @。它应该是@using (Html.BeginForm....., not using (@Html.BeginForm..... 更新了原始答案。
猜你喜欢
  • 2015-06-15
  • 1970-01-01
  • 2015-06-21
  • 2016-10-28
  • 2019-02-20
  • 2015-06-17
  • 2012-01-04
  • 2012-03-31
  • 2016-08-04
相关资源
最近更新 更多