C# FormMethod.Post返回0
FormMethod Post有问题,我试图发布一个值(id)并将其存储在会话变量中,但该值返回0 这是我的密码C# FormMethod.Post返回0,c#,html,post,entity-framework-6,asp.net-mvc-5,C#,Html,Post,Entity Framework 6,Asp.net Mvc 5,FormMethod Post有问题,我试图发布一个值(id)并将其存储在会话变量中,但该值返回0 这是我的密码 @foreach (var item in Model) { using (@Html.BeginForm("Index", "ProductSelec", FormMethod.Post)) { @Html.Hidden
@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>
}
}
这是我的模型
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; }
}
公共部分类应用程序
{
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2214:DoNotCallOverridableMethodsInConstructors”)]
公共应用程序()
{
this.appNfam=new HashSet();
}
公共int id{get;set;}
公共字符串nombre{get;set;}
公共字符串图标{get;set;}
公共字符串img{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection appNfam{get;set;}
}
我试图创建另一个模型,但是当我添加时,foreach没有从数据库中读取值
希望您能帮助我。将控制器方法更改为:
[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将自动从表单操作中发布。为什么在
foreach
之前有一个“@”?这正是我想要的,但我有另一个问题,当我更改Html.BeginForm时,我的视图中会发生一些奇怪的事情,加载按钮后,代码会添加这个。System.Web.Mvc.Html.MvcForm System.Web.Mvc.Html.MvcForm。算了吧,我解决了这个问题,谢谢你的回答。删除@before Html.BeginForm。它应该是@using(Html.BeginForm…),而不是使用(@Html.BeginForm…)更新了原始答案。
@using (Html.BeginForm("Index", "ProductSelec", new { id = item.id },FormMethod.Post, new { })